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