#!/bin/sh

# Setup variables
PREFIX=/usr
WFUT_JAR=WFUT.jar

# Determine if java executable exists in path
JAVA=`which java`
if [ -x $JAVA ] ; then

  # See if Updater exists in home dir
  if [ ! -e $HOME/.sear/$WFUT_JAR ] ; then
    # Check to see if its in the install dir
    if [ -e $PREFIX/share/sear/$WFUT_JAR ] ; then
      # Install into home dir 
      echo "Installing Updater"

      # Create dir if required 
      if [ ! -d $HOME/.sear ] ; then
        mkdir $HOME/.sear
      fi

      # Copy WFUT to home dir
      cp $PREFIX/share/sear/$WFUT_JAR $HOME/.sear/

      # Create startup.script
      echo "/setvar SEAR_MEDIA $HOME/.sear/sear-media/" > $HOME/.sear/startup.script
      echo "/setvar SEAR_INSTALL $PREFIX/share/sear/" >> $HOME/.sear/startup.script
    fi
  fi

  # Run Updater if it exists
  if [ -e $HOME/.sear/$WFUT_JAR ] ; then
    # Store Current dir
    CUR_DIR=`pwd`
    # We need to change here so updater knows where to find / store data
    cd $HOME/.sear/
    # Update
    $JAVA -jar $WFUT_JAR update sear-media
    # Restore dir
    cd $PWD
  fi
else
  echo "Java not found in path. Updater cannot be run."
fi

# Execute real sear binary
echo "Starting Sear...."
$PREFIX/games/sear.bin

