#!/bin/sh

#BFM's Attempt at a Server controller
#==================
# start/stop/servers
# list files and folders
# edit logs (1-4)
# edit ini's
# close all
# servers in array[0]
# sep ini for controller?
# colour coded
#------------------


chkcnfg()
{
    #check if config file present;
        checkconfig="msc.conf"
  	  	#if [ -f ${ssline[$PREFIX]} ];
  	  	if [ -f $checkconfig ];
  	  	then 
          return
        else
          echo
          echo "==================================="
          echo "      ** A T T E N T I O N **"
          echo "==================================="
          echo " msc.conf : File Missing!!"
          echo
          echo " Master Server Controller Exiting!"
          echo "-----------------------------------"
          echo
          exit 1
        fi
} 
chkcnfg       
#import data from config file
source $checkconfig

#Define user Variables
OPT=$1   # option
SERVER=$2  # server prefix
CUSTOM=$3  # custom stuff ffu. (for future use)
suser=${USER}
shost=${HOSTNAME}

#FUNCTIONS;

#File Not Found
FNF()
{
        cd $newpath  
  	  	#if [ -f ${ssline[$PREFIX]} ];
  	  	if [ -f $checkfile ];
  	  	then 
          return
        else
          echo
          echo "==================================="
          echo "      ${red}** A T T E N T I O N **${normal}"
          echo "==================================="
          echo "ERROR in ${red}${sdesc[$PREFIX]}${normal} config!"
          echo "file ${red}${checkfile}${normal} not found!"
          echo "Please check ${lblue}msc.conf${normal} settings..."
          echo "-----------------------------------"
          echo
          exit 1
        fi
}

#Server not found
SNF()
{
    n=${#spath[@]} #num elements in array spath
      
     for (( i=0; i<n; i++ ))
    do
      tmp2="${sprefix[$i]}"
      if [ "$SERVER" == "$tmp2" ]
      then 
      PREFIX=$i
      #echo "$PREFIX : $SERVER : ${sprefix[$i]}"
      fi
     done
      if [ "$SERVER" != "${sprefix[$PREFIX]}" ]
      then
        if [ "$SERVER" == "" ] 
          then
            return
        fi
            echo
            echo "==================================="
            echo "${lblue}$SERVER ${normal}: ${red}Server not found!${normal}"
            echo "-----------------------------------"
            echo "usage: ${lblue}$0 -s ${sprefix[0]}${normal}"
            echo "==================================="
            echo
      exit 1
      fi     
}

noservers()
{
#check that at least one server is configured;
n=${#spath[@]} #num elements in array spath
if [ $n=" " ]
  then
    echo "==================================="
    echo "   Please configure at least one
    echo "   server in the ${lblue}.conf${normal}
    echo "==================================="
    exit 1
  fi
} 
#noservers
# check for Root 
if [ $suser == "root" ]
      then   
        echo "${red}you are currently logged in as root"
        echo "please use non root account.${normal}"
        read
        exit
      fi
     

# Check PREFIX against msc servers...
SNF
     
# bash servers [-OPTIONS]
case $OPT in
  -s|-S)
        newpath="${spath[$PREFIX]}"
        checkfile="${ssline[$PREFIX]}"
  	  	FNF
  	  	echo
        echo "==================================="
  	  	echo "${green}Starting:${normal} ${lblue}${sdesc[$PREFIX]}${normal}"
  	  	echo "===================================" 
  	  	echo
  	  	cd ${spath[$PREFIX]}
  	  	${sstart[$PREFIX]}
  	  	exit
    ;;
  -r|-R)
        newpath="${spath[$PREFIX]}"
        checkfile="${ssline[$PREFIX]}"
        FNF
        echo
        echo "==================================="
  	  	echo "${green}Restarting:${normal} ${lblue}${sdesc[$PREFIX]}${normal}"
  	  	echo "===================================" 
  	  	echo
  	  	cd ${spath[$PREFIX]}
  	  	${srestart[$PREFIX]}
  	  	exit
  	;;
  -x|-X)
        newpath="${spath[$PREFIX]}"
        checkfile="${ssline[$PREFIX]}"
        FNF
        echo
        echo "==================================="
  	  	echo "${red}Stopping:${normal} ${lblue}${sdesc[$PREFIX]}${normal}"
  	  	echo "===================================" 
  	  	echo
  	  	cd ${spath[$PREFIX]}
  	  	${sstop[$PREFIX]}
  	  	exit
  	;;
   -i|-I)
        #Heh tricky variable this fkn sfldrS (capital 'S' is important @end)
        newpath="${spath[$PREFIX]}${sfldrS[$PREFIX]}"
        checkfile="${sini[$PREFIX]}"
        FNF
        echo
        echo "==================================="
  	  	echo "${green}Opening:${normal} ${lblue}${sini[$PREFIX]}${normal}"
  	  	echo "===================================" 
  	  	echo "          press any key"
  	  	read
  	  	cd $newpath
  	  	$DEFED ${sini[$PREFIX]}
  	  	exit
        
  	;;
  -l|-L)
    echo
        echo "==================================="
  	  	echo "${green}System files:${normal} ${lblue}${sdesc[$PREFIX]}${normal}"
  	  	echo "===================================" 
  	  	echo "       Press any key to list files."
  	  	echo 
  	  	read
  	  	tmp="${spath[$PREFIX]}${sfldrS[$PREFIX]}"
  	  	LSCMD="${LSCMD[$PREFIX]}"
  	  	cd $tmp
  	  	$LSCMD 
  	  	exit
    ;;
  -1)
    newpath="${spath[$PREFIX]}${sfldrL[$PREFIX]}"
        checkfile="${slog1[$PREFIX]}"
        FNF
        echo
        echo "==================================="
  	  	echo "${green}Opening:${normal} ${lblue}${slog1[$PREFIX]}${normal}"
  	  	echo "===================================" 
  	  	echo "          press any key"
  	  	read
  	  	cd $newpath
  	  	$DEFED ${slog1[$PREFIX]}
  	  	exit
    ;;
   
  -2)
    newpath="${spath[$PREFIX]}${sfldrL[$PREFIX]}"
        checkfile="${slog2[$PREFIX]}"
        FNF
        echo
        echo "==================================="
  	  	echo "${green}Opening:${normal} ${lblue}${slog2[$PREFIX]}${normal}"
  	  	echo "===================================" 
  	  	echo "          press any key"
  	  	read
  	  	cd $newpath
  	  	$DEFED ${slog2[$PREFIX]}
  	  	exit
    ;;
  -3)
    newpath="${spath[$PREFIX]}${sfldrL[$PREFIX]}"
        checkfile="${slog3[$PREFIX]}"
        FNF
        echo
        echo "==================================="
  	  	echo "${green}Opening:${normal} ${lblue}${slog3[$PREFIX]}${normal}"
  	  	echo "===================================" 
  	  	echo "          press any key"
  	  	read
  	  	cd $newpath
  	  	$DEFED ${slog3[$PREFIX]}
  	  	exit
    ;;
  -4)
    newpath="${spath[$PREFIX]}${sfldrL[$PREFIX]}"
        checkfile="${slog4[$PREFIX]}"
        FNF
        echo
        echo "==================================="
  	  	echo "${green}Opening:${normal} ${lblue}${slog4[$PREFIX]}${normal}"
  	  	echo "===================================" 
  	  	echo "          press any key"
  	  	read
  	  	cd $newpath
  	  	$DEFED ${slog4[$PREFIX]}
  	  	exit
    ;;
  -al|-AL)
    newpath="${spath[$PREFIX]}${sfldrL[$PREFIX]}"
        #checkfile="${slog4[$PREFIX]}"
        #FNF
        echo
        echo "==================================="
  	  	echo "${green}Listing all .log files : $SERVER:${normal}"
  	  	echo "===================================" 
  	  	echo "          press any key"
  	  	read
  	  	cd $newpath
  	  	ls -l *.log
  	  	exit
    ;;
   -ol|-OL)
    newpath="${spath[$PREFIX]}${sfldrL[$PREFIX]}"
        checkfile="$CUSTOM"
        FNF
        echo
        echo "==================================="
  	  	echo "${green}Opening : $CUSTOM${normal}"
  	  	echo "===================================" 
  	  	echo "          press any key"
  	  	read
  	  	cd $newpath
  	  	$DEFED $CUSTOM
  	  	exit
    ;;
  
  -m|-M)
        newpath="${spath[$PREFIX]}${sfldrS[$PREFIX]}"
        checkfile="${smapvote[$PREFIX]}"
        FNF
        echo
        echo "==================================="
  	  	echo "${green}Opening:${normal} ${lblue}${smapvote[$PREFIX]}${normal}"
  	  	echo "===================================" 
  	  	echo "          press any key"
  	  	read
  	  	cd $newpath
  	  	$DEFED ${smapvote[$PREFIX]}
  	  	exit
    ;;
  -u|-u)
    newpath="${spath[$PREFIX]}${sfldrS[$PREFIX]}"
        checkfile="${useri[$PREFIX]}"
        FNF
        echo
        echo "==================================="
  	  	echo "${green}Opening:${normal} ${lblue}${useri[$PREFIX]}${normal}"
  	  	echo "===================================" 
  	  	echo "          press any key"
  	  	read
  	  	cd $newpath
  	  	$DEFED ${useri[$PREFIX]}
  	  	exit
    ;;
  -ci|-CI)
    newpath="${spath[$PREFIX]}${sfldrS[$PREFIX]}"
        checkfile="$CUSTOM"
        FNF
        echo
        echo "==================================="
  	  	echo "${green}Opening:${normal} ${lblue}$CUSTOM${normal}"
  	  	echo "===================================" 
  	  	echo "          press any key"
  	  	read
  	  	cd $newpath
  	  	$DEFED $CUSTOM
  	  	exit
    ;;
   
   -sa|-SA)
    echo
    echo "==================================="
    echo "      ${red}** A T T E N T I O N **${normal}"
    echo "==================================="
    echo
    echo "     ! SHUT DOWN ALL SERVERS !"
    echo 
    echo "         ARE YOU SURE? Y/n"
    echo
    read closeall
    echo "==================================="
    #if test ! -f "$FILE" 
     if [ $closeall != "Y" ]
      then   
        echo "${green}          *WHEW! Exiting*${normal}"
      exit 1
     else
      n=${#spath[@]} #num elements in array spath
 
     for (( i=0; i<n; i++ ))
    do
      if [ ${Exclude[$i]} == "True" ]
        then 
        echo "${green}Excluding:${normal} ${lblue}${sdesc[$i]}${normal}"
          continue
          else
     #echo "${red}Stopping${normal} : ${sdesc[$i]} ${spath[$i]} ${sstop[$i]}"
     echo "${red}Stopping:${normal} ${lblue}${sdesc[$i]}${normal}"
     cd ${spath[$i]}
  	  	${sstop[$i]}
  	  fi
  	done
  	echo "==================================="
  	echo
    fi
    ;;
   -ra|-RA)
   echo
    echo "==================================="
    echo "      ${green}Restarting ALL Servers${normal}"
    echo "==================================="
    echo "         ARE YOU SURE? Y/n"
    echo
    read closeall
    echo "==================================="
    #if test ! -f "$FILE" 
     if [ $closeall != "Y" ]
      then   
        echo "${green}          *WHEW! Exiting*${normal}"
      exit 1
     else
      
    n=${#spath[@]} #num elements in array spath
 
     for (( i=0; i<n; i++ ))
       do
       if [ ${Exclude[$i]} == "True" ]
        then 
        echo "${green}Excluding:${normal} ${lblue}${sdesc[$i]}${normal}"
          continue
          else
     #echo "${red}Restarting${normal} : ${sdesc[$i]} ${spath[$i]} ${sstop[$i]}"
     echo "${red}Restarting:${normal} ${lblue}${sdesc[$i]}${normal}"
     cd ${spath[$i]}
  	  	${srestart[$i]}
  	  	sleep $SRDELAY
  	fi
  	done
  	echo "==================================="
  	echo
    fi
    ;;
   -da|-DA)
      n=${#spath[@]}
      scount=0
         if [ -d "$DEPLOY" ]
            then
              echo
              echo "==================================="
              echo "${red}  KAUTION: DEPLOY ALL FILES FROM${normal}"
                echo "      ${lblue}$DEPLOY${normal} folder"
                echo "      to all ${green}NON-EXCLUDED${normal} Servers!"
                echo "==================================="
                echo "         ARE YOU SURE? Y/n"
                  read closeall
                  if [ $closeall != "Y" ]
                    then   
                      echo "${green}          *WHEW! Exiting*${normal}"
                      exit 1
                  fi
                  echo "${green} > Applying Permissions...${normal}"
                  cd $DEPLOY
                    chmod +x *.*
                  echo
              for (( i=0; i<n; i++ ))
                do
                  if [ ${Exclude[$i]} == "True" ]
                  then
                    echo "${red} - Excluded:${normal} ${lblue}${sdesc[$i]}${normal}"
                    continue
                    else
                    newpath="${spath[$i]}${sfldrS[$i]}"
                echo "${green} + Deploying: ${red}${sdesc[$i]}${normal}"
                echo "${lblue}   $newpath${normal}"
                cd $DEPLOY
                cp *.* $newpath
                cd $newpath
                scount=$((scount+1))
               fi
                done
                echo "==================================="
                echo "Files Deployed to ${lblue}${scount}/$n${normal} servers"
                echo "==================================="
          else
          echo "==================================="
            echo "${red}$DEPLOY Folder NOT FOUND!${normal}"
            echo "==================================="
        fi
      exit 1
    ;;
   -conf|-CONF)
    nano msc.conf
   ;;
   *) 
    # Display Parameters
    # Define Variables
    clear
    echo
    echo "==================================="
    echo "    MULTI SERVER CONTROLLER .1j"
    echo "-----------------------------------"
    if [ $suser == "root" ]
      then
        echo "user: ${red}$suser  ${normal}on: ${green}$shost${normal}"
      else
        echo "user: ${green}$suser  ${normal}on: ${green}$shost${normal}"
      fi
    echo "MSC path: ${green}$RPATH${normal}"
    echo "Usage: ${lblue}$0 -s bt${normal}"
    #Num servers
    n=${#spath[@]} #num elements in array spath
    echo "==================================="
    echo " Currently [${lblue}$n${normal}] Servers configured"
    echo "-----------------------------------"
   for (( i=0; i<n; i++ ))
    do
    echo " ${lblue}${sprefix[$i]}${normal}" : "${sdesc[$i]}"
   done
    echo "==================================="
    echo "         ${green}Server controls:${normal}"
    echo "-----------------------------------"
    echo "   -s  server : Start server."
    echo "   -r  server : Restart server."
    echo "   -x  server : Stop server."
    echo "   -l  server : List Files/Folders"
    #echo "           ${red}K A U T I O N${normal}"
    echo "   -sa server : Shut Down All!!!"
    echo "   -ra server : Restart ALL Servers"
    echo "   -da : Deploy files from..."
    echo "         ${lblue}$DEPLOY${normal}"
    echo "==================================="
    echo "         ${green}INI quick editor${normal}"
    echo "-----------------------------------"
    echo "   -i  server : edit ut.ini"
    echo "   -u  server : edit user.ini"
    echo "   -m  server : edit mapvote.ini"
    echo "   -ci server x.ext : edit 'file'"
    #echo "   -c2 server : edit custom2.ini"
    echo "==================================="
    echo "         ${green}LOG file editor${normal}"
    echo "-----------------------------------"
    echo "   -1  server : to view logs"
    echo "              : options: -1 to -4"
    #echo "   -al server : List all logs"
    #echo "   -ol server x.ext : read log x" 
    echo "==================================="	
    echo "Default Editor: ${green}${DEFED}${normal}"
    echo "==================================="
    echo "                          Back4More"
    #echo $PATH
    exit 1
    ;;
esac

