#!/bin/sh
##
# iRAPP Proxy Server control
# Mac OS X 10.4+
#
# Copyright (C) 2006-2011, Code Rebel LLC. 
# All rights reserved. 
##

if [ $UID -ne 0 ] ; then
   echo   "ERROR: You have not permissions to manage iRAPP Proxy Server."
   echo   "Please use \"sudo\", e.g: sudo rapproxyctl start -w"
   exit 1
fi

CMD="$1"
OPT="$2"

if [ "$OPT" = "" ] ; then OPT="-F" ; fi

if [ "$CMD" = "start" ] ;  then

   launchctl load $OPT "/System/Library/LaunchDaemons/com.coderebel.rapproxyd.plist"

elif [ "$CMD" = "stop" ] ;  then

   launchctl unload $OPT "/System/Library/LaunchDaemons/com.coderebel.rapproxyd.plist" > /dev/null 2>&1

elif [ "$CMD" = "restart" ] ; then

   launchctl unload $OPT "/System/Library/LaunchDaemons/com.coderebel.rapproxyd.plist" > /dev/null 2>&1
   launchctl load $OPT "/System/Library/LaunchDaemons/com.coderebel.rapproxyd.plist"
   
elif [ "$CMD" = "state" ] ;  then

   launchctl list | grep com.coderebel.rapproxyd

else
   echo   "ERROR: Unknown command: $CMD"
   echo   "USAGE: rapproxyctl start/stop/state [-w]"
   echo   "NOTE:  Use -w option to store state permanently"
   exit 1 
fi

exit $?
