Skip to content

Instantly share code, notes, and snippets.

@raffitz
Last active December 4, 2016 19:54
Show Gist options
  • Save raffitz/214aa80d19d8a8964e4a5d3908fcfab0 to your computer and use it in GitHub Desktop.
Save raffitz/214aa80d19d8a8964e4a5d3908fcfab0 to your computer and use it in GitHub Desktop.
Minimalist, potentially dangerous, KSP mod managing script.
#!/usr/bin/env bash
# Kerbal Mods Shell Script
#
# by raffitz
# Default Vatiable Declaration:
DEFKMSSGAMEFOLDER="${HOME}/.local/share/Steam/steamapps/common/Kerbal Space Program"
DEFKMSSFOLDER="${HOME}/Documents/KMSS"
# Folders:
KMSSGAMEFOLDER="${KMSSGAMEFOLDER:-${DEFKMSSGAMEFOLDER}}"
KMSSFOLDER="${KMSSFOLDER:-${DEFKMSSFOLDER}}"
# Default values can be overriden by setting the environment variables before the script execution
# Check whether folders exist
if [[ ! -d "$KMSSGAMEFOLDER" ]]; then
echo "No game folder present! Reconfigure script!"
exit 1
else
if [[ ! -d "$KMSSGAMEFOLDER/GameData" ]]; then
echo "No GameData folder present! Reconfigure script!"
exit 1
elif [[ ! -d "$KMSSGAMEFOLDER/saves" ]]; then
mkdir -p "$KMSSGAMEFOLDER/saves"
fi
fi
if [[ ! -d "$KMSSFOLDER" ]]; then
echo "No KMSS folder present. It will be created."
mkdir -p "$KMSSFOLDER"
fi
if [[ ! -d "$KMSSFOLDER/Mods" ]]; then
echo "No KMSS Mods folder present. It will be created."
mkdir -p "$KMSSFOLDER/Mods"
fi
if [[ ! -d "$KMSSFOLDER/Backups" ]]; then
echo "No KMSS Backups folder present. It will be created."
mkdir -p "$KMSSFOLDER/Backups"
fi
if [[ ! -d "$KMSSFOLDER/Saves" ]]; then
echo "No KMSS Saves folder present. It will be created."
mkdir -p "$KMSSFOLDER/Saves"
fi
# Check validity of command:
function chckcmd(){
if [[ "$1" = "clear" || \
"$1" = "add" || \
"$1" = "rmv" || \
"$1" = "toggle" || \
"$1" = "bak" || \
"$1" = "rst" || \
"$1" = "baksav" || \
"$1" = "rstsav" || \
"$1" = "quit"
]]; then
return 0
else
return 1
fi
}
KMSSCOMMAND=$1
until chckcmd "$KMSSCOMMAND"; do
printf "Kerbal Mods Shell Script\n\nby raffitz\n"
printf "\n"
printf "\tclear\tremove all mods\n"
printf "\tadd\tadd mods\n"
printf "\trmv\tremove mods\n"
printf "\ttoggle\ttoggle mods on and off\n"
printf "\n"
printf "\tbak\tbackup mods\n"
printf "\trst\trestore mods\n"
printf "\n"
printf "\tbaksav\tbackup saves\n"
printf "\trstsav\trestore saves\n"
printf "\n"
printf "\tquit\tdo nothing and quit program\n"
printf "\n"
read -p ">" -r
KMSSCOMMAND=$REPLY
done
# Run command:
case "$KMSSCOMMAND" in
clear)
read -p "Are you sure you want to clear all mods?" -r
if [[ $REPLY =~ ^[Yy]$ ]];then
cd "$KMSSGAMEFOLDER/GameData"
files=$(ls -1)
echo "${files/Squad/}" | grep . | while read line ; do rm "$line" ; done
else
exit 1
fi
;;
add)
printf "Available mods (in folder $KMSSFOLDER/Mods):\n"
cd "$KMSSFOLDER/Mods"
ls
printf "Write the mod names above, one at a time, to add them.\nGibberish will exit\n"
read -p ">" -r
until [[ ! -d "$REPLY" ]]; do
cp -r "$REPLY"/* "$KMSSGAMEFOLDER/GameData"
read -p ">" -r
done
;;
rmv)
KMSSGD="$KMSSGAMEFOLDER/GameData"
printf "Available mods (in folder $KMSSFOLDER/Mods):\n"
cd "$KMSSFOLDER/Mods"
ls
printf "Write the mod names above, one at a time, to remove them.\nIf mod isn't installed, it won't be removed (evidently).\nGibberish will exit\n"
read -p ">" -r
until [[ ! -d "$REPLY" ]]; do
cd $REPLY
find . -type f | while read line ; do rm "${line/#./$KMSSGD}" ; done
find . -type d | tail -n +2 | tac | while read line ; do rmdir --ignore-fail-on-non-empty "${line/#./$KMSSGD}" ; done
read -p ">" -r
done
;;
toggle)
if [[ -d "$KMSSFOLDER/Backups/Toggle" ]]; then
echo "Restoring mods"
cd "$KMSSFOLDER/Backups/Toggle"
mv ./* "$KMSSGAMEFOLDER/GameData"
cd ..
rmdir "$KMSSFOLDER/Backups/Toggle"
else
echo "Storing mods"
mkdir -p "$KMSSFOLDER/Backups/Toggle"
cd "$KMSSGAMEFOLDER/GameData"
files=$(ls -1)
echo "${files/Squad/}" | grep . | while read line ; do mv "$line" "$KMSSFOLDER/Backups/Toggle"; done
fi
;;
bak)
KMSSBUNAME="$(date +%Y%m%d%H%M)"
printf "Mods will be backed up.\nBackup name [$KMSSBUNAME] :"
read -r
KMSSBUNAME="${REPLY:-$KMSSBUNAME}"
mkdir -p "$KMSSFOLDER/Backups/$KMSSBUNAME"
cd "$KMSSGAMEFOLDER/GameData"
files=$(ls -1)
echo "${files/Squad/}" | grep . | while read line ; do cp -r "$line" "$KMSSFOLDER/Backups/$KMSSBUNAME"; done
;;
rst)
printf "Available Backups (in folder $KMSSFOLDER/Backups):\n"
cd "$KMSSFOLDER/Backups"
ls
printf "Write the backup name to restore it.\n"
until [[ -d "$REPLY" ]]; do
read -p ">" -r
done
cp -r "$REPLY"/* "$KMSSGAMEFOLDER/GameData"
;;
baksav)
KMSSBUNAME="$(date +%Y%m%d%H%M)"
printf "Saves will be backed up.\nBackup name [$KMSSBUNAME] :"
read -r
KMSSBUNAME="${REPLY:-$KMSSBUNAME}"
cd "$KMSSGAMEFOLDER"
cp -r saves "$KMSSFOLDER/Saves/$KMSSBUNAME"
;;
rstsav)
printf "Available savegame backups (in folder $KMSSFOLDER/Saves):\n"
cd "$KMSSFOLDER/Saves"
ls
printf "Write the backup name to restore it.\n"
until [[ -d "$REPLY" ]]; do
read -p ">" -r
done
read -p "Are you sure you want to override current savegames?" -r
if [[ $REPLY =~ ^[Yy]$ ]];then
rm -rf "$KMSSGAMEFOLDER/saves"
cp -r "$KMSSFOLDER/Saves/$REPLY" "$KMSSGAMEFOLDER/saves"
else
exit 1
fi
;;
quit)
;;
*)
echo "Critical error: Unrecognised command!"
exit 1
;;
esac
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment