:#######################################################################
:# File name: _stop.bat
:######################################################################## 

@echo off
echo.
rem ## Save return path
pushd %~dp0

rem ## Get drive letter
SET /P Disk=<udrive\drive.txt

echo Stopping Apache
rem ## Check to see if already stopped
if NOT exist udrive\usr\local\apache2\logs\httpd.pid goto :AALREADYKILLED

rem ## It exists is it running
SET /P pid=<udrive\usr\local\apache2\logs\httpd.pid
netstat -anop tcp | FIND /I " %pid%" >NUL
IF ERRORLEVEL 1 goto :ANOTRUNNING
IF ERRORLEVEL 0 goto :ARUNNING

:ANOTRUNNING
rem ## Not shutdown using server_stop.bat hence delete file
del udrive\usr\local\apache2\logs\httpd.pid
goto :AALREADYKILLED

rem ## It is running hence shut server down
:ARUNNING
rem ## Kill Apache process and all it's sub-processes
SET killstring= -t "%pid%"
udrive\home\admin\program\pskill.exe  Apache_8.exe c
echo  Apache Stopped

rem ## Remove pid file
del udrive\usr\local\apache2\logs\httpd.pid


goto :AEND

:AALREADYKILLED
echo  Apache already stopped
:AEND
echo.

echo Stopping MYSQL
rem ## Check to see if MYSQL already stopped
if NOT exist udrive\usr\local\mysql\data\mysql_mini.pid goto :MALREADYKILLED

rem ## It exists is it running
SET /P pid=<udrive\usr\local\mysql\data\mysql_mini.pid
netstat -anop tcp | FIND /I " %pid%" >NUL
IF ERRORLEVEL 1 goto :MNOTRUNNING
IF ERRORLEVEL 0 goto :MRUNNING 

:MNOTRUNNING
rem ## Not running clean-up
del udrive\usr\local\mysql\data\mysql_mini.pid 
goto :MALREADYKILLED

rem ## It is running so shut server down
:MRUNNING
rem ## Remove pid file server was closed
del udrive\usr\local\mysql\data\mysql_mini.pid

rem ## Remove disk file
del udrive\drive.txt

rem ## Kill server
udrive\usr\local\mysql\bin\mysqladmin.exe --port=3311 --user=root --password=root shutdown

echo  MySQL Stopped

goto :MEND

:MALREADYKILLED
echo  MySQL already stopped
:MEND
echo.

rem ## Kill drive
subst %Disk%: /D

rem ## Return to caller
popd

echo.
setlocal ENABLEDELAYEDEXPANSION
for /l %%a in (5,-1,1) do (
echo Closing window in %%a Seconds
ping -w 1000 127.0.0.1 > NUL
)
