3 CONFIG
=/boot
/config.txt
4 DATESTAMP
=`date "+%Y-%M-%d-%H-%M-%S"`
7 USER_HOME
=/home
/$SUDO_USER
8 RESOURCES_TOP_DIR
=$USER_HOME/Pimoroni
12 if [ $
(id
-u) -ne 0 ]; then
13 printf "Script must be run as root. Try 'sudo ./install.sh'\n"
19 if [ "$FORCE" == '-y' ]; then
22 read -r -p "$1 [y/N] " response
< /dev
/tty
23 if [[ $response =~ ^
(yes|y|Y
)$
]]; then
32 read -r -p "$1 [y/N] " response
< /dev
/tty
33 if [[ $response =~ ^
(yes|y|Y
)$
]]; then
41 echo -e "$(tput setaf 2)$1$(tput sgr0)"
45 echo -e "$(tput setaf 6)$1$(tput sgr0)"
49 echo -e "$(tput setaf 1)$1$(tput sgr0)"
52 function do_config_backup
{
53 if [ ! $CONFIG_BACKUP == true
]; then
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
66 function apt_pkg_install
{
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")
77 PACKAGES
="${PACKAGES[@]}"
78 if ! [ "$PACKAGES" == "" ]; then
79 echo "Installing missing packages: $PACKAGES"
80 if [ ! $APT_HAS_UPDATED ]; then
84 apt
install -y $PACKAGES
85 if [ -f "$UNINSTALLER" ]; then
86 echo "apt uninstall -y $PACKAGES"
93 apt_pkg_install python-configparser
95 CONFIG_VARS
=`python - <<EOF
96 from configparser import ConfigParser
98 c.read('library/setup.cfg')
99 p = dict(c['pimoroni'])
100 # Convert multi-line config entries into bash arrays
104 p[k] = "'\n\t'".join(p[k].split('\n')[1:])
106 p[k] = fmt.format(p[k])
108 LIBRARY_NAME="{name}"
109 LIBRARY_VERSION="{version}"
110 """.format(**c['metadata']))
114 SETUP_CMDS={commands}
115 CONFIG_TXT={configtxt}
119 if [ $?
-ne 0 ]; then
120 warning
"Error parsing configuration...\n"
126 RESOURCES_DIR
=$RESOURCES_TOP_DIR/$LIBRARY_NAME
127 UNINSTALLER
=$RESOURCES_DIR/uninstall.sh
129 cat << EOF > $UNINSTALLER
130 printf "It's recommended you run these steps manually.\n"
131 printf "If you want to run the full script, open it in\n"
132 printf "an editor and remove 'exit 1' from below.\n"
136 printf "$LIBRARY_NAME $LIBRARY_VERSION Python Library: Installer\n\n"
140 printf "Installing for Python 2..\n"
141 apt_pkg_install
"${PY2_DEPS[@]}"
142 python setup.py
install > /dev
/null
143 if [ $?
-eq 0 ]; then
145 echo "pip uninstall $LIBRARY_NAME" >> $UNINSTALLER
148 if [ -f "/usr/bin/python3" ]; then
149 printf "Installing for Python 3..\n"
150 apt_pkg_install
"${PY3_DEPS[@]}"
151 python3 setup.py
install > /dev
/null
152 if [ $?
-eq 0 ]; then
154 echo "pip3 uninstall $LIBRARY_NAME" >> $UNINSTALLER
160 for ((i
= 0; i
< ${#SETUP_CMDS[@]}; i
++)); do
161 CMD
="${SETUP_CMDS[$i]}"
162 # Attempt to catch anything that touches /boot/config.txt and trigger a backup
163 if [[ "$CMD" == *"raspi-config"* ]] ||
[[ "$CMD" == *"$CONFIG"* ]] ||
[[ "$CMD" == *"\$CONFIG"* ]]; then
169 for ((i
= 0; i
< ${#CONFIG_TXT[@]}; i
++)); do
170 CONFIG_LINE
="${CONFIG_TXT[$i]}"
171 if ! [ "$CONFIG_LINE" == "" ]; then
173 inform
"Adding $CONFIG_LINE to $CONFIG\n"
174 sed -i "s/^#$CONFIG_LINE/$CONFIG_LINE/" $CONFIG
175 if ! grep -q "^$CONFIG_LINE" $CONFIG; then
176 printf "$CONFIG_LINE\n" >> $CONFIG
181 if [ -d "examples" ]; then
182 if confirm
"Would you like to copy examples to $RESOURCES_DIR?"; then
183 inform
"Copying examples to $RESOURCES_DIR"
184 mkdir
-p $RESOURCES_DIR
185 cp -r examples
/ $RESOURCES_DIR
186 echo "rm -r $RESOURCES_DIR" >> $UNINSTALLER
190 success
"\nAll done!"
191 inform
"If this is your first time installing you should reboot for hardware changes to tkae effect.\n"
192 inform
"Find uninstall steps in $UNINSTALLER\n"