#!/bin/bash QUIET_INSTALL='no' STUPID_DIR=$(pwd) function installRoutine { echo "[$1] Running checks for $1 ..." if grep -q "$1" 'installed.stupid-log'; then echo "| Package has already been installed! It will be reinstalled!" read -p "L Do you want to continue? [y/N]: " confirm && ! [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] && exit removeRoutine $1 elif grep -q "$name" 'installed.stupid-log'; then otherPkg=$(grep "$name--*" 'installed.stupid-log') echo "| Another version of the package ($otherPkg) has been found! It will be removed and $1 will be installed!" read -p "L Do you want to continue? [y/N]: " confirm && ! [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] && exit removeRoutine $otherPkg fi echo "[$1] Installing $1 ..." mkdir -p "/tmp/stupid/$name" && cd "/tmp/stupid/$name"; touch "$1.install-log" stupidInstall >"$1.install-log" 2>&1 || (installFail $1 && exit) echo "FINISHED" >> "$1.install-log"; echo "$1" >> "$STUPID_DIR/installed.stupid-log" echo "L Installed!" } function installFail { echo "| Could not install! Log:" cat "/tmp/stupid/$name/$1.install-log" } function removeRoutine { echo "[$1] Running checks for $1 ..." if ! grep -q "$1" 'installed.stupid-log'; then echo "L Package isn't installed!" && exit fi echo "[$1] Removing $1 ..." mkdir -p "/tmp/stupid/$name" && cd "/tmp/stupid/$name"; touch "$1.remove-log" stupidRemove >"$1.remove-log" 2>&1 || (removeFail $1 && exit) grep -qv "$1" "$STUPID_DIR/installed.stupid-log" > "$STUPID_DIR/installed.stupid-log" echo "L Removed!" } function removeFail { echo "[$1] Could not remove! Log:" cat "/tmp/stupid/$name/$1.remove-log" } function handlePackages { routine=$1 shift; shift while [ ! -z $1 ]; do echo '' # Package names can either be "PACKAGENAME" or "PACKAGENAME--VERSION" # If it's "PACKAGENAME" we only want the last occurence, since alphanumerically this should be the latest version pkg=$(find . -type f -name "$1--*.stupid" | tail -n 1 || grep -q '--' && find . -type f -name "$1.stupid") if [ -z $pkg ]; then echo "[$1] No package file!"; shift; continue fi # Ran in subshell so the source-ing doesn't persist between routine functions of multiple packages ( source "$pkg" if [[ "$1--$version" != "$name--$version" ]] && [[ "$1" != "$name--$version" ]]; then echo "[$1] Package file name and package name and/or version do not match!"; exit fi if [[ $(type -t stupidInstall) != "function" ]]; then echo "[$1] No install function found!"; exit fi if [[ $(type -t stupidRemove) != "function" ]]; then echo "[$1] No remove function found!"; exit fi $routine "$name--$version" ) shift done } while [ ! -z $1 ]; do case $1 in -q|quiet) QUIET_INSTALL='yes' ;; -i|install) handlePackages installRoutine $@ exit ;; -r|remove) handlePackages removeRoutine $@ exit ;; *) echo '[Error] Invalid command!' ;; esac shift done