#!/usr/bin/bash

set -e

. /usr/share/opengl-games-utils/opengl-game-functions.sh

checkDriOK "Smokin' Guns"

# For smokinguns 1.0 we directly use ~/.smokinguns/smokinguns
if [ -L $HOME/.smokinguns/smokinguns ]; then
	rm $HOME/.smokinguns/smokinguns
fi

# Clean up old smokinguns if any, note we do this undepent of the symlink
# existing as even older autodownloader installs ran directly from ~/.q3a/smokinguns
rm -fr $HOME/.q3a/smokinguns

if [ ! -f ~/.smokinguns/smokinguns/zpak000.pk3 ]; then
  set +e
  /usr/share/autodl/AutoDL.py /usr/share/ioquake3/smokinguns.autodlrc
  STATUS=$?
  set -e
  # status 2 means download was ok, but the user choice not to start the game
  if [ "$STATUS" = "0" -o "$STATUS" = "2" ]; then
    pushd ~/.smokinguns > /dev/null
    unzip -qq -u Smokin_Guns_1.0.zip
    # remove any old versions (if present) otherwise the mv fails
    rm -fr smokinguns
    mv "Smokin' Guns/smokinguns" .
    rm -r "Smokin' Guns" Smokin_Guns_1.0.zip
    popd > /dev/null
  fi
  if [ "$STATUS" != "0" ]; then
    exit $STATUS
  fi
fi

# We run from ~/.smokinguns, using ~/.q3a is a bad idea as that will
# cause com_standalone to not get set if regular quake3 is also present
CVARS="+set com_basegame smokinguns"
CVARS="$CVARS +set com_homepath .smokinguns"
CVARS="$CVARS +set fs_basepath /usr/share/smokinguns"
CVARS="$CVARS +set com_hunkMegs 128"

exec ioquake3 $CVARS "$@"
