#!/bin/bash

mkdir -p ~/rpiaddons-pkgs
rm -f ~/rpi-addons-pkgs/*
PKGDIR="/home/pi/rpiaddons-pkgs/"

# Amiberry (need deb)

# AppImageLauncher
LATEST=`curl -s https://api.github.com/repos/TheAssassin/AppImageLauncher/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/TheAssassin/AppImageLauncher/releases/latest \
  | grep browser_download_url \
  | grep 'armhf.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/appimagelauncher-$LATEST-armhf.deb

curl -s https://api.github.com/repos/TheAssassin/AppImageLauncher/releases/latest \
  | grep browser_download_url \
  | grep 'arm64.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/appimagelauncher-$LATEST-arm64.deb

# ArchiveBox
LATEST=`curl -s https://api.github.com/repos/ArchiveBox/ArchiveBox/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/ArchiveBox/ArchiveBox/releases/latest \
  | grep browser_download_url \
  | grep 'all.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/archivebox-$LATEST-all.deb

# bpytop (need deb) (see azlux scripts)

# bashtop (need deb) (see azlux scripts)

# bat
LATEST=`curl -s https://api.github.com/repos/sharkdp/bat/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/sharkdp/bat/releases/latest \
  | grep browser_download_url \
  | grep 'armhf.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/bat-$LATEST-armhf.deb

curl -s https://api.github.com/repos/sharkdp/bat/releases/latest \
  | grep browser_download_url \
  | grep 'arm64.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/bat-$LATEST-arm64.deb

# BleachBit
LATEST=`curl -s https://api.github.com/repos/bleachbit/bleachbit/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")' | tr -d 'v'`
wget https://download.bleachbit.org/bleachbit_$LATEST-0_all_debian10.deb -O $PKGDIR/bleachbit_$LATEST_all.deb

# BlockPi
LATEST=`curl -s https://api.github.com/repos/alienzhangyw/BlockPi/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/alienzhangyw/BlockPi/releases/latest \
  | grep browser_download_url \
  | grep 'armv7l.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/blockpi-$LATEST-armhf.deb

curl -s https://api.github.com/repos/alienzhangyw/BlockPi/releases/latest \
  | grep browser_download_url \
  | grep 'arm64.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/blockpi-$LATEST-arm64.deb

# Box64

# Box86
apt download box86 
mv box86_* $PKGDIR

# broot (need deb) (see azlux scripts)

# browsh
LATEST=`curl -s https://api.github.com/repos/browsh-org/browsh/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/browsh-org/browsh/releases/latest \
  | grep browser_download_url \
  | grep 'armv7.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/browsh-$LATEST-armhf.deb

curl -s https://api.github.com/repos/browsh-org/browsh/releases/latest \
  | grep browser_download_url \
  | grep 'arm64.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/browsh-$LATEST-arm64.deb

# bulky
#LATEST=`curl -s https://api.github.com/repos/linuxmint/bulky/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")' | tr -d 'v'`

# cbonsai (need deb)

# caddy
LATEST=`curl -s https://api.github.com/repos/caddyserver/caddy/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/caddyserver/caddy/releases/latest \
  | grep browser_download_url \
  | grep 'armv7.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/caddy-$LATEST-armhf.deb

curl -s https://api.github.com/repos/caddyserver/caddy/releases/latest \
  | grep browser_download_url \
  | grep 'arm64.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/caddy-$LATEST-arm64.deb

# cfetch (need deb)

# cherrytree
#the -3 at the end of the version number causes this to not work
#LATEST=`curl -s https://api.github.com/repos/giuspen/cherrytree/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")' | tr -d 'v'`
#https://launchpad.net/~giuspen/+archive/ubuntu/ppa/+files/cherrytree_0.99.40-3_arm64.deb

# clamtk
LATEST=`curl -s https://api.github.com/repos/dave-theunsub/clamtk/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/dave-theunsub/clamtk/releases/latest \
  | grep browser_download_url \
  | grep 'all.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/clamtk-$LATEST-all.deb

# code-server
LATEST=`curl -s https://api.github.com/repos/cdr/code-server/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/cdr/code-server/releases/latest \
  | grep browser_download_url \
  | grep 'arm64.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/code-server-$LATEST-arm64.deb

# VSCodium
LATEST=`curl -s https://api.github.com/repos/VSCodium/VSCodium/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/VSCodium/VSCodium/releases/latest \
  | grep browser_download_url \
  | grep 'armhf.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/codium-$LATEST-armhf.deb

curl -s https://api.github.com/repos/VSCodium/VSCodium/releases/latest \
  | grep browser_download_url \
  | grep 'arm64.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/codium-$LATEST-arm64.deb

# Foliate
LATEST=`curl -s https://api.github.com/repos/johnfactotum/foliate/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/johnfactotum/foliate/releases/latest \
  | grep browser_download_url \
  | grep 'all.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/foliate-$LATEST-all.deb

# croc
LATEST=`curl -s https://api.github.com/repos/schollz/croc/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/schollz/croc/releases/latest \
  | grep browser_download_url \
  | grep 'ARM.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/croc-$LATEST-armhf.deb

curl -s https://api.github.com/repos/schollz/croc/releases/latest \
  | grep browser_download_url \
  | grep 'ARM64.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/croc-$LATEST-arm64.deb

# Deskreen (no deb)
# instructions here: https://github.com/chunky-milk/raspbian-addons/issues/48

# drawing
LATEST=`curl -s https://api.github.com/repos/maoschanz/drawing/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
wget https://launchpad.net/~cartes/+archive/ubuntu/drawing/+files/drawing_$LATEST-0ubuntu1~hirsute_arm64.deb -O $PKGDIR/
wget https://launchpad.net/~cartes/+archive/ubuntu/drawing/+files/drawing_$LATEST-0ubuntu1~hirsute_armhf.deb -O $PKGDIR/

# DuckStation (need deb)

# duf
LATEST=`curl -s https://api.github.com/repos/muesli/duf/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/muesli/duf/releases/latest \
  | grep browser_download_url \
  | grep 'armv7.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/duf-$LATEST-armhf.deb

curl -s https://api.github.com/repos/muesli/duf/releases/latest \
  | grep browser_download_url \
  | grep 'arm64.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/duf-$LATEST-arm64.deb

# fancy-cursor-themes (need deb)
# todo: create github repo and deb script

# figma-linux
LATEST=`curl -s https://api.github.com/repos/Figma-Linux/figma-linux/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/Figma-Linux/figma-linux/releases/latest \
  | grep browser_download_url \
  | grep 'arm64.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/figma-linux-$LATEST-arm64.deb

# flameshot
LATEST=`curl -s https://api.github.com/repos/flameshot-org/flameshot/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/flameshot-org/flameshot/releases/latest \
  | grep browser_download_url \
  | grep 'armhf.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/flameshot-$LATEST-armhf.deb

curl -s https://api.github.com/repos/flameshot-org/flameshot/releases/latest \
  | grep browser_download_url \
  | grep 'arm64.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/flameshot-$LATEST-arm64.deb

# freecad (need deb)
# todo: make deb creation script & upload to freecad-rpi repository

# freetube
#armhf: no deb: use script to create
#todo arm64: unable to use github api, all releases are marked as pre-releases

# github cli
LATEST=`curl -s https://api.github.com/repos/cli/cli/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/cli/cli/releases/latest \
  | grep browser_download_url \
  | grep 'armv6.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/gh-$LATEST-armhf.deb

curl -s https://api.github.com/repos/cli/cli/releases/latest \
  | grep browser_download_url \
  | grep 'arm64.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/gh-$LATEST-arm64.deb

# gitea
apt download gitea:armhf
apt download gitea:arm64
mv gitea_* $PKGDIR

# goreleaser
LATEST=`curl -s https://api.github.com/repos/goreleaser/goreleaser/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/goreleaser/goreleaser/releases/latest \
  | grep browser_download_url \
  | grep 'armv6.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/goreleaser-$LATEST-armhf.deb

curl -s https://api.github.com/repos/goreleaser/goreleaser/releases/latest \
  | grep browser_download_url \
  | grep 'arm64.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/goreleaser-$LATEST-arm64.deb

# halo weather
LATEST=`curl -s https://api.github.com/repos/cijo7/halo/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/cijo7/halo/releases/latest \
  | grep browser_download_url \
  | grep 'all.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/python3-halo-weather-$LATEST-all.deb

# hypnotix
LATEST=`curl -s https://api.github.com/repos/linuxmint/hypnotix/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/linuxmint/hypnotix/releases/latest \
  | grep browser_download_url \
  | grep 'all.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/hypnotix-$LATEST-all.deb

# angry ip scanner
LATEST=`curl -s https://api.github.com/repos/angryip/ipscan/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/angryip/ipscan/releases/latest \
  | grep browser_download_url \
  | grep 'all.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/ipscan-$LATEST-all.deb

# log2ram
#LATEST=`curl -s https://api.github.com/repos/azlux/log2ram/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
#wget http://packages.azlux.fr/debian/pool/main/l/log2ram/log2ram_$LATEST_all.deb -O $PKGDIR/

# logo-ls
LATEST=`curl -s https://api.github.com/repos/Yash-Handa/logo-ls/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
curl -s https://api.github.com/repos/Yash-Handa/logo-ls/releases/latest \
  | grep browser_download_url \
  | grep 'armv6.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/logo-ls-$LATEST-armhf.deb

curl -s https://api.github.com/repos/Yash-Handa/logo-ls/releases/latest \
  | grep browser_download_url \
  | grep 'arm64.deb"' \
  | cut -d '"' -f 4 \
  | xargs -n 1 curl -L -o $PKGDIR/logo-ls-$LATEST-arm64.deb

