#!/bin/sh -e
#
### BEGIN INIT INFO
# Provides:             serverg
# Required-Start:       $remote_fs $syslog
# Required-Stop:        $remote_fs $syslog
# Default-Start:        
# Default-Stop:         0
# Short-Description:    Start/stop Server General services
# Description:		Server General protects directories of applications
#			by encrypting and restricting access to the
#			directories.
### END INIT INFO

# (c) Copyright 2001-2013, Packet General Networks, Inc. All rights reserved.

# Copyright (c) 2013.  The Packet General Networks, Inc. (Packet General).
# All Rights Reserved. Contact Packet General Networks, Inc., 865
# Merrick Road, Suite 204, NY 11510, (650) 485-1415 for any additional
# information regarding this notice.

# All materials are the intellectual property of Packet General
# Networks, Inc. and may not be copied, reproduced, distributed or
# displayed without Packet General Networks, Inc.'s express written
# permission.

# IN NO EVENT SHALL PACKET GENERAL BE LIABLE TO ANY PARTY FOR DIRECT,
# INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
# LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
# DOCUMENTATION, EVEN IF PACKET GENERAL HAS BEEN ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# PACKET GENERAL SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION,
# IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". PACKET GENERAL HAS NO
# OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
# MODIFICATIONS.

FILENAME=/etc/serverg/serverg.conf
REPO=repo
APP=app_name

case "$1" in
  start)
	;;
  restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;

  stop)
	# Go over the config file and stop the apps
	while read LINE
	do
		# Take care of the blank line
		if [ -z $LINE ]; then
			continue;
		fi

		str=`echo $LINE | awk '{print substr($0,0,8)}'`
		if [ $str = $APP ]; then
			apps=`echo $LINE | awk '{print substr($0,10)}'`
			#Split apps if there are more than one apps
			n=`echo $apps | awk '{print split($0, arr, ",")}'`
			i=1
			while [ $i -le $n ]
			do
				app=`echo $apps | cut -f $i -d ","`
				service $app stop
				i=`expr $i + 1`
			done
		fi
	done < $FILENAME
	# Go over all the repos and unmount them
	while read LINE
	do
		# Take care of the blank line
		if [ -z $LINE ]; then
			continue;
		fi

		str=`echo $LINE | awk '{print substr($0,0,4)}'`
		if [ $str = $REPO ]; then
			repos=`echo $LINE | awk '{print substr($0,6)}'`
			#Split apps if there are more than one apps
			n=`echo $repos | awk '{print split($0, arr, ",")}'`
			repo=`echo $repos | cut -f 1 -d ","`
			umount $repo
		fi
	done < $FILENAME

        ;;
  *)
        log_action_msg "Usage: $0 {stop}"
        exit 1
esac

exit 0

