Merge branch 'installer-patch-1'
[EVA-2020-02-2.git] / install.sh
... / ...
CommitLineData
1#!/bin/bash
2
3CONFIG=/boot/config.txt
4DATESTAMP=`date "+%Y-%M-%d-%H-%M-%S"`
5CONFIG_BACKUP=false
6APT_HAS_UPDATED=false
7
8if [ $(id -u) -ne 0 ]; then
9 printf "Script must be run as root. Try 'sudo ./install.sh'\n"
10 exit 1
11fi
12
13function 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
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"
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
44apt_pkg_install python-configparser
45
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`
69
70if [ $? -ne 0 ]; then
71 printf "Error parsing configuration...\n"
72 exit 1
73fi
74
75eval $CONFIG_VARS
76
77printf "$LIBRARY_NAME $LIBRARY_VERSION Python Library: Installer\n\n"
78
79cd library
80
81printf "Installing for Python 2..\n"
82apt_pkg_install "${PY2_DEPS[@]}"
83python setup.py install
84
85if [ -f "/usr/bin/python3" ]; then
86 printf "Installing for Python 3..\n"
87 apt_pkg_install "${PY3_DEPS[@]}"
88 python3 setup.py install
89fi
90
91cd ..
92
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
113
114printf "Done!\n"