#!/bin/bash

server_name="/usr/bin/myserver"
log_path="/var/log/myserver.log"
pid_file="/var/run/myserver.pid"

ip=127.0.0.1	# default ip
port=80			# default port

cmd=
case $1 in
	start) {
		echo "Starting server..."
		cmd="start"
	} ;;
	stop) {
		echo "Stopping server..."
		cmd="stop"
	} ;;
	*) (
		echo "Usage: $0 start|stop options"
		echo "Options: ip, port"
	) ;;
esac

if [[ "$cmd" == "" ]]; then
	exit 1
fi

shift

TEMP=`getopt -o i:p: --long ip:,port: -- "$@"`

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

while true ; do
        case "$1" in
                -i|--ip) ip=$2 ; echo "ip=$ip" ; shift 2 ;;
                -p|--port) port=$2 ; echo "port=$port" ; shift 2 ;;
                --) shift ; break ;;
                *) echo "Internal error!" ; exit 1 ;;
        esac
done

if [[ "$cmd" == "start" ]]; then
	$myserver $pid_file $ip $port $log_path # в зависимости от интерфейса сервера
	if [[ "$?" == "0" ]]; then
		echo "Start SUCCESS =)"
	else
		echo "Start FAILED =("
	fi
else
	if [[ "$cmd" == "stop" ]]; then
		pid=`cat $pid_file`
		kill -s INT "${pid}"
		echo "Server has stopped"
	fi
fi
