#!"C:\Arquivos de programas\LucasArts\Star Wars Jedi Knight Jedi Academy\GameData\JAScript\bash.exe"
# Directory where your bash.exe is located.
# Format should always contain the shebang line #! at the beginning, use " " if the location has blank spaces or special characters.

# Windows CGI Monitor script.
# Part of server starter/restarter and crash checker for Jedi Knight:Jedi Academy.

# HTML+BASH SCRIPTING.
# Coded by Cthulhu@GBITnet.com.br and eXploit@GBITnet.com.br.

# INFORMATION

# You will need a webhosting account with CGI enabled for this script to work.
# The script must be located in the same box and user, where the main script is located.
# Windows does not have a module such as suExecPHP/suPHP, beware that the script/server will be executed at the same user which runs the httpd process.

# ************************************************************************************************** #

######################################################################################################
#                                Configuration zone. (MANDATORY)                                     #
######################################################################################################

# Directory where the the main script is located. (JAScript) (DO NOT ADD A \ OR / AT THE END.)

cgiscriptdir="C:\Arquivos de programas\LucasArts\Star Wars Jedi Knight Jedi Academy\GameData\JAScript"

# Display Server log.
# 0 = Disabled, 1 = Enabled.

cgidisplaylogserver="1"

# Display start/restart log.
# 0 = Disabled, 1 = Enabled.

cgidisplaylognormal="1"

# Display crash log.
# 0 = Disabled, 1 = Enabled.

cgidisplaylogcrash="1"

######################################################################################################
#                               End of configuration zone. (MANDATORY)                               #
######################################################################################################

# ************************************************************************************************** #

# DO NOT CHANGE ANYTHING FROM HERE.

# Date code for autodownload.

versionc="19062010" # NEVER CHANGE THIS LINE OR AUTODOWNLOAD WILL BREAK.

# Local functions.

bashvar ()
{

  bash.exe cgicmd > /dev/null 2>&1

  . cgivar > /dev/null 2>&1

}

bashlog ()
{

  bash.exe cgicmd > /dev/null 2>&1

  . cgilog

}

sstartercheckf ()
{

  if [ -f serverstarter.pid ]; then

    echo 'cgicmd=`tasklist /V /NH /FI "IMAGENAME eq bash.exe" | ./grep -s "'$user'" | ./gawk "{print $2}" | ./grep -ws -f serverstarter.pid`' > cgicmd
    echo 'if [ -n "$cgicmd" ]; then' >> cgicmd
    echo 'echo "cgipalivere=yes" > cgivar' >> cgicmd
    echo 'fi' >> cgicmd

    bashvar

  else

    cgipalivere=

  fi

}

scriptpidcheckf ()
{

  echo 'echo cgiscriptpid=`./cat script.pid` > cgivar' > cgicmd

  bashvar

}

sstarterpidcheckf ()
{

  echo 'echo cgistarterpid=`./cat serverstarter.pid` > cgivar' > cgicmd

  bashvar

}

# Main script directory check for bash.exe and the master script (master.sh).

if [ -n "$cgiscriptdir" ]; then

  if [ -f "$cgiscriptdir"/bash.exe ] && [ -f "$cgiscriptdir"/master.sh ]; then

    cd "$cgiscriptdir"

  else

    exit 10

  fi

else

  exit 10

fi

. cfg/script.cfg # Configuration call.

# HTML initial code.

echo "Content-type: text/html"
echo ""

echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<title>JKA Script BASH+HTML/CGI Monitor</title>'
echo '</head>'
echo '<body bgcolor="#888888">'

# Script status.

  echo -n "<b>Master Script status:</b> "

  if [ -f script.pid ]; then

    echo 'cgicmd=`tasklist /V /NH /FI "IMAGENAME eq bash.exe" | ./grep -s "'$user'" | ./gawk "{print $2}" | ./grep -ws -f script.pid`' > cgicmd
    echo 'if [ -n "$cgicmd" ]; then' >> cgicmd
    echo 'echo "cgipalive=yes" > cgivar' >> cgicmd
    echo 'fi' >> cgicmd

    bashvar

  else

    cgipalive=

  fi

  if [ "$cgipalive" = yes ]; then

    echo '<span style="color: #009900"><b>ON</b></span>'
    echo "<br>"

  else

    echo '<span style="color: red"><b>OFF</b></span>'
    echo "<br>"

  fi

# Server status.

  echo -n "<b>JKA Server process status:</b> "

  echo 'cgicmd=`tasklist /V /NH /FI "IMAGENAME eq '$process'" | ./grep -s "$user"`' > cgicmd
  echo 'if [ -n "$cgicmd" ]; then' >> cgicmd
  echo 'echo "cgipalivese=yes" > cgivar' >> cgicmd
  echo 'fi' >> cgicmd

  bashvar

  if [ "$cgipalivese" = yes ]; then

    echo '<span style="color: #009900"><b>ON</b></span>'
    echo "<br>"

  else

    echo '<span style="color: red"><b>OFF</b></span>'
    echo "<br>"

  fi

# Command submit buttons.

  echo "<br>"
  echo "<form method=GET action=\"${SCRIPT}\">"
       
    echo '<table border="0">'\
         '<tr>'\
         '<th>Command</th>'\
         '<th>Description</th>'\
         '</tr>'\
         '<tr>'\
         '<td><br><input type="submit" value="START" name="command"></td>'\
         '<td><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Start the master script.</b></td>'\
         '</tr>'\
         '<tr>'\
         '<td><input type="submit" value="STOP" name="command"></td>'\
         '<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Stop all scripts within the master script.</b></td>'\
         '</tr>'\
         '<tr>'\
         '<td><input type="submit" value="KILLSERVER" name="command"></td>'\
         '<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Kill/Restart the JKA server process.</b></td>'\
         '</tr>'\
         '<tr>'\
         '<td><input type="submit" value="KILLALL" name="command"></td>'\
         '<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Stop all scripts within the master script and the JKA Server.</b></td>'\
         '</tr>'\
         '<tr>'\
         '<td><input type="submit" value="REFRESH"></td>'\
         '<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Refresh the page.</b></td>'\
         '</tr>'\
         '</table>'

  echo "</form>"

  if [ "$REQUEST_METHOD" != "GET" ]; then
        
    echo "<hr>Error:"\
         "<br>Error processing REQUEST_METHOD!=GET."
    
    exit 10

  fi

  if [ -n "$QUERY_STRING" ]; then
 
    echo "$QUERY_STRING" > cgivar

    . cgivar > /dev/null 2>&1

# Command reactions and status box.

    echo '<form>'
    echo '<fieldset>'\
         '<legend><b>Command Status:</b></legend>'

# Start script command line check.

    case "$command" in

      START)

        if [ -z "$cgipalive" ]; then

          JAScript.bat &

          echo '<span style="color: blue"><b>Call to start the script has been sent.</b></span>'

        else

          echo '<span style="color: blue"><b>Master script is already runnning.</b></span>'

        fi

        echo '<meta http-equiv="refresh" content="1;URL=?">'

        exit 0

      ;;

# Stop script/s command line check.

      STOP)

        sstartercheckf

        if [ "$cgipalive" = yes ] || [ "$cgipalivere" = yes ]; then

          if [ "$cgipalive" = yes ]; then

            scriptpidcheckf

            taskkill /F /PID "$cgiscriptpid" > /dev/null 2>&1

          fi

          if [ "$cgipalivere" = yes ]; then

            sstarterpidcheckf

            taskkill /F /PID "$cgistarterpid" > /dev/null 2>&1

          fi

          echo '<span style="color: blue"><b>Kill command sent.</b></span>'

        else

          echo '<span style="color: blue"><b>Script is currently not running.</b></span>'

        fi
    
        echo '<meta http-equiv="refresh" content="1;URL=?">'

        exit 0

      ;;

# Killserver command line check.

      KILLSERVER)

        if [ "$cgipalivese" = yes ]; then

          taskkill /F /FI "USERNAME eq "$user"" /IM "$process" /T > /dev/null 2>&1
      
          echo '<span style="color: blue"><b>Killserver command sent.</b></span>'

        else

          echo '<span style="color: blue"><b>Server is currently not running.</b></span>'

        fi  
    
        echo '<meta http-equiv="refresh" content="1;URL=?">'

        exit 0

      ;;

# Killall command line check.

      KILLALL)

        sstartercheckf

        if [ "$cgipalive" = yes ] || [ "$cgipalivere" = yes ] || [ "$cgipalivese" = yes ]; then

          if [ "$cgipalive" = yes ]; then

            scriptpidcheckf

            taskkill /F /PID "$cgiscriptpid" /T > /dev/null 2>&1

          elif [ "$cgipalivere" = yes ]; then

            sstarterpidcheckf

            taskkill /F /PID "$cgistarterpid" /T > /dev/null 2>&1

          elif [ "$cgipalivese" = yes ]; then

            taskkill /F /FI "USERNAME eq "$user"" /IM "$process" /T > /dev/null 2>&1

          fi

          echo '<span style="color: blue"><b>Killall command sent.</b></span>'

        else

          echo '<span style="color: blue"><b>Neither the script nor the server is currently running.</b></span>'

        fi  
    
        echo '<meta http-equiv="refresh" content="1;URL=?">'

        exit 0

      ;;
    
      *)

        echo -n '<span style="color: yellow"><b>'
        echo -n "ERROR: Invalid command line. Please execute only the commands available at the Monitor's page."
        echo "</b></span>"
        echo '<meta http-equiv="refresh" content="1;URL=?">'

        exit 10

      ;;

    esac

    echo '</fieldset>'
    echo '</form>'

  fi  

# Server log display.

  if [ -n "$cgidisplaylogserver" ]; then

    case "$cgidisplaylogserver" in

      0)

        echo '<form>'
        echo '<fieldset>'\
             "<legend><b>Display server log is disabled.</b></legend>"
        echo '</fieldset>'
        echo '</form>'

      ;;

      1)

        echo '<form>'
        echo '<fieldset>'

        if [ -n "$logserver" ]; then

          cd "$dir"

          echo "<legend><b>Server log:</b> "$logserver"</legend>"
          echo '<div style="width: 972; height: 500; overflow: auto";>'
  
          if [ -f "$logserver" ]; then

            echo -n '"'$cgiscriptdir'"/sed -e "s/' > "$cgiscriptdir"/cgicmd
            echo -n "'/*/g" >> "$cgiscriptdir"/cgicmd
            echo -n '" -e "s/^/' >> "$cgiscriptdir"/cgicmd
            echo -n "'/" >> "$cgiscriptdir"/cgicmd
            echo -n '" -e ' >> "$cgiscriptdir"/cgicmd
            echo -n "'s/$/<br>/' -e 's/^/echo /' -e " >> "$cgiscriptdir"/cgicmd
            echo -n '"s/$/' >> "$cgiscriptdir"/cgicmd
            echo -n "'/" >> "$cgiscriptdir"/cgicmd
            echo '" "'$logserver'" > "'$cgiscriptdir'"/cgilog' >> "$cgiscriptdir"/cgicmd
            
            "$cgiscriptdir"/bash.exe "$cgiscriptdir"/cgicmd > /dev/null 2>&1

            . "$cgiscriptdir"/cgilog

          fi

          echo '</div>'
          echo '</fieldset>'
          echo '</form>'

        else

          echo "<legend><b>WARNING: Server log:</b> LOG FILE NOT SET</legend>"
          echo '</fieldset>'
          echo '</form>'

        fi

      ;;

      *)

        echo '<span style="color: yellow"><b>CGI ERROR: Value for display server log is incorrect. Please set a value between 0 and 1.</b></span>'

        exit 10

      ;;

    esac

  else

    echo '<span style="color: yellow"><b>CGI ERROR: No value is set for display server log. Please set a value between 0 and 1.</b></span>'

    exit 10

  fi

# Re-setting working directory as cgiscriptdir.

cd "$cgiscriptdir"

# Server start/restart log display.

  if [ -n "$cgidisplaylognormal" ]; then

    case "$cgidisplaylognormal" in

      0)

        echo '<form>'
        echo '<fieldset>'\
             "<legend><b>Display server start/restart log is disabled.</b></legend>"
        echo '</fieldset>'
        echo '</form>'

      ;;

      1)

        echo '<form>'
        echo '<fieldset>'

        if [ -n "$lognormal" ]; then

          echo "<legend><b>Server start/restart log:</b> "$lognormal"</legend>"
          echo '<div style="width: 972; height: 150; overflow: auto";>'
  
          if [ -f "$lognormal" ]; then

            echo -n './sed -e "s/^/' > cgicmd
            echo -n "'/" >> cgicmd
            echo -n '" -e ' >> cgicmd
            echo -n "'s/$/<br>/' -e 's/^/echo /' -e " >> cgicmd
            echo -n '"s/$/' >> cgicmd
            echo -n "'/" >> cgicmd
            echo '" "'$lognormal'" > cgilog' >> cgicmd

            bashlog

          fi

          echo '</div>'
          echo '</fieldset>'
          echo '</form>'

        else

          echo "<legend><b>WARNING: Server start/restart log:</b> LOG FILE NOT SET</legend>"
          echo '</fieldset>'
          echo '</form>'

        fi

      ;;

      *)

        echo '<span style="color: yellow"><b>CGI ERROR: Value for display server start/restart log is incorrect. Please set a value between 0 and 1.</b></span>'

        exit 10

      ;;

    esac

  else

    echo '<span style="color: yellow"><b>CGI ERROR: No value is set for display server start/restart log. Please set a value between 0 and 1.</b></span>'

    exit 10

  fi

# Crash log display.

  if [ -n "$cgidisplaylogcrash" ]; then

    case "$cgidisplaylogcrash" in

      0)

        echo '<form>'
        echo '<fieldset>'\
             "<legend><b>Display crash log is disabled.</b></legend>"
        echo '</fieldset>'
        echo '</form>'

      ;;

      1)

        echo '<form>'
        echo '<fieldset>'

        if [ -n "$logcrash" ]; then

          echo "<legend><b>Crash log:</b> "$logcrash"</legend>"
          echo '<div style="width: 972; height: 150; overflow: auto";>'
  
          if [ -f "$logcrash" ]; then

            echo -n './sed -e "s/^/' > cgicmd
            echo -n "'/" >> cgicmd
            echo -n '" -e ' >> cgicmd
            echo -n "'s/$/<br>/' -e 's/^/echo /' -e " >> cgicmd
            echo -n '"s/$/' >> cgicmd
            echo -n "'/" >> cgicmd
            echo '" "'$logcrash'" > cgilog' >> cgicmd

            bashlog

          fi

          echo '</div>'
          echo '</fieldset>'
          echo '</form>'

        else

          echo "<legend><b>WARNING: Crash log:</b> LOG FILE NOT SET</legend>"
          echo '</fieldset>'
          echo '</form>'

        fi

      ;;

      *)

        echo '<span style="color: yellow"><b>CGI ERROR: Value for display crash log is incorrect. Please set a value between 0 and 1.</b></span>'

        exit 10

      ;;

    esac

  else

    echo '<span style="color: yellow"><b>CGI ERROR: No value is set for display crash log. Please set a value between 0 and 1.</b></span>'

    exit 10

  fi

# Removing cgicmd, cgivar, and cgilog, as it's no longer necessary at this point.

  ./rm -f cgicmd cgivar cgilog > /dev/null 2>&1 

# HTML final code.

echo '</body>'
echo '</html>'

# Exit with a 0 code if script made this far.

exit 0