Jump to content

A dumb-simple mod installation bash script


Psycho_zs

Recommended Posts

Hi everyone!

Just wanted to share a script that might be useful for those who play KSP with mods on Linux without using CKAN.

It automates boring things like removing old files, copying over new files, setting permissions (matches GameData owner and group, handy for root-protected installs).

Takes files and dirs as arguments, nothing fancy.

Edit path on 8th line before use. Read report before answering "yes". Anything before positive answer is safe.


#!/bin/bash
# kspmodinstall
# dumb KSP mod installer by Psycho_zs
# run as:
# kspmodinstall stuff

# path to KSP GameData dir
KSPDATA="/path/to/your/KSP/GameData"

[ -z "$@" ] && { echo "No arguments given. Give me something to install!" >&2 ; exit 1 ; }
[ ! -d "$KSPDATA" ] && { echo "GameData dir \"$KSPDATA\" does not exist!" >&2 ; exit 1 ; }
[ ! -w "$KSPDATA" ] && { echo "GameData dir \"$KSPDATA\" is not writeable!" >&2 ; exit 1 ; }

# get owner and group of GameData
GD_OWNER="$(stat -c %U "$KSPDATA")"
GD_GROUP="$(stat -c %G "$KSPDATA")"

FAIL=0
# check existence of data to be installed
echo "Checking what you've given..."
for OBJECT in "$@"
do
# canonicalize path to avoid bad stuff with relative locations
OBJECT="$(readlink -f "$OBJECT")"

if [ -e "$OBJECT" ]
then
if [ -e "${KSPDATA}/$(basename ${OBJECT})" ]
then
echo -e "Will remove old object:\n\t\"${KSPDATA}/$(basename ${OBJECT})\"\n"
fi
echo -e "Will copy \"$(basename ${OBJECT})\" to:\n\t\"${KSPDATA}/$(basename ${OBJECT})\"\n"
else
echo "\"$OBJECT\" does not exist!" >&2
FAIL=1
fi
done

[ "$FAIL" = "1" ] && { echo "Some objects you've given were not found. Aborting." >&2 ; exit 1 ; }

CONTINUE=0
until [ "$CONTINUE" = "1" ]
do
echo -ne "Continue with installing?\n(y/n) > "
read ANSWER

if echo "$ANSWER" | grep -qi '^[[:space:]]*y[[:space:]]*$\|^[[:space:]]*yes[[:space:]]*$'
then
echo "Proceeding..."
CONTINUE=1
elif echo "$ANSWER" | grep -qi '^[[:space:]]*n[[:space:]]*$\|^[[:space:]]*no[[:space:]]*$'
then
echo "Aborting."
exit 0
else
echo "Not sure what you've meant by \"$ANSWER\", try again!"
fi
done

for OBJECT in "$@"
do
# canonicalize path to avoid bad stuff with relative locations
OBJECT="$(readlink -f "$OBJECT")"

# remove
if [ -e "${KSPDATA}/$(basename ${OBJECT})" ]
then
rm -r "${KSPDATA}/$(basename ${OBJECT})" && echo "Removed: \"${KSPDATA}/$(basename ${OBJECT})\"" || echo "Failed to remove \"${KSPDATA}/$(basename ${OBJECT})\"!" >&2
fi

# copy
cp -r --preserve=mode,timestamps "$OBJECT" -t "${KSPDATA}" && echo -e "Copied \"$(basename ${OBJECT})\" to:\n\t\"${KSPDATA}/$(basename ${OBJECT})\"" || echo "Failed to copy \"$(basename ${OBJECT})\"!" >&2
# set GameData ownership to newly copied files
chown -R "$GD_OWNER":"$GD_GROUP" "${KSPDATA}/$(basename ${OBJECT})" || echo "Failed to set ownership \"${GD_OWNER}:${GD_GROUP}\" on \"${KSPDATA}/$(basename ${OBJECT})\"!" >&2
done

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...