| 1 | #!/bin/bash |
| 2 | |
| 3 | CONFIG=/boot/config.txt |
| 4 | DATESTAMP=`date "+%Y-%M-%d-%H-%M-%S"` |
| 5 | CONFIG_BACKUP=false |
| 6 | |
| 7 | CONFIG_VARS=`python - <<EOF |
| 8 | from configparser import ConfigParser |
| 9 | c = ConfigParser() |
| 10 | c.read('library/setup.cfg') |
| 11 | p = dict(c['pimoroni']) |
| 12 | # Convert multi-line config entries into bash arrays |
| 13 | for k in p.keys(): |
| 14 | fmt = '"{}"' |
| 15 | if '\n' in p[k]: |
| 16 | p[k] = "'\n\t'".join(p[k].split('\n')[1:]) |
| 17 | fmt = "('{}')" |
| 18 | p[k] = fmt.format(p[k]) |
| 19 | print(""" |
| 20 | LIBRARY_NAME="{name}" |
| 21 | LIBRARY_VERSION="{version}" |
| 22 | """.format(**c['metadata'])) |
| 23 | print(""" |
| 24 | PY3_DEPS={py3deps} |
| 25 | PY2_DEPS={py2deps} |
| 26 | SETUP_CMDS={commands} |
| 27 | CONFIG_TXT={configtxt} |
| 28 | """.format(**p)) |
| 29 | EOF` |
| 30 | |
| 31 | if [ $? -ne 0 ]; then |
| 32 | printf "Error parsing configuration...\n" |
| 33 | exit 1 |
| 34 | fi |
| 35 | |
| 36 | function do_config_backup { |
| 37 | if [ ! $CONFIG_BACKUP == true ]; then |
| 38 | CONFIG_BACKUP=true |
| 39 | FILENAME="/boot/config.preinstall-$LIBRARY_NAME-$DATESTAMP.txt" |
| 40 | printf "Backing up $CONFIG to $FILENAME\n" |
| 41 | cp $CONFIG $FILENAME |
| 42 | fi |
| 43 | } |
| 44 | |
| 45 | function apt_pkg_install { |
| 46 | PACKAGES=() |
| 47 | PACKAGES_IN=("$@") |
| 48 | for ((i = 0; i < ${#PACKAGES_IN[@]}; i++)); do |
| 49 | PACKAGE="${PACKAGES_IN[$i]}" |
| 50 | printf "Checking for $PACKAGE\n" |
| 51 | dpkg -L $PACKAGE > /dev/null 2>&1 |
| 52 | if [ "$?" == "1" ]; then |
| 53 | PACKAGES+=("$PACKAGE") |
| 54 | fi |
| 55 | done |
| 56 | PACKAGES="${PACKAGES[@]}" |
| 57 | if ! [ "$PACKAGES" == "" ]; then |
| 58 | echo "Installing missing packages: $PACKAGES" |
| 59 | sudo apt update |
| 60 | sudo apt install -y $PACKAGES |
| 61 | fi |
| 62 | } |
| 63 | |
| 64 | eval $CONFIG_VARS |
| 65 | |
| 66 | printf "$LIBRARY_NAME $LIBRARY_VERSION Python Library: Installer\n\n" |
| 67 | |
| 68 | if [ $(id -u) -ne 0 ]; then |
| 69 | printf "Script must be run as root. Try 'sudo ./install.sh'\n" |
| 70 | exit 1 |
| 71 | fi |
| 72 | |
| 73 | cd library |
| 74 | |
| 75 | printf "Installing for Python 2..\n" |
| 76 | apt_pkg_install "${PY2_DEPS[@]}" |
| 77 | python setup.py install |
| 78 | |
| 79 | if [ -f "/usr/bin/python3" ]; then |
| 80 | printf "Installing for Python 3..\n" |
| 81 | apt_pkg_install "${PY3_DEPS[@]}" |
| 82 | python3 setup.py install |
| 83 | fi |
| 84 | |
| 85 | cd .. |
| 86 | |
| 87 | for ((i = 0; i < ${#SETUP_CMDS[@]}; i++)); do |
| 88 | CMD="${SETUP_CMDS[$i]}" |
| 89 | # Attempt to catch anything that touches /boot/config.txt and trigger a backup |
| 90 | if [[ "$CMD" == *"raspi-config"* ]] || [[ "$CMD" == *"$CONFIG"* ]] || [[ "$CMD" == *"\$CONFIG"* ]]; then |
| 91 | do_config_backup |
| 92 | fi |
| 93 | eval $CMD |
| 94 | done |
| 95 | |
| 96 | for ((i = 0; i < ${#CONFIG_TXT[@]}; i++)); do |
| 97 | CONFIG_LINE="${CONFIG_TXT[$i]}" |
| 98 | if ! [ "$CONFIG_LINE" == "" ]; then |
| 99 | do_config_backup |
| 100 | printf "Adding $CONFIG_LINE to $CONFIG\n" |
| 101 | sed -i "s/^#$CONFIG_LINE/$CONFIG_LINE/" $CONFIG |
| 102 | if ! grep -q "^$CONFIG_LINE" $CONFIG; then |
| 103 | printf "$CONFIG_LINE\n" >> $CONFIG |
| 104 | fi |
| 105 | fi |
| 106 | done |
| 107 | |
| 108 | printf "Done!\n" |