diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2022-05-24 09:35:55 +0300 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2022-05-24 09:35:55 +0300 |
| commit | 2438e727fee8bf154ab229057923d1c974ade180 (patch) | |
| tree | 12581ef208a85d78d22c19c4e5c211a999978ad0 /stupid.sh | |
| parent | d003662f4e1e12d012e9f4a6dec23641ac5e4beb (diff) | |
| download | stupid-2438e727fee8bf154ab229057923d1c974ade180.tar stupid-2438e727fee8bf154ab229057923d1c974ade180.tar.gz stupid-2438e727fee8bf154ab229057923d1c974ade180.zip | |
Reworked structure and fully implemented install and remove functionality
Diffstat (limited to 'stupid.sh')
| -rwxr-xr-x | stupid.sh | 115 |
1 files changed, 69 insertions, 46 deletions
@@ -1,66 +1,89 @@ #!/bin/bash -quietInstall='no' +QUIET_INSTALL='no' +STUPID_DIR=$(pwd) -function installPackages { - shift - while [ ! -z $1 ]; do - pkg=$(find . -type f -name "$1--*.stupid" | tail -n 1 || grep -q '--' && find . -type f -name "$1.stupid") - if [ -z $pkg ]; then - echo "[$1]<Error> No package file!"; shift; continue - fi - ( - source "$pkg" - if [[ $(type -t stupidInstall) != "function" ]]; then - echo "[$1]<Error> No install function found!"; exit - fi - if [[ $(type -t stupidRemove) != "function" ]]; then - echo "[$1]<Error> No remove function found (required even when installing)!"; exit - fi +function installRoutine { + echo "[$1]<Info> Running checks for $1 ..." - echo "[$1]<Info > Installing $title $version ..." + if grep -q "$1" 'installed.stupid-log'; then + echo "| <Warn > Package has already been installed! It will be reinstalled!" + read -p "L <Warn > Do you want to continue? [y/N]: " confirm && ! [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] && exit - mkdir -p "/tmp/stupid/$1"; cd "/tmp/stupid/$1" - touch "$pkg.install-log" - stupidInstall >"$pkg.install-log" 2>&1 - echo "FINISHED" >> "$pkg.install-log" + removeRoutine $1 + elif grep -q "$name" 'installed.stupid-log'; then + otherPkg=$(grep "$name--*" 'installed.stupid-log') + echo "| <Warn > Another version of the package ($otherPkg) has been found! It will be removed and $1 will be installed!" + read -p "L <Warn > Do you want to continue? [y/N]: " confirm && ! [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] && exit - echo "[$1]<Info > Installed!" - ) - if (( $? != 0 )); then - echo "[$1]<Error> Could not install! Log:" - cat "/tmp/stupid/$1/$pkg.install-log" - fi - shift - done + removeRoutine $otherPkg + fi + + echo "[$1]<Info> 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 <Info> Installed!" } -function removePackages { - shift +function installFail { + echo "| <Error> Could not install! Log:" + cat "/tmp/stupid/$name/$1.install-log" +} + +function removeRoutine { + echo "[$1]<Info> Running checks for $1 ..." + + if ! grep -q "$1" 'installed.stupid-log'; then + echo "L <Warning> Package isn't installed!" && exit + fi + + echo "[$1]<Info> 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 <Info> Removed!" +} + +function removeFail { + echo "[$1]<Error> 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]<Error> 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]<Error> Package file name and package name and/or version do not match!"; exit + fi + if [[ $(type -t stupidInstall) != "function" ]]; then + echo "[$1]<Error> No install function found!"; exit + fi if [[ $(type -t stupidRemove) != "function" ]]; then echo "[$1]<Error> No remove function found!"; exit fi - echo "[$1]<Info > Removing $title $version ..." - - mkdir -p "/tmp/stupid/$1"; cd "/tmp/stupid/$1" - touch "$pkg.remove-log" - stupidRemove >"$pkg.remove-log" 2>&1 - echo "FINISHED" >> "$pkg.remove-log" - - echo "[$1]<Info > Installed!" + $routine "$name--$version" ) - if (( $? != 0 )); then - echo "[$1]<Error> Could not remove! Log:" - cat "/tmp/stupid/$1/$pkg.remove-log" - fi shift done } @@ -68,14 +91,14 @@ function removePackages { while [ ! -z $1 ]; do case $1 in -q|quiet) - quietInstall='yes' + QUIET_INSTALL='yes' ;; -i|install) - installPackages $@ + handlePackages installRoutine $@ exit ;; -r|remove) - removePackages $@ + handlePackages removeRoutine $@ exit ;; *) |
