#!/bin/bash
## stuff
SCRIPT_DIR="scripts" # subdir to be installed
SRC_DIR="vaca_plus" # subdir to be installed
INST_DIR="vaca_plus" # what the subdir will be called in the EDuke32 dir
SYSTEM=`uname -s`
ARCH=`uname -a`
TMP="/tmp"
TOP_DIR=`pwd`
## constants
USER_PATH="~/.eduke32"
DUKE_PATH[]="/usr/local/games/eduke32"
DUKE_PATH[]="/usr/games/eduke32"
DUKE_PATH[]=$USER_PATH
UTIL_PATH=$TOP_DIR"/utils/"$SYSTEM"/"$ARCH
## check requirements
# check kextract
KEXTRACT=`whereis kextract`
if test "x$KEXTRACT" == "x"; then
KEXTRACT=$UTIL_PATH"/kextract"
fi
# check unpackssi
KEXTRACT=`whereis unpackssi`
if test "x$KEXTRACT" == "x"; then
KEXTRACT=$UTIL_PATH"/unpackssi"
fi
# check uips
KEXTRACT=`whereis uips`
if test "x$KEXTRACT" == "x"; then
KEXTRACT=$UTIL_PATH"/uips"
fi
## check root or user
IS_USER="/bin/true"
if [[ `echo -n $UID` == "0" ]]; then
IS_USER="/bin/false"
fi
## check arguments
## find install path
INSTALL_PATH=""
if $IS_USER; then
INSTALL_PATH=$USER_PATH
else
for (( i=0; $i < ${#DUKE_PATH}; i=$i+1 )); do
if [[ -f ${DUKE_PATH[$i]}"/duke3d.grp" ]]; then
INSTALL_PATH=${DUKE_PATH[$i]}
continue
fi
done
fi
if test "x$INSTALL_PATH" == "x"; then
echo "Could not locate EDuke32 installation." >&2
exit -1
fi
## patch
TMP_PATH="$TMP/patch.$$"
mkdir $TMP_PATH
"$TOP_DIR/patch" "$TOP_DIR" "$SRC_DIR" "$TMP_PATH"
cp -R "$TMP_PATH/." "$TOP_DIR/$SRC_DIR/"
rm -rf $TMP_PATH
## install
cp -R "$TOP_DIR/$SRC_DIR" "$INSTALL_PATH/"
cp "$TOP_DIR/$SCRIPT_DIR/*" "$INSTALL_PATH/"