:######################################################################## 
:# File name: _start.bat
:
: examine and edit the drive list indicated by the arrow below.
: 
:######################################################################## 

@echo off

rem ## Save return path
pushd %~dp0

: Find a free drive letter  (***** Edit this list and remove any drives you specifically want excluded)
:             #
:             #
:             #
:           #####  
:            ###
:             #
for %%a in (D E F G H I J K L M N O P Q R S T U V W X Y ) do CD %%a: 1>> nul 2>&1 & if errorlevel 1 set freedrive=%%a

rem ## Use batch file drive parameter if included else use freedrive
set Disk=%1
if "%Disk%"=="" set Disk=%freedrive%

rem ## To force a drive letter, remove "rem" and change drive leter
rem set Disk=w

rem ## Having decided which drive letter to use create the disk
subst %Disk%: "udrive"

rem ## Save drive letter to file. Used by stop bat 
(set /p dummy=%Disk%) >udrive\drive.txt <nul

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

rem ## It exists is it running? fo an artifact of a bad shutdown
SET /P pid=<udrive\usr\local\apache2\logs\httpd.pid
netstat -anop tcp | FIND /I " %pid%" >NUL
IF ERRORLEVEL 1 goto :APACHENOTRUNNING
IF ERRORLEVEL 0 goto :APACHERUNNING

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

:APACHENOTSTARTED
rem ## Check for another server on this Apache port
netstat -anp tcp | FIND /I "0.0.0.0:8088" >NUL
IF ERRORLEVEL 1 goto APACHENOTFOUND
echo.
echo Another server is running on port 8088 cannot run Apache server
echo.
goto APACHEEND

:APACHENOTFOUND
rem ## Set variable paths
set apachepath=\usr\local\apache2\
del  /q  %Disk%:%apachepath%logs\*.*
set apacheit=%Disk%:%apachepath%bin\Apache_8.exe -f %apachepath%conf\httpd.conf -d %apachepath%.

rem ## Start server
%Disk%:
start %Disk%:\home\admin\program\uniserv.exe "%apacheit%" 

echo Apache Started
echo To view site type http://127.0.0.1:8088 into a browser
goto :APACHEEND

:APACHERUNNING
CLS
subst %Disk%: /D
echo.
echo An Apache server is already running on port 8088
echo Stop the server using _stop.bat
goto MYSQLEND

:APACHEEND
echo.
echo Starting MYSQL
:E:\X mini_server - TEST\udrive\usr\local\mysql
rem ## Check to see if mysql not started
if NOT exist udrive\usr\local\mysql\data\mysql_mini.pid goto :MNOTSTARTED

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 :MYSQLNOTRUNNING
IF ERRORLEVEL 0 goto :MYSQLRUNNING 

:MYSQLNOTRUNNING
rem ## Not shutdown using mysql_stop.bat hence delete file
del udrive\usr\local\mysql\data\mysql_mini.pid 

:MNOTSTARTED
rem ## Check for another server on this MySQL port
netstat -anp tcp | FIND /I "0.0.0.0:3311" >NUL
IF ERRORLEVEL 1 goto MYSQLNOTFOUND
echo.
echo Another server is running on port 3311 cannot run MySQL server
echo.
goto MYSQLEND

:MYSQLNOTFOUND
rem ## Start MYSQL
%Disk%:
del  /q  \usr\local\mysql\tmp\*.*
start \usr\local\mysql\bin\mysqld.exe --defaults-file=/usr/local/mysql/bin/my.ini

rem ## Info to user
echo MySQL server is running on disk %Disk%:\ [port 3311]
goto :MYSQLEND

:MYSQLRUNNING
CLS
echo.
echo MySQL server already running.
echo You can stop the server using _stop.bat

:MYSQLEND
echo.

rem ## Return to caller
popd

echo.
echo ###########################################################
echo #                                                         #
echo #  Servers will continue to run after this window closes. #
echo #                                                         #
echo ###########################################################
echo.
: 2 second delay before starting ie
ping -w 1000 127.0.0.1 > NUL
ping -w 1000 127.0.0.1 > NUL
explorer http://127.0.0.1:8088

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


