#! /bin/bash # Script to help manage PPA in Ubuntu and derivatives. # Written by vankasteelj # Licence GNU-GPL 3.0 # https://www.gnu.org/licenses/gpl-3.0.html # Special Thanks : Wilf, Blade19899, stwissel, Tartampion91, lynn, metalux, 200_success # This script uses 'ppa-purge' by Lorenzo De Liso, it will be installed if needed. # Version number is X.X.X (published version/feature added/minor revision) #Script SCRIPT="ppa-tool" VERSION="2.1.0" DATE="2014-03-29" COLORNORMAL="\\033[0;39m" COLORERROR="\\033[1;31m" COLOROK="\\033[0;32m" COLORREADABLE="\\033[0;36m" COLORITA="\\033[3;39m" [ -n "${1#ppa:}" ] && PPA=$1 [ -n "${2#ppa:}" ] && PPA=$2 BKPLIST="`echo $HOME`/BACKUP-`(date +%Y.%m.%d-%Hh%M)`.$SCRIPT" RELEASE="$(lsb_release -si) $(lsb_release -sr)" CURRENTRELEASE="$(lsb_release -sc)" CUSTOMRELEASE="${1#--chk-release=}" helpsection() { echo -e "Usage : $SCRIPT [OPTION]... [PPA]... Handles the PPA's on the system. [PPA] : ppa:user/directory Example : "$COLORREADABLE"$SCRIPT --check ppa:libreoffice/ppa"$COLORNORMAL" If [OPTION] is ommited, only the compatibility '--check' for [PPA] will be checked. "$COLOROK"Available options:"$COLORNORMAL" -h, --help Print this help -c, --check Check the [PPA] compatibility with the system -a, --add Add [PPA] to repositories (implies -c, --check) -r, --remove Remove [PPA] from repositories -p, --purge Purge [PPA] and all installed packages coming from there -l, --list Get a list of all installed PPA's -v, --version Print the script's version number --update Update '$SCRIPT' to the most recent version "$COLOROK"Advanced options: BACKUP"$COLORNORMAL" --bkp-list Make a backup of all installed PPA's in HOME --bkp-install Restore PPA from [FILE] Usage : $SCRIPT [BKP-OPTION]... [FILE]... Example : "$COLORREADABLE"$SCRIPT --bkp-install /home/user/BACKUP.$SCRIPT"$COLORNORMAL" "$COLOROK"Advanced options: CHECK"$COLORNORMAL" --chk-release=[VERSION] Check the installed PPA's for an upgrade to [VERSION] Example : "$COLORREADABLE"$SCRIPT --chk-release=trusty"$COLORNORMAL" => All installed PPA's will be checked. --chk-release=[VERSION]... [PPA]... Check [PPA] compatibility with [VERSION] Example : "$COLORREADABLE"$SCRIPT --chk-release=trusty ppa:libreoffice/ppa"$COLORNORMAL" => A fake check : is [PPA] compatible with [VERSION] ? "$COLORITA"NOTE: multiples PPA's aren't implemented."$COLORNORMAL" -------- Uninstallation: « "$COLORREADABLE"sudo rm /usr/bin/$SCRIPT "$COLORNORMAL"» Version $VERSION, $DATE - Script written by vankasteelj." exit 0 } ## FUNCTIONS ## error() { case "$1" in connection) local errortype="E : Unable to join the server. Please check your Internet connection." ;; root) local errortype="E : Unable to perform as 'su' or 'root'." ;; syntax) local errortype="E : Syntax error." ;; option) local errortype="E : Unknown option." ;; ignored) echo -e "$SCRIPT : ""$COLORERROR""$2""$COLORNORMAL"" ignored, '--list' and '--bkp-list' don't accept arguments\n" return 1 ;; toomany) local errortype="E : Too many options." esac echo -e "$SCRIPT - "$COLORERROR"Oops, something went wrong."$COLORNORMAL" $errortype Usage: $SCRIPT [OPTION]... [PPA]... Try « "$COLOROK"$SCRIPT --help"$COLORNORMAL" » for more information." exit 0 } success() { case "$1" in removed) local message="'$PPA' removed." ;; added) local message="'$PPA' added." ;; esac echo -e "... OK ! $message" } update() { local url="https://raw.githubusercontent.com/vankasteelj/ppa-tool/master/dist-install" wget "$url" -q -O /tmp/"$SCRIPT"-update case $? in 0) bash /tmp/"$SCRIPT"-update ;; *) error connection ;; esac rm /tmp/"$SCRIPT"-update exit 0 } ppa_verification() { local ppa="${PPA#ppa:}" local codename="$(lsb_release -sc)" local release="$RELEASE" [ -n "$1" ] && local codename="${1#--chk-release=}" && local release="$codename" local url="http://ppa.launchpad.net/$ppa/ubuntu/dists/$codename/" wget "$url" -q -O /dev/null case $? in 0) # Success echo "$SCRIPT : '$ppa' is available for $release" && INSTALLABLE=1 ;; 8) # HTTP 404 (Not Found) would result in wget returning 8 echo "$SCRIPT : '$ppa' is NOT available for $release" ;; *) error connection ;; esac } ppa_add() { [ "$INSTALLABLE" != "1" ] && exit 0 echo -e "\n- Installing..." && sudo add-apt-repository $PPA -y echo -e "\n- Updating package list..." && sudo apt-get update > /dev/null 2>&1 && success added exit 0 } ppa_remove() { echo -e "\n- Uninstalling..." && sudo add-apt-repository -r $PPA -y > /dev/null 2>&1 echo -e "\n- Updating package list..." && sudo apt-get update > /dev/null 2>&1 && success removed exit 0 } ppa_purge() { case $1 in purge) read -p "Do you want to purge '$PPA' and uninstall all packages installed from it - All critical packages required by the system will be automatically reinstalled from the official repositories (y/n)? : " [ "$REPLY" != "y" ] && exit 0 sudo ppa-purge "$PPA" && sudo apt-get autoremove -y ;; needed) echo -e "$COLORERROR""Error: ""$COLORNORMAL""'ppa-purge' wasn't found.\nThis package is required for the '--purge' feature\n" read -p "Do you wish to install 'ppa-purge' (y/n)? : " [ "$REPLY" != "y" ] && exit 0 echo -e "\n- Installing..." && sudo apt-get install ppa-purge -y > /dev/null 2>&1 && success && "$SCRIPT $OPTIONS" ;; esac exit 0 } ppa_list() { [ "$1" == "save" ] && [ "$USER" == "root" ] && error root #do not save files in /root directory echo "### BACKUP '$SCRIPT' ###" | tee -a /tmp/"$SCRIPT"-list.tmp > /dev/null 2>&1 for APT in `find /etc/apt/ -name \*.list`; do grep -o "^deb http://ppa.launchpad.net/[a-z0-9\-]\+/[a-z0-9\-]\+" $APT | while read ENTRY ; do PPAOWNER=`echo $ENTRY | cut -d/ -f4` PPANAME=`echo $ENTRY | cut -d/ -f5` echo "ppa:$PPAOWNER/$PPANAME" | tee -a /tmp/"$SCRIPT"-list.tmp done done [ "$1" == "save" ] && cp /tmp/"$SCRIPT"-list.tmp "$BKPLIST" && echo -e "\nFile '"$COLOROK"$BKPLIST"$COLORNORMAL"' saved." rm /tmp/"$SCRIPT"-list.tmp exit 0 } backup_install() { local bkplogs="$(date +%d/%m/%Y)-$(date +%H:%M)" local bkpfile="$1" echo -e "$SCRIPT : Installing new repositories from backup file '$bkpfile' This may take a while... Please wait." while read LINE ; do if [[ -n "`echo $LINE | grep 'ppa:'`" ]] ; then local ppa="${LINE#ppa:}" local codename="$(lsb_release -sc)" local release="$RELEASE" local url="http://ppa.launchpad.net/$ppa/ubuntu/dists/$codename/" wget "$url" -q -O /dev/null case $? in 0) # Success echo -e "$COLOROK" && echo -e "- ### INSTALLATION : '$LINE' ###" | tee -a /tmp/"$SCRIPT"-backup.log && echo -e "$COLORNORMAL" sudo add-apt-repository -y $LINE | tee -a /tmp/"$SCRIPT"-backup.log ;; 8) # HTTP 404 (Not Found) would result in wget returning 8 echo -e "$COLORERROR" && echo -e "- ### !!! WARNING !!! '$LINE' unavailable for $RELEASE : installation aborted. ###" | tee -a /tmp/$SCRIPT-backup.log && echo -e "$COLORNORMAL" ;; *) error connection ;; esac fi done < $bkpfile echo -e "- ### UPDATING package-list ###" | tee -a /tmp/"$SCRIPT"-backup.log > /dev/null 2>&1 echo -e "\n- Updating package list..." && sudo apt-get update | tee -a /tmp/"$SCRIPT"-backup.log > /dev/null 2>&1 echo -e "\n... OK ! Repositories reinstalled." read -p "Read the log file (y/n)? " [ "$REPLY" != "y" ] && exit 0 echo -e "\n-----\n"$COLOROK"Logs : $bkplogs"$COLORNORMAL"" && xdg-open /tmp/"$SCRIPT"-backup.log exit 0 } custom_full_verification() { "$SCRIPT" --list > /tmp/"$SCRIPT"-check_release.tmp echo -e "$SCRIPT: Checking repositories for an entire system-upgrade.\n\nCurrent: $CURRENTRELEASE\nWanted : $CUSTOMRELEASE\n\n- Checking...\n" > /tmp/"$SCRIPT"-unwanted.tmp > /dev/null 2>&1 #empty temp file while read LINE ; do local ppa="${LINE#ppa:}" local codename="$CUSTOMRELEASE" local url="http://ppa.launchpad.net/$ppa/ubuntu/dists/$codename/" wget "$url" -q -O /dev/null case $? in 0) # Success echo -e "$LINE\n"$COLOROK"=> compatible $codename"$COLORNORMAL"" ;; 8) # HTTP 404 (Not Found) would result in wget returning 8 echo -e "$LINE\n"$COLORERROR"=> incompatible $codename"$COLORNORMAL"" echo $LINE | tee -a /tmp/"$SCRIPT-unwanted".tmp > /dev/null 2>&1 #save a list of incompatible ppa's ;; *) error connection ;; esac done < /tmp/"$SCRIPT"-check_release.tmp echo "" if [[ -n "`cat /tmp/$SCRIPT-unwanted.tmp`" ]] ; then #check the list of incompatible ppa's echo -e "In case of upgrade to '$CUSTOMRELEASE', the following PPA's will become unavailable :" cat /tmp/$SCRIPT-unwanted.tmp else echo -e "No '$codename'-incompatible repositories found : you can proceed to an upgrade." fi rm /tmp/$SCRIPT-unwanted.tmp > /dev/null 2>&1 ; rm /tmp/$SCRIPT-check_release.tmp > /dev/null 2>&1 echo -e "$COLORITA""\nNote: In case of upgrade, all PPA's will be desactivated. You will be able to reinstall the available PPA's from a backup file, using: "$COLORREADABLE"$SCRIPT --bkp-install [File]\n""$COLORNORMAL" read -p "- Make a backup (y/n)? " [ "$REPLY" == "y" ] && "$SCRIPT" --bkp-list } #check for too many options/arguments [ -n "$3" ] && error toomany case $1 in -h|--help) helpsection ;; -v|--version) echo "$SCRIPT : $VERSION ($DATE)" && exit 0 ;; -c|--check|check) [ -z "$2" ] || [ -z "${2#ppa:}" ] && error syntax ppa_verification && exit 0 ;; -a|--add|add) [ -z "$2" ] || [ -z "${2#ppa:}" ] && error syntax ppa_verification && ppa_add ;; -r|--remove|remove) [ -z "$2" ] || [ -z "${2#ppa:}" ] && error syntax ppa_remove ;; -p|--purge|purge) if [[ -n "`echo $2`" ]] ; then [ -n "`dpkg -l | grep ppa-purge`" ] && ppa_purge purge OPTIONS="$1 $2" ppa_purge needed #if ppa-purge isn't installed else error syntax fi exit 0 ;; -l|--list|list|--bkp-list) [ -n "$2" ] && error ignored "$2" [ "$1" == "--bkp-list" ] && ppa_list save ppa_list ;; --bkp-install) [ -z "$2" ] && error syntax backup_install "$2" ;; --chk-release=*) if [[ -n "`echo $2 | grep ppa`" ]] ; then #if ppa entered, check the ppa ppa_verification "$1" else #if no ppa entered, check entire system [ -n "$2" ] && error syntax custom_full_verification fi exit 0 ;; ppa:*) ppa_verification && exit 0 ;; --update) update ;; *) error option ;; esac error syntax