#!/bin/bash
### BEGIN INIT INFO
# Provides:          gotohttp
# Required-Start:    $local_fs $network
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: gotohttp
# Description:       gotohttp
### END INIT INFO

SERVICE_NAME=gotohttp


PROGRAM=$GOTOHTTP

DEFAULTACT=uninstall
start() {
    
        echo "Starting $SERVICE_NAME ..." 
        $PROGRAM noboot
        exit 0;
   
}

stop() {
    echo "Stopping $SERVICE_NAME ..."
    pkill  $SERVICE_NAME
}

install() {
    command -v chkconfig >/dev/null 2>&1 && { chkconfig --add gotohttp; exit 0;}
    command -v systemctl >/dev/null 2>&1 && { systemctl enable gotohttp; exit 0;}
    command -v update-rc.d >/dev/null 2>&1 && { update-rc.d gotohttp defaults; exit 0;}
}

remove() {
if [ -f /etc/init.d/gotohttp ];then
		rm /etc/init.d/gotohttp
fi
if [ -f /etc/systemd/system/gotohttp.service ];then		
		rm /etc/systemd/system/gotohttp.service
fi		
if [ -f /usr/lib/systemd/system/gotohttp.service ];then		
		rm /usr/lib/systemd/system/gotohttp.service
fi		
    stop
    exit 0
}

uninstall() {
   
    command -v chkconfig >/dev/null 2>&1 && { chkconfig --del gotohttp; remove;}
    command -v systemctl >/dev/null 2>&1 && { systemctl disable gotohttp; remove;}
    command -v update-rc.d >/dev/null 2>&1 && { update-rc.d -f gotohttp remove; remove;}
    
}

usage() {
    echo $"Usage: $0 {start|stop|restart|status}"
  	exit 1
}


case "$1" in
start)
  start
  ;;
stop)
  stop
  ;;
reload|restart)
  stop
  start
  ;;
install)
  install
  ;;
uninstall)
  uninstall
  ;;
status)
  status $SERVICE_NAME
  ;;
*)
  $DEFAULTACT
  exit 1
esac
