Commit | Line | Data |
---|---|---|
651e1c20 PH |
1 | #!/bin/bash |
2 | ||
d7ce2531 PH |
3 | CONFIG=/boot/config.txt |
4 | DATESTAMP=`date "+%Y-%M-%d-%H-%M-%S"` | |
ab1eb580 | 5 | CONFIG_BACKUP=false |
d7fc0b62 | 6 | APT_HAS_UPDATED=false |
ab1eb580 | 7 | |
d7fc0b62 PH |
8 | if [ $(id -u) -ne 0 ]; then |
9 | printf "Script must be run as root. Try 'sudo ./install.sh'\n" | |
ab1eb580 PH |
10 | exit 1 |
11 | fi | |
12 | ||
13 | function do_config_backup { | |
14 | if [ ! $CONFIG_BACKUP == true ]; then | |
15 | CONFIG_BACKUP=true | |
db36c95e | 16 | FILENAME="/boot/config.preinstall-$LIBRARY_NAME-$DATESTAMP.txt" |
ab1eb580 PH |
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" | |
d7fc0b62 PH |
36 | if [ ! $APT_HAS_UPDATED ]; then |
37 | apt update | |
38 | APT_HAS_UPDATED=true | |
39 | fi | |
40 | apt install -y $PACKAGES | |
ab1eb580 PH |
41 | fi |
42 | } | |
43 | ||
d7fc0b62 | 44 | apt_pkg_install python-configparser |
1b2cabb8 | 45 | |
d7fc0b62 PH |
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` | |
651e1c20 | 69 | |
d7fc0b62 PH |
70 | if [ $? -ne 0 ]; then |
71 | printf "Error parsing configuration...\n" | |
651e1c20 PH |
72 | exit 1 |
73 | fi | |
74 | ||
d7fc0b62 PH |
75 | eval $CONFIG_VARS |
76 | ||
77 | printf "$LIBRARY_NAME $LIBRARY_VERSION Python Library: Installer\n\n" | |
78 | ||
651e1c20 PH |
79 | cd library |
80 | ||
81 | printf "Installing for Python 2..\n" | |
ab1eb580 | 82 | apt_pkg_install "${PY2_DEPS[@]}" |
651e1c20 PH |
83 | python setup.py install |
84 | ||
85 | if [ -f "/usr/bin/python3" ]; then | |
86 | printf "Installing for Python 3..\n" | |
ab1eb580 | 87 | apt_pkg_install "${PY3_DEPS[@]}" |
651e1c20 PH |
88 | python3 setup.py install |
89 | fi | |
90 | ||
91 | cd .. | |
92 | ||
ab1eb580 PH |
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 | |
d7ce2531 | 113 | |
651e1c20 | 114 | printf "Done!\n" |