#!/bin/sh /etc/rc.common
################################################################################
#                                                                              #
# Polish Koders Team 2010                                                      #
#                                                                              #
# contact: http://pkteam.pl                                                    #
#                                                                              #
################################################################################
#
# Card Server general init script
# version $Revision: 338 $ $Date: 2010-12-31 21:54:33 +0100 (pią) $
#

START=35

EXTRA_COMMANDS="status config"
EXTRA_HELP=$(echo -e "	config	< newcs|oscam_server > Set new Card Server\n \
	status	Show current Card Server status")

CONF_EMUMAN="config.plugins.emuman"

FLAVOR=$(config_get "${CONF_EMUMAN}.cs")


# Prepare cs configuration arguments 
#
prepare_conf() { #flavor
	case "$1" in
	  newcs)	CONF="-C /var/cardserver/newcs.xml";;
	  oscam_server)	CONF="-b -c /var/cardserver/";;
	  *)		help1
	  		exit 1;;
	esac
	echo "${CONF}"
}

# Prepare cs enviroment variables
#
prepare_env() {
	CMDENV="PATH=/var/cardserver "
	echo "${CMDENV}"
}

start() {
	if [ -z "${FLAVOR}" ]; then
	  echo "Card Server not configured"
	  return
	fi

	local pids=$(pidof ${FLAVOR})
	if [ ! -z "${pids}" ]; then
	  echo "${FLAVOR} alredy running with pids: [${pids}]"
	  return
	fi
	
	echo "Starting ${FLAVOR}"
	echo "Starting ${FLAVOR}" > /dev/vfd
	eval "$(prepare_env)$FLAVOR $(prepare_conf $FLAVOR) > /dev/null"
}

stop() {
	if [ -z "${FLAVOR}" ]; then
	  echo "Card Server not configured"
	  return
	fi
	echo "Stopping ${FLAVOR}"
	killall -15 "${FLAVOR}" 2> /dev/null
}

status() {
	echo $FLAVOR
	if [ -z "${FLAVOR}" ]; then
	  echo "Card Server not configured"
	  return
	fi

	local pids=$(pidof ${FLAVOR})
	if [ -z "${pids}" ]; then
	  echo "Not running ${FLAVOR}"
	else
	  echo "Running ${FLAVOR}: [${pids}]"
	fi
}

config() {
	if [ -z "$1" ]; then
	  config_set "${CONF_EMUMAN}.cs" ""
	  echo "Card Server disabled"
	  return
	fi
	config_set "${CONF_EMUMAN}.cs" "$1"
	echo "Card Server $1 configured"
}
