#!/bin/bash

# ts2perlmod_multi_startscript - TS2PerlMod startscript for Linux
# This script is a very very modified version of something that seemed to be a startscript long time ago
# Copyright (c) 2006 Steven Barth - www.planetteamspeak.com

mkdir -p pid

case "$1" in
        start)
                if [ -e ts2perlmod.pl ]; then
                        echo "Starting all instances of PerlMod..."
                        for i in configs/*.ini; do
                                echo -n "Starting `basename $i .ini`... "
                                perl ./ts2perlmod.pl -config=`basename $i` -pidfile=pid/`basename $i .ini`.pid -daemon 1>/dev/null || echo -n "FAILED!"
                                echo
                        done
                        echo "done."
                else
                        echo "Unable to find ts2perlmod.pl: tell me where to find it"
                        exit 5
                fi
        ;;
        stop)
                echo "Stopping all running TS2PerlMod threads... "
                for i in pid/*.pid; do
                        if [ -e $i ]; then
                                echo -n "Stopping $i..."
                                kill -SIGTERM `cat $i` || echo -n "FAILED!"
                                echo
                                rm $i
                        else
                                echo
                                echo "WARNING: No files found!"
                                echo
                        fi
                done
                echo "done."
                exit 0
        ;;
        restart)
                $0 stop && $0 start || exit 1
        ;;
        *)
                echo "Usage: $0 {start | stop | restart}"
                exit 2
esac
exit 0
