# https://forum.kicad.info/t/nightly-builds-for-debian-buster-and-bullseye/27487
#RELEASE=bookworm
RELEASE=trixie
#RELEASE=sid
# rm /etc/apt/sources.list-available/kicad_nightly-buster.list /etc/apt/sources.list-available/kicad_nightly-bullseye.list
# rm /etc/apt/sources.list-available/kicad_nightly-buster.list /etc/apt/sources.list-available/kicad_nightly-buster.list
# rm /etc/apt/sources.list.d/kicad_nightly-bullseye.list /etc/apt/sources.list.d/kicad_nightly-buster.list

#echo "deb [arch=amd64] https://debian.sur5r.net/kicad-nightly buster main" | tee /etc/apt/sources.list-available/kicad_nightly-${RELEASE}.list

ARCH="$(dpkg --print-architecture)"
KEY_DIR="/etc/apt/trusted.gpg.d"
KEY=${KEY_DIR}/sur5r-keyring.gpg

# RECV_KEYS="E3CA1A89941C42E6 C3DE1DD4F661CDCB"
# gpg --keyserver keyserver.ubuntu.com --recv-keys "${RECV_KEYS}"
# gpg --export --armor "${RECV_KEYS}" >"${KEY}"

#The repository is signed by my debian.sur5r.net key, Fingerprint
#
#pub   rsa4096/E3CA1A89941C42E6 2015-01-24 [SC] [expires: 2022-03-08]`
#      Key fingerprint = BFD9 0F4D AAEF A72B 67BB  AF48 E3CA 1A89 941C 42E6`
#uid                 [  full  ] debian.sur5r.net Archive Automatic Signing Key 2015 (sur5r) <debian@sur5r.net>
#
#which is also available in the package sur5r-keyring from that very repo.

# The easiest way to install the keyring from https://debian.sur5r.net/kicad-nightly/pool/main/s/sur5r-keyring/ beforehand is:
# KEYDATE="2021.02.02"
# KEYDATE="2021.02.17"
# KEYDATE="2023.02.18"
KEYDATE="2024.03.04"
VERSION="8.99"

# /usr/lib/apt/apt-helper download-file https://debian.sur5r.net/i3/pool/main/s/sur5r-keyring/sur5r-keyring_2024.03.04_all.deb keyring.deb SHA256:f9bb4340b5ce0ded29b7e014ee9ce788006e9bbfe31e96c09b2118ab91fca734
# /usr/lib/apt/apt-helper download-file https://debian.sur5r.net/kicad-nightly/pool/main/s/sur5r-keyring/sur5r-keyring_2021.02.02_all.deb sur5r-keyring.deb SHA256:cccfb1dd7d6b1b6a137bb96ea5b5eef18a0a4a6df1d6c0c37832025d2edaa710
/usr/lib/apt/apt-helper download-file https://debian.sur5r.net/kicad-nightly/pool/main/s/sur5r-keyring/sur5r-keyring_${KEYDATE}_all.deb sur5r-keyring.deb
# /usr/lib/apt/apt-helper download-file https://debian.sur5r.net/sur5r-keyring/pool/main/s/sur5r-keyring/sur5r-keyring_${KEYDATE}_all.deb sur5r-keyring.deb
dpkg -i ./sur5r-keyring.deb

# dpkg -S sur5r-keyring
# sur5r-keyring: /usr/share/doc/sur5r-keyring/copyright
# sur5r-keyring: /usr/share/doc/sur5r-keyring/changelog.gz
# sur5r-keyring: /etc/apt/trusted.gpg.d/sur5r-keyring.gpg
# sur5r-keyring: /usr/share/lintian/overrides/sur5r-keyring
# sur5r-keyring: /usr/share/keyrings/sur5r-keyring.gpg
# sur5r-keyring: /usr/share/doc/sur5r-keyring

cat <<-EOF | tee /etc/apt/sources.list-available/kicad_nightly-${RELEASE}.list
	deb [arch=${ARCH} signed-by=${KEY}] https://debian.sur5r.net/kicad-nightly ${RELEASE} main
	deb-src [arch=${ARCH} signed-by=${KEY}] https://debian.sur5r.net/kicad-nightly ${RELEASE} main
EOF

#ln -sf /etc/apt/sources.list-available/kicad_nightly-${RELEASE}.list /etc/apt/sources.list.d/kicad_nightly-${RELEASE}.list

ln -sf /etc/apt/sources.list-available/kicad_nightly-${RELEASE}.list /etc/apt/sources.list.d/kicad_nightly-${RELEASE}.list

apt-get update
apt-get install \
	kicad-nightly \
	kicad-nightly-demos \
	kicad-nightly-footprints \
	kicad-nightly-libraries \
	kicad-nightly-packages3d \
	kicad-nightly-symbols \
	kicad-nightly-templates
#
# Deps for spice and sycira (maxima) and spice
# sudo apt-get install -y ngspice maxima wxmaxima libtinyxml2-6 libzip5

#mv /usr/share/kicad-nightly/templates/fp-lib-table ~/.config/kicad/7.0/

# Plugins
# Search path from KiPython console:
## import pcbnew
## print(pcbnew.GetWizardsSearchPaths())
### /usr/share/kicad-nightly/scripting
### /usr/share/kicad-nightly/scripting/plugins
### /home/default/.config/kicad/5.99/scripting
### /home/default/.config/kicad/5.99/scripting/plugins
### /home/default/.local/share/kicad/5.99/scripting
### /home/default/.local/share/kicad/5.99/scripting/plugins
### /home/default/.local/share/kicad/5.99/3rdparty/plugins
# ~/.kicad_plugins/
KICAD_SCRIPTS=${HOME}/.local/share/kicad/${VERSION}/scripting/plugins/
mkdir -p "${KICAD_SCRIPTS}"

# Rounded tracks
#git clone https://github.com/mitxela/kicad-round-tracks ${KICAD_SCRIPTS}/kicad-round-tracks

# teardrop
#git clone -b kicad6 https://github.com/sekigon-gonnoc/kicad_scripts /tmp/kicad_scripts
#cp -a /tmp/kicad_scripts/teardrops ${KICAD_SCRIPTS}/
#rm -rf /tmp/kicad_scripts
