# Control script for CrushFTP
# To setup, set $CRUSH_DIR and $USER properly, and possibly edit $JAVA, $PS, $GREP, and $WHOAMI variables.
#
# Rewritten by Jim Fougeron, Feb 2010
#
# THESE NEED TO BE SET
CRUSH_DIR="/var/opt/CrushFTP5_PC/" #crushftp directory:
USER="frame" # only work for this user
JAVA="java"
PS="ps"
AWK="awk"
GREP="grep"
WHOAMI="whoami"
NOHUP="nohup"

# example of how to redirect a low port to a high port so Crush doesn't have to run as root
# iptables -t nat -A PREROUTING -p tcp -m tcp --dport 21 -j REDIRECT --to-ports 2121

# We MUST start the server in the proper directory. If we can not change to that directory, we exit.
change_dir()
{
 cd $CRUSH_DIR
 ret_val=$?
 if [ ${ret_val} -ne 0 ]; then
   echo FAIL
   echo could not change to CrushFTP directory
   echo the directory is setup as:
   echo $CRUSH_DIR
   exit 1
 fi
}

# get PID from process list.  Not from a 'stored' file.  Since Crush updates will
# restart the server, but NEVER run this script, then if we stored off the PID into
# a file, then after an update, this script would not be able to shut down the
# process.  We have added a couple greps into the get_pid() so that we 'know' we
# are getting the proper PID if it exists.
get_pid()
{
 CRUSH_PID="`$PS -ef | $GREP java | $GREP $CRUSH_DIR | awk '{print $2}'`"
}

# if the wrong user runs this script then BAIL.  If this script should be run as user
# OTHER than 'root' (or su or sudo), then you must redirect port 21 (or 22) up to a higher
# port, such as 60021.  iptables can do this well.  Then setup the crush server to bind to
# these higher ports.  Running as non-root is much more secure.  NOTE, it 'is' valid for root
# to shut down the server (but not to start it, unless USER="root" is set at teh top of the file
ROOT_OK=0
chk_user()
{
  if [ "$USER" != `whoami` ]; then
    if [ `whoami` = "root" -a "$ROOT_OK" = "1" ]; then
# Not an error.  Root user is OK here, even if 'not' the proper user (such as killing the process).
       echo
    else
       echo FAIL
       echo "Wrong user.  The user running this script MUST be $USER, but you are `whoami`"
       exit 1;
    fi
  fi
}

#############################################################################################
# Here is the 'main' script.  We can either start the server, or shutdown the current       #
# running server.   There is error checking to make sure the proper user is being used.     #
#############################################################################################
case "$1" in
        start)
             chk_user
             get_pid
             if [ "$CRUSH_PID" ]; then
               echo FAIL
               echo Found an already running instance of CrushFTP.
               echo It is not valid o start 2 sessions in the same directory.
               exit 1;
             fi
             echo -n "Starting CrushFTP... "
             change_dir

             # run daemon
             $NOHUP $JAVA -Ddir=$CRUSH_DIR -Xmx384M -Dcrushftp.version=5 -jar CrushFTP.jar -d & >/dev/null 2>&1
             echo OK
        ;;

         stop)
             # root or $USER is ok to shut down the server.
             ROOT_OK=1
             chk_user
             get_pid
             if [ ! "$CRUSH_PID" ]; then
               echo FAIL
               echo Could not find Crush PID
               exit 1
             fi

             echo -n Shutting down CrushFTP...
             kill $CRUSH_PID
             ret_val=$?
             if [ ${ret_val} -ne 0 ]; then
                echo FAIL
                echo could not kill PID
                exit 1
             fi
             echo OK
        ;;

        *)
             echo "Usage: $0 [start|stop]   NOTE you must be logged in as $USER to run this script"
esac

exit 0

