#!/bin/bash
export LD_LIBRARY_PATH=/usr/local/hostingcontroller/bin
export PATH=$PATH:/sbin:/usr/bin:/bin:/usr/local/bin
case "$1" in
  start)
	if pgrep hcremoting &>/dev/null; then
	    echo "HCnix remoting is already running"
	else
	     if [ -f /usr/local/hostingcontroller/HostingController.pid ]; then
         	rm /usr/local/hostingcontroller/HostingController.pid
 	     fi
	    /usr/local/hostingcontroller/bin/hcremoting start
	fi

	if pgrep hcserver &>/dev/null; then
	    echo "HCnix server is already running"
	else
	     if [ -f /usr/local/hostingcontroller/HCServer.pid ]; then
         	rm /usr/local/hostingcontroller/HCServer.pid
 	     fi
	    /usr/local/hostingcontroller/bin/hcserver start
	fi

	if pgrep hcquota &>/dev/null; then
	    echo "HCnix quota service is already running"
	else
	     if [ -f /usr/local/hostingcontroller/HCQuota.pid ]; then
         	rm /usr/local/hostingcontroller/HCQuota.pid
 	     fi
	    /usr/local/hostingcontroller/bin/hcquota start
	fi

	if pgrep hcmail &>/dev/null; then
	    echo "HCnix mail service is already running"
	else
	     if [ -f /usr/local/hostingcontroller/HCMail.pid ]; then
         	rm /usr/local/hostingcontroller/HCMail.pid
 	     fi
	    /usr/local/hostingcontroller/bin/hcmail start
	fi

	if pgrep hcsync &>/dev/null; then
	    echo "HCnix sync service is already running"
	else
	     if [ -f /usr/local/hostingcontroller/HCSync.pid ]; then
         	rm /usr/local/hostingcontroller/HCSync.pid
 	     fi
	    /usr/local/hostingcontroller/bin/hcsync start
	fi

 echo "HCnix Services Started"
        ;;
  stop)
 	if pgrep hcremoting &>/dev/null; then
	    /usr/local/hostingcontroller/bin/hcremoting stop
	    pkill -9 hcremoting	
	fi

	if pgrep hcserver &>/dev/null; then
	    /usr/local/hostingcontroller/bin/hcserver stop
	    pkill -9 hcserver	
	fi

	if pgrep hcquota &>/dev/null; then
	    /usr/local/hostingcontroller/bin/hcquota stop
	    pkill -9 hcquota
	fi

	if pgrep hcmail &>/dev/null; then
	    /usr/local/hostingcontroller/bin/hcmail stop
	    pkill -9 hcmail	
	fi

	if pgrep hcsync &>/dev/null; then
	    /usr/local/hostingcontroller/bin/hcsync stop
	    pkill -9 hcsync
	fi
    if [ -f /usr/local/hostingcontroller/HostingController.pid ]; then
 	rm /usr/local/hostingcontroller/HostingController.pid
    fi

    if [ -f /usr/local/hostingcontroller/HCServer.pid ]; then
 	rm /usr/local/hostingcontroller/HCServer.pid
    fi
    if [ -f /usr/local/hostingcontroller/HCQuota.pid ]; then
 	rm /usr/local/hostingcontroller/HCQuota.pid
    fi

    if [ -f /usr/local/hostingcontroller/HCMail.pid ]; then
 	rm /usr/local/hostingcontroller/HCMail.pid
    fi
    if [ -f /usr/local/hostingcontroller/HCSync.pid ]; then
 	rm /usr/local/hostingcontroller/HCSync.pid
    fi

	
 echo "HCnix Services Stopped"
        ;;

  restart)
  	if pgrep hcremoting &>/dev/null; then
	    /usr/local/hostingcontroller/bin/hcremoting stop
	    pkill -9 hcremoting	
	fi

	if pgrep hcserver &>/dev/null; then
	    /usr/local/hostingcontroller/bin/hcserver stop
	    pkill -9 hcserver	
	fi

	if pgrep hcquota &>/dev/null; then
	    /usr/local/hostingcontroller/bin/hcquota stop
	    pkill -9 hcquota
	fi

	if pgrep hcmail &>/dev/null; then
	    /usr/local/hostingcontroller/bin/hcmail stop
	    pkill -9 hcmail	
	fi

	if pgrep hcsync &>/dev/null; then
	    /usr/local/hostingcontroller/bin/hcsync stop
	    pkill -9 hcsync
	fi
    if [ -f /usr/local/hostingcontroller/HostingController.pid ]; then
 	rm /usr/local/hostingcontroller/HostingController.pid
    fi

    if [ -f /usr/local/hostingcontroller/HCServer.pid ]; then
 	rm /usr/local/hostingcontroller/HCServer.pid
    fi
    if [ -f /usr/local/hostingcontroller/HCQuota.pid ]; then
 	rm /usr/local/hostingcontroller/HCQuota.pid
    fi

    if [ -f /usr/local/hostingcontroller/HCMail.pid ]; then
 	rm /usr/local/hostingcontroller/HCMail.pid
    fi
    if [ -f /usr/local/hostingcontroller/HCSync.pid ]; then
 	rm /usr/local/hostingcontroller/HCSync.pid
    fi

        /usr/local/hostingcontroller/bin/hcremoting start
 	/usr/local/hostingcontroller/bin/hcserver start
	/usr/local/hostingcontroller/bin/hcsync start
	/usr/local/hostingcontroller/bin/hcquota start
	/usr/local/hostingcontroller/bin/hcmail start
 echo "HCnix Services Restarted"
        ;;
--version)
	/usr/local/hostingcontroller/bin/hcremoting --version
        ;;
--minor)
	/usr/local/hostingcontroller/bin/hcremoting --minor
        ;;	
  *)
	echo "Usage: hcnix {start | stop | restart}"
esac

exit 1
