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.
		
		
		
		
		
			
		
			
				
	
	
		
			96 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Bash
		
	
			
		
		
	
	
			96 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Bash
		
	
#!/bin/bash
 | 
						|
 | 
						|
#$1 is a quotation-marked, space-seperated list of package names.
 | 
						|
#$2 is the path to the program folder being installed.
 | 
						|
#Example usage: ~/pi-apps/pkg-install "gparted buffer expect" ~/pi-apps/apps/Arduino
 | 
						|
 | 
						|
PKG_LIST="$1"
 | 
						|
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..."
 | 
						|
 | 
						|
if [ -z "$PKG_LIST" ];then
 | 
						|
  echo -e "\e[91mNo packages were specified!\e[39m"
 | 
						|
  exit=yes
 | 
						|
fi
 | 
						|
 | 
						|
if [ -z "$PRG" ];then
 | 
						|
  echo -e "\e[91mNo program directory specified!\e[39m"
 | 
						|
  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"
 | 
						|
fi
 | 
						|
 | 
						|
if [ ! -z $exitcode ];then
 | 
						|
  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
 | 
						|
 | 
						|
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
 | 
						|
 | 
						|
#save that list of installed packages in the program directory for future removal
 | 
						|
echo "$INSTALL_LIST" >> "${DIRECTORY}/data/installed-packages/${PRG}"
 | 
						|
 | 
						|
if [ ! -z "$INSTALL_LIST" ];then
 | 
						|
  echo -e "These packages will be installed: \e[2m$INSTALL_LIST\e[22m"
 | 
						|
  output="$(sudo apt-get install -y $INSTALL_LIST 2>&1)"
 | 
						|
  exitcode=$?
 | 
						|
  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
 | 
						|
  
 | 
						|
  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"
 | 
						|
fi
 |