Merge pull request #8 from sepulworld/bug/adding_python_setuptools
[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
PH
5CONFIG_BACKUP=false
6
7CONFIG_VARS=`python - <<EOF
8from configparser import ConfigParser
9c = ConfigParser()
10c.read('library/setup.cfg')
11p = dict(c['pimoroni'])
12# Convert multi-line config entries into bash arrays
13for 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])
19print("""
20LIBRARY_NAME="{name}"
21LIBRARY_VERSION="{version}"
22""".format(**c['metadata']))
23print("""
24PY3_DEPS={py3deps}
25PY2_DEPS={py2deps}
26SETUP_CMDS={commands}
27CONFIG_TXT={configtxt}
28""".format(**p))
29EOF`
30
31if [ $? -ne 0 ]; then
32 printf "Error parsing configuration...\n"
33 exit 1
34fi
35
36function do_config_backup {
37 if [ ! $CONFIG_BACKUP == true ]; then
38 CONFIG_BACKUP=true
db36c95e 39 FILENAME="/boot/config.preinstall-$LIBRARY_NAME-$DATESTAMP.txt"
ab1eb580
PH
40 printf "Backing up $CONFIG to $FILENAME\n"
41 cp $CONFIG $FILENAME
42 fi
43}
44
45function 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
64eval $CONFIG_VARS
1b2cabb8
PH
65
66printf "$LIBRARY_NAME $LIBRARY_VERSION Python Library: Installer\n\n"
651e1c20
PH
67
68if [ $(id -u) -ne 0 ]; then
69 printf "Script must be run as root. Try 'sudo ./install.sh'\n"
70 exit 1
71fi
72
73cd library
74
75printf "Installing for Python 2..\n"
ab1eb580 76apt_pkg_install "${PY2_DEPS[@]}"
651e1c20
PH
77python setup.py install
78
79if [ -f "/usr/bin/python3" ]; then
80 printf "Installing for Python 3..\n"
ab1eb580 81 apt_pkg_install "${PY3_DEPS[@]}"
651e1c20
PH
82 python3 setup.py install
83fi
84
85cd ..
86
ab1eb580
PH
87for ((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
94done
95
96for ((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
106done
d7ce2531 107
651e1c20 108printf "Done!\n"