diff --git a/modules/add-ppa.sh b/modules/add-ppa.sh new file mode 100755 index 0000000..1b8c2c3 --- /dev/null +++ b/modules/add-ppa.sh @@ -0,0 +1,117 @@ +#!/bin/bash +add-ppa () { + +if [ $EUID != 0 ]; then + sudo bash -c "$(declare -f add-ppa); add-ppa $*" + else + VERSION=jammy + KEYSDIR=/etc/apt/trusted.gpg.d + KEYSERVER=keyserver.ubuntu.com + + declare OPTION; declare OPTARG; declare OPTIND + while getopts 'v:p:s:k:c:d:oi' OPTION; do + echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND} + case "$OPTION" in + i) + INSTALL=true + ;; + v) + VERSION=$OPTARG + ;; + p) + PACKAGE=$OPTARG + ;; + c) + CMD=$OPTARG + ;; + d) + KEYSDIR=$OPTARG + ;; + s) + KEYSERVER=$OPTARG + ;; + o) + # overwrite any exising public key + KEYOVERWRITE=true + ;; + *) echo unknown run option -$OPTARG + echo "USAGE: add-ppa package/branch (e.g. git-core/ppa)" + echo "available options -v ; -p " + ;; + esac + done + + shift $((OPTIND - 1)) + + #check input + if [ -z ${1+x} ]; then + echo "No ppa provided!" + return 1 + fi + + LAUNCHPAD="https://ppa.launchpadcontent.net" + DEV=$(echo $1 | cut -d ':' -f 2 | cut -d '/' -f1 ) + PACKAGE=${PACKAGE:-$DEV} + CMD=${CMD:-$PACKAGE} + BRANCH=${2:-$(echo $1| cut -d '/' -f 2)} + URL="$LAUNCHPAD/$DEV/$BRANCH/ubuntu $VERSION main" + + echo "*********** Adding PPA Repository ************" + echo DEVELOPER: $DEV + echo BRANCH: $BRANCH + echo PACKAGE: $PACKAGE + echo COMMAND: $CMD + echo URL: $URL + + if [[ -t 0 ]]; then + read -n 1 -p "do you want to continue [y]=>" REPLY + [[ $REPLY != "y" ]] && return 0 + fi + + echo -e "\n*********************************************" + #create source list file + echo "deb $URL" > /etc/apt/sources.list.d/$DEV.list + echo "***** added /etc/apt/sources.list.d/$DEV.list with****" + cat /etc/apt/sources.list.d/$DEV.list + echo "*********************************************" + + KEYFILE=$KEYSDIR/$DEV.gpg + + [[ $KEYOVERWRITE ]] && rm $KEYFILE + if [ ! -f $KEYFILE ]; then + # using an update error to grab key id + KEY_ERROR=/tmp/${DEV}_key_error + touch $KEY_ERROR + apt-get update > /dev/null 2> $KEY_ERROR + cat $KEY_ERROR + KEY=$(sed -n 's/^.*NO_PUBKEY //p' "$KEY_ERROR" | head -1) + # echo Reposity Public Key Settings + # echo KEYS DIRECTORY: $KEYSDIR + # echo KEY SERVER: $KEYSERVER + # echo KEY: $KEY + if [ ! $KEY ]; then + echo can not determine $DEV/$BRANCH key sign + echo "removing file: /etc/apt/sources.list.d/$DEV.list and aborting" + rm /etc/apt/sources.list.d/$DEV.list + return 1 + fi + echo downloading and saving public key $KEY for $DEV/$BRANCH to $KEYFILE + gpg --keyserver $KEYSERVER --recv $KEY + gpg --export $KEY > $KEYFILE + else + echo " >>>>>> $KEYFILE already exists, using that key $KEY <<<<<" + fi + echo ppa repo $DEV/$BRANCH for package $PACKAGE now registered, updating... + apt-get update 1> /dev/null + if [[ $INSTALL ]]; then + echo installing $PACKAGE + [[ -t 0 ]] && apt policy $PACKAGE + apt-get install $PACKAGE -y + $CMD --version + fi +fi + +} + +# # if script was executed then call the function +(return 0 2>/dev/null) || add-ppa $@ \ No newline at end of file