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 |
b96b7e04 PH |
7 | USER_HOME=/home/$SUDO_USER |
8 | RESOURCES_TOP_DIR=$USER_HOME/Pimoroni | |
9 | WD=`pwd` | |
10 | ||
11 | user_check() { | |
12 | if [ $(id -u) -ne 0 ]; then | |
13 | printf "Script must be run as root. Try 'sudo ./install.sh'\n" | |
14 | exit 1 | |
15 | fi | |
16 | } | |
ab1eb580 | 17 | |
b96b7e04 PH |
18 | confirm() { |
19 | if [ "$FORCE" == '-y' ]; then | |
20 | true | |
21 | else | |
22 | read -r -p "$1 [y/N] " response < /dev/tty | |
23 | if [[ $response =~ ^(yes|y|Y)$ ]]; then | |
24 | true | |
25 | else | |
26 | false | |
27 | fi | |
28 | fi | |
29 | } | |
30 | ||
31 | prompt() { | |
32 | read -r -p "$1 [y/N] " response < /dev/tty | |
33 | if [[ $response =~ ^(yes|y|Y)$ ]]; then | |
34 | true | |
35 | else | |
36 | false | |
37 | fi | |
38 | } | |
39 | ||
40 | success() { | |
41 | echo -e "$(tput setaf 2)$1$(tput sgr0)" | |
42 | } | |
43 | ||
44 | inform() { | |
45 | echo -e "$(tput setaf 6)$1$(tput sgr0)" | |
46 | } | |
47 | ||
48 | warning() { | |
49 | echo -e "$(tput setaf 1)$1$(tput sgr0)" | |
50 | } | |
ab1eb580 PH |
51 | |
52 | function do_config_backup { | |
53 | if [ ! $CONFIG_BACKUP == true ]; then | |
54 | CONFIG_BACKUP=true | |
b96b7e04 PH |
55 | FILENAME="config.preinstall-$LIBRARY_NAME-$DATESTAMP.txt" |
56 | inform "Backing up $CONFIG to /boot/$FILENAME\n" | |
57 | cp $CONFIG /boot/$FILENAME | |
58 | mkdir -p $RESOURCES_TOP_DIR/config-backups/ | |
59 | cp $CONFIG $RESOURCES_TOP_DIR/config-backups/$FILENAME | |
60 | if [ -f "$UNINSTALLER" ]; then | |
61 | echo "cp $RESOURCES_TOP_DIR/config-backups/$FILENAME $CONFIG" >> $UNINSTALLER | |
62 | fi | |
ab1eb580 PH |
63 | fi |
64 | } | |
65 | ||
66 | function apt_pkg_install { | |
67 | PACKAGES=() | |
68 | PACKAGES_IN=("$@") | |
69 | for ((i = 0; i < ${#PACKAGES_IN[@]}; i++)); do | |
70 | PACKAGE="${PACKAGES_IN[$i]}" | |
71 | printf "Checking for $PACKAGE\n" | |
72 | dpkg -L $PACKAGE > /dev/null 2>&1 | |
73 | if [ "$?" == "1" ]; then | |
74 | PACKAGES+=("$PACKAGE") | |
75 | fi | |
76 | done | |
77 | PACKAGES="${PACKAGES[@]}" | |
78 | if ! [ "$PACKAGES" == "" ]; then | |
79 | echo "Installing missing packages: $PACKAGES" | |
b96b7e04 PH |
80 | if [ ! $APT_HAS_UPDATED ]; then |
81 | apt update | |
82 | APT_HAS_UPDATED=true | |
83 | fi | |
d7fc0b62 | 84 | apt install -y $PACKAGES |
b96b7e04 PH |
85 | if [ -f "$UNINSTALLER" ]; then |
86 | echo "apt uninstall -y $PACKAGES" | |
87 | fi | |
ab1eb580 PH |
88 | fi |
89 | } | |
90 | ||
b96b7e04 PH |
91 | user_check |
92 | ||
d7fc0b62 | 93 | apt_pkg_install python-configparser |
1b2cabb8 | 94 | |
d7fc0b62 PH |
95 | CONFIG_VARS=`python - <<EOF |
96 | from configparser import ConfigParser | |
97 | c = ConfigParser() | |
98 | c.read('library/setup.cfg') | |
99 | p = dict(c['pimoroni']) | |
100 | # Convert multi-line config entries into bash arrays | |
101 | for k in p.keys(): | |
102 | fmt = '"{}"' | |
103 | if '\n' in p[k]: | |
104 | p[k] = "'\n\t'".join(p[k].split('\n')[1:]) | |
105 | fmt = "('{}')" | |
106 | p[k] = fmt.format(p[k]) | |
107 | print(""" | |
108 | LIBRARY_NAME="{name}" | |
109 | LIBRARY_VERSION="{version}" | |
110 | """.format(**c['metadata'])) | |
111 | print(""" | |
112 | PY3_DEPS={py3deps} | |
113 | PY2_DEPS={py2deps} | |
114 | SETUP_CMDS={commands} | |
115 | CONFIG_TXT={configtxt} | |
116 | """.format(**p)) | |
117 | EOF` | |
651e1c20 | 118 | |
d7fc0b62 | 119 | if [ $? -ne 0 ]; then |
b96b7e04 | 120 | warning "Error parsing configuration...\n" |
651e1c20 PH |
121 | exit 1 |
122 | fi | |
123 | ||
d7fc0b62 PH |
124 | eval $CONFIG_VARS |
125 | ||
b96b7e04 PH |
126 | RESOURCES_DIR=$RESOURCES_TOP_DIR/$LIBRARY_NAME |
127 | UNINSTALLER=$RESOURCES_DIR/uninstall.sh | |
128 | ||
b58bac2f PH |
129 | mkdir -p $RESOURCES_DIR |
130 | ||
b96b7e04 PH |
131 | cat << EOF > $UNINSTALLER |
132 | printf "It's recommended you run these steps manually.\n" | |
133 | printf "If you want to run the full script, open it in\n" | |
134 | printf "an editor and remove 'exit 1' from below.\n" | |
135 | exit 1 | |
136 | EOF | |
137 | ||
d7fc0b62 PH |
138 | printf "$LIBRARY_NAME $LIBRARY_VERSION Python Library: Installer\n\n" |
139 | ||
651e1c20 PH |
140 | cd library |
141 | ||
142 | printf "Installing for Python 2..\n" | |
ab1eb580 | 143 | apt_pkg_install "${PY2_DEPS[@]}" |
b96b7e04 PH |
144 | python setup.py install > /dev/null |
145 | if [ $? -eq 0 ]; then | |
146 | success "Done!\n" | |
147 | echo "pip uninstall $LIBRARY_NAME" >> $UNINSTALLER | |
148 | fi | |
651e1c20 PH |
149 | |
150 | if [ -f "/usr/bin/python3" ]; then | |
151 | printf "Installing for Python 3..\n" | |
ab1eb580 | 152 | apt_pkg_install "${PY3_DEPS[@]}" |
b96b7e04 PH |
153 | python3 setup.py install > /dev/null |
154 | if [ $? -eq 0 ]; then | |
155 | success "Done!\n" | |
156 | echo "pip3 uninstall $LIBRARY_NAME" >> $UNINSTALLER | |
157 | fi | |
651e1c20 PH |
158 | fi |
159 | ||
b96b7e04 | 160 | cd $WD |
651e1c20 | 161 | |
ab1eb580 PH |
162 | for ((i = 0; i < ${#SETUP_CMDS[@]}; i++)); do |
163 | CMD="${SETUP_CMDS[$i]}" | |
164 | # Attempt to catch anything that touches /boot/config.txt and trigger a backup | |
165 | if [[ "$CMD" == *"raspi-config"* ]] || [[ "$CMD" == *"$CONFIG"* ]] || [[ "$CMD" == *"\$CONFIG"* ]]; then | |
166 | do_config_backup | |
167 | fi | |
168 | eval $CMD | |
169 | done | |
170 | ||
171 | for ((i = 0; i < ${#CONFIG_TXT[@]}; i++)); do | |
172 | CONFIG_LINE="${CONFIG_TXT[$i]}" | |
173 | if ! [ "$CONFIG_LINE" == "" ]; then | |
174 | do_config_backup | |
b96b7e04 | 175 | inform "Adding $CONFIG_LINE to $CONFIG\n" |
ab1eb580 PH |
176 | sed -i "s/^#$CONFIG_LINE/$CONFIG_LINE/" $CONFIG |
177 | if ! grep -q "^$CONFIG_LINE" $CONFIG; then | |
178 | printf "$CONFIG_LINE\n" >> $CONFIG | |
179 | fi | |
180 | fi | |
181 | done | |
d7ce2531 | 182 | |
b96b7e04 PH |
183 | if [ -d "examples" ]; then |
184 | if confirm "Would you like to copy examples to $RESOURCES_DIR?"; then | |
185 | inform "Copying examples to $RESOURCES_DIR" | |
b96b7e04 PH |
186 | cp -r examples/ $RESOURCES_DIR |
187 | echo "rm -r $RESOURCES_DIR" >> $UNINSTALLER | |
188 | fi | |
189 | fi | |
190 | ||
191 | success "\nAll done!" | |
192 | inform "If this is your first time installing you should reboot for hardware changes to tkae effect.\n" | |
193 | inform "Find uninstall steps in $UNINSTALLER\n" |