You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Botspot-Pi-Apps/pkg-install

96 lines
3.4 KiB
Plaintext

4 years ago
#!/bin/bash
#$1 is a quotation-marked, space-seperated list of package names.
4 years ago
#$2 is the path to the program folder being installed.
4 years ago
#Example usage: ~/pi-apps/pkg-install "gparted buffer expect" ~/pi-apps/apps/Arduino
PKG_LIST="$1"
4 years ago
PRG="$(echo "$2" | tr '/' '\n' | tail -1)"
DIRECTORY="$(readlink -f "$(dirname "$0")")"
function error {
echo -e "\e[91m$1\e[39m"
exit 1
}
echo "Running pkg-install..."
4 years ago
if [ -z "$PKG_LIST" ];then
echo -e "\e[91mNo packages were specified!\e[39m"
4 years ago
exit=yes
4 years ago
fi
4 years ago
if [ -z "$PRG" ];then
4 years ago
echo -e "\e[91mNo program directory specified!\e[39m"
4 years ago
exitcode=1
elif [ ! -d "$2" ];then
echo -e "\e[91m$2 does not exist!\e[39m"
exitcode=1
elif [ -z "$(echo "$2" | grep "pi-apps/apps")" ];then
echo -e "\e[33mWarning: That program directory ($PRG) is located outside of pi-apps.\e[39m"
4 years ago
fi
4 years ago
if [ ! -z $exitcode ];then
4 years ago
echo -e '$1 is a quotation-marked, space-seperated list of package names.\n$2 is the path to the program folder being installed.\nExample usage: ~/pi-apps/pkg-install '\"'gparted buffer expect'\"' ~/pi-apps/apps/Arduino'
echo -e "\e[91mExiting now.\e[39m"
exit 1
fi
output="$(sudo apt update 2>&1)"
exitcode=$?
#inform user packages are upgradeable
if [ ! -z "$(echo "$output" | grep 'packages can be upgraded' )" ];then
echo -e "\e[33mSome packages can be upgraded.\e[39m Please consider running \e[4msudo apt full-upgrade -y\e[0m."
fi
#exit on apt error
errors="$(echo "$output" | grep '^[(W)|(E)|(Err]:')"
if [ $exitcode != 0 ] || [ ! -z "$errors" ];then
echo -e "\e[91mFailed to run \e[4msudo apt update\e[0m\e[39m!"
echo -e "APT reported these errors:\n\e[91m$errors\e[39m"
exit 1
fi
INSTALL_LIST="$(sudo apt-get install --no-install-recommends --dry-run $PKG_LIST | sed -n '/The following NEW packages/,/to remove/p' | sed -e '2,$!d' -e '$d' | tr -d '*' | tr '\n' ' ')"
4 years ago
if [ ! -z "$INSTALL_LIST" ];then
#save that list of installed packages in the program directory for future removal
mkdir -p "${DIRECTORY}/data/installed-packages"
echo "$INSTALL_LIST" >> "${DIRECTORY}/data/installed-packages/${PRG}"
4 years ago
echo -e "These packages will be installed: \e[2m$INSTALL_LIST\e[22m"
output="$(sudo apt-get install -y --no-install-recommends $INSTALL_LIST 2>&1)"
4 years ago
exitcode=$?
echo "Output was: ${output}EOO"
4 years ago
errors="$(echo "$output" | grep '^[(W)|(E)|(Err]:')"
if [ $exitcode != 0 ] || [ ! -z "$errors" ];then
echo -e "\e[91mFailed to install the packages!\e[39m"
echo -e "APT reported these errors:\n\e[91m$errors\e[39m"
exit 1
fi
#re-check package list. This time it should be blank.
#INSTALL_LIST=''
#for i in $PKG_LIST
#do
# PKG_OK="$(dpkg-query -W --showformat='${Status}\n' "$i" 2>/dev/null | grep "install ok installed")"
# if [ "" == "$PKG_OK" ]; then
# INSTALL_LIST="${INSTALL_LIST} ${i}" #add package to install list
# fi
#done
INSTALL_LIST="$(sudo apt-get install --no-install-recommends --dry-run $PKG_LIST | sed -n '/The following packages/,/to remove/p' | sed -e '2,$!d' -e '$d' | tr -d '*' | tr '\n' ' ')"
4 years ago
if [ ! -z $INSTALL_LIST ];then
echo -e "\e[91mAPT did not exit with an error, but these packages failed to install somehow: $INSTALL_LIST\e[39m"
exit 1
else
echo -e "\e[32mAll packages were installed succesfully.\e[39m"
fi
else
echo -e "\e[32mNo new packages to install. Nothing to do!\e[39m"
echo '' >> "${DIRECTORY}/data/installed-packages/${PRG}"
4 years ago
fi