#!/bin/bash
-LIBRARY_VERSION=`cat library/setup.py | grep version | awk -F"'" '{print $2}'`
-LIBRARY_NAME=`cat library/setup.py | grep name | awk -F"'" '{print $2}'`
CONFIG=/boot/config.txt
DATESTAMP=`date "+%Y-%M-%d-%H-%M-%S"`
+CONFIG_BACKUP=false
+
+CONFIG_VARS=`python - <<EOF
+from configparser import ConfigParser
+c = ConfigParser()
+c.read('library/setup.cfg')
+p = dict(c['pimoroni'])
+# Convert multi-line config entries into bash arrays
+for k in p.keys():
+ fmt = '"{}"'
+ if '\n' in p[k]:
+ p[k] = "'\n\t'".join(p[k].split('\n')[1:])
+ fmt = "('{}')"
+ p[k] = fmt.format(p[k])
+print("""
+LIBRARY_NAME="{name}"
+LIBRARY_VERSION="{version}"
+""".format(**c['metadata']))
+print("""
+PY3_DEPS={py3deps}
+PY2_DEPS={py2deps}
+SETUP_CMDS={commands}
+CONFIG_TXT={configtxt}
+""".format(**p))
+EOF`
+
+if [ $? -ne 0 ]; then
+ printf "Error parsing configuration...\n"
+ exit 1
+fi
+
+function do_config_backup {
+ if [ ! $CONFIG_BACKUP == true ]; then
+ CONFIG_BACKUP=true
+ FILENAME="config.preinstall-$LIBRARY_NAME-$DATESTAMP.txt"
+ printf "Backing up $CONFIG to $FILENAME\n"
+ cp $CONFIG $FILENAME
+ fi
+}
+
+function apt_pkg_install {
+ PACKAGES=()
+ PACKAGES_IN=("$@")
+ for ((i = 0; i < ${#PACKAGES_IN[@]}; i++)); do
+ PACKAGE="${PACKAGES_IN[$i]}"
+ printf "Checking for $PACKAGE\n"
+ dpkg -L $PACKAGE > /dev/null 2>&1
+ if [ "$?" == "1" ]; then
+ PACKAGES+=("$PACKAGE")
+ fi
+ done
+ PACKAGES="${PACKAGES[@]}"
+ if ! [ "$PACKAGES" == "" ]; then
+ echo "Installing missing packages: $PACKAGES"
+ sudo apt update
+ sudo apt install -y $PACKAGES
+ fi
+}
+
+eval $CONFIG_VARS
printf "$LIBRARY_NAME $LIBRARY_VERSION Python Library: Installer\n\n"
cd library
printf "Installing for Python 2..\n"
+apt_pkg_install "${PY2_DEPS[@]}"
python setup.py install
if [ -f "/usr/bin/python3" ]; then
printf "Installing for Python 3..\n"
+ apt_pkg_install "${PY3_DEPS[@]}"
python3 setup.py install
fi
cd ..
-printf "Backing up $CONFIG\n"
-cp $CONFIG "config.preinstall-$DATESTAMP.txt"
-
-printf "Setting up i2c and SPI..\n"
-raspi-config nonint do_spi 0
-raspi-config nonint do_i2c 0
-
-printf "Setting up serial for PMS5003..\n"
-# Disable serial terminal over /dev/ttyAMA0
-raspi-config nonint do_serial 1
-# Enable serial port
-raspi-config nonint set_config_var enable_uart 1 $CONFIG
-# Switch serial port to full UART for stability (may adversely affect bluetooth)
-sed -i 's/^#dtoverlay=pi3-miniuart-bt/dtoverlay=pi3-miniuart-bt/' $CONFIG
-if ! grep -q -E "^dtoverlay=pi3-miniuart-bt" $CONFIG; then
- printf "dtoverlay=pi3-miniuart-bt\n" >> $CONFIG
-fi
+for ((i = 0; i < ${#SETUP_CMDS[@]}; i++)); do
+ CMD="${SETUP_CMDS[$i]}"
+ # Attempt to catch anything that touches /boot/config.txt and trigger a backup
+ if [[ "$CMD" == *"raspi-config"* ]] || [[ "$CMD" == *"$CONFIG"* ]] || [[ "$CMD" == *"\$CONFIG"* ]]; then
+ do_config_backup
+ fi
+ eval $CMD
+done
+
+for ((i = 0; i < ${#CONFIG_TXT[@]}; i++)); do
+ CONFIG_LINE="${CONFIG_TXT[$i]}"
+ if ! [ "$CONFIG_LINE" == "" ]; then
+ do_config_backup
+ printf "Adding $CONFIG_LINE to $CONFIG\n"
+ sed -i "s/^#$CONFIG_LINE/$CONFIG_LINE/" $CONFIG
+ if ! grep -q "^$CONFIG_LINE" $CONFIG; then
+ printf "$CONFIG_LINE\n" >> $CONFIG
+ fi
+ fi
+done
printf "Done!\n"