Merge branch 'installer-patch-1'
[EVA-2020-02-2.git] / install.sh
CommitLineData
651e1c20
PH
1#!/bin/bash
2
d7ce2531
PH
3CONFIG=/boot/config.txt
4DATESTAMP=`date "+%Y-%M-%d-%H-%M-%S"`
ab1eb580 5CONFIG_BACKUP=false
d7fc0b62 6APT_HAS_UPDATED=false
ab1eb580 7
d7fc0b62
PH
8if [ $(id -u) -ne 0 ]; then
9 printf "Script must be run as root. Try 'sudo ./install.sh'\n"
ab1eb580
PH
10 exit 1
11fi
12
13function 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
22function 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 44apt_pkg_install python-configparser
1b2cabb8 45
d7fc0b62
PH
46CONFIG_VARS=`python - <<EOF
47from configparser import ConfigParser
48c = ConfigParser()
49c.read('library/setup.cfg')
50p = dict(c['pimoroni'])
51# Convert multi-line config entries into bash arrays
52for 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])
58print("""
59LIBRARY_NAME="{name}"
60LIBRARY_VERSION="{version}"
61""".format(**c['metadata']))
62print("""
63PY3_DEPS={py3deps}
64PY2_DEPS={py2deps}
65SETUP_CMDS={commands}
66CONFIG_TXT={configtxt}
67""".format(**p))
68EOF`
651e1c20 69
d7fc0b62
PH
70if [ $? -ne 0 ]; then
71 printf "Error parsing configuration...\n"
651e1c20
PH
72 exit 1
73fi
74
d7fc0b62
PH
75eval $CONFIG_VARS
76
77printf "$LIBRARY_NAME $LIBRARY_VERSION Python Library: Installer\n\n"
78
651e1c20
PH
79cd library
80
81printf "Installing for Python 2..\n"
ab1eb580 82apt_pkg_install "${PY2_DEPS[@]}"
651e1c20
PH
83python setup.py install
84
85if [ -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
89fi
90
91cd ..
92
ab1eb580
PH
93for ((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
100done
101
102for ((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
112done
d7ce2531 113
651e1c20 114printf "Done!\n"