X-Git-Url: https://zdv2.bktei.com/gitweb/EVA-2020-02-2.git/blobdiff_plain/b17f34b42aa5e6ca959d8f3d748012c87d4d8999..d7fc0b62ccbcbf42f2510e76726e8b4c5a5add5d:/install.sh?ds=inline diff --git a/install.sh b/install.sh index c1d36d6..ad61036 100755 --- a/install.sh +++ b/install.sh @@ -1,41 +1,114 @@ #!/bin/bash -LIBRARY_VERSION=`cat library/setup.py | grep version | awk -F"'" '{print $2}'` -LIBRARY_NAME=`cat library/setup.py | grep name | awk -F"'" '{print $2}'` CONFIG=/boot/config.txt DATESTAMP=`date "+%Y-%M-%d-%H-%M-%S"` - -printf "$LIBRARY_NAME $LIBRARY_VERSION Python Library: Installer\n\n" +CONFIG_BACKUP=false +APT_HAS_UPDATED=false if [ $(id -u) -ne 0 ]; then printf "Script must be run as root. Try 'sudo ./install.sh'\n" exit 1 fi +function do_config_backup { + if [ ! $CONFIG_BACKUP == true ]; then + CONFIG_BACKUP=true + FILENAME="/boot/config.preinstall-$LIBRARY_NAME-$DATESTAMP.txt" + printf "Backing up $CONFIG to $FILENAME\n" + cp $CONFIG $FILENAME + fi +} + +function apt_pkg_install { + PACKAGES=() + PACKAGES_IN=("$@") + for ((i = 0; i < ${#PACKAGES_IN[@]}; i++)); do + PACKAGE="${PACKAGES_IN[$i]}" + printf "Checking for $PACKAGE\n" + dpkg -L $PACKAGE > /dev/null 2>&1 + if [ "$?" == "1" ]; then + PACKAGES+=("$PACKAGE") + fi + done + PACKAGES="${PACKAGES[@]}" + if ! [ "$PACKAGES" == "" ]; then + echo "Installing missing packages: $PACKAGES" + if [ ! $APT_HAS_UPDATED ]; then + apt update + APT_HAS_UPDATED=true + fi + apt install -y $PACKAGES + fi +} + +apt_pkg_install python-configparser + +CONFIG_VARS=`python - <> $CONFIG -fi +for ((i = 0; i < ${#SETUP_CMDS[@]}; i++)); do + CMD="${SETUP_CMDS[$i]}" + # Attempt to catch anything that touches /boot/config.txt and trigger a backup + if [[ "$CMD" == *"raspi-config"* ]] || [[ "$CMD" == *"$CONFIG"* ]] || [[ "$CMD" == *"\$CONFIG"* ]]; then + do_config_backup + fi + eval $CMD +done + +for ((i = 0; i < ${#CONFIG_TXT[@]}; i++)); do + CONFIG_LINE="${CONFIG_TXT[$i]}" + if ! [ "$CONFIG_LINE" == "" ]; then + do_config_backup + printf "Adding $CONFIG_LINE to $CONFIG\n" + sed -i "s/^#$CONFIG_LINE/$CONFIG_LINE/" $CONFIG + if ! grep -q "^$CONFIG_LINE" $CONFIG; then + printf "$CONFIG_LINE\n" >> $CONFIG + fi + fi +done printf "Done!\n"