/* ndc.cmd



   A ReXX version of ndc for the OS/2 port of BIND 8


   This program make the following assumptions
	emxkill.exe is in PATH
	named.exe is in PATH
	ndc.exe is in PATH
	named.conf is in ETC
	named.pid is in ETC
	nolist.exe is in PATH	/* to hide named.exe after starting */

   Please change the three variables kill, named, and PIDFILE as needed
   if you move any of these files to other locations.

   emxkill.exe is written by Garey Smiley <garey@slink.com>
   ndc.cmd is written by Bjørn Mork <bjorn@mork.no>

   CMD file changed so it can work with Bind 8.2.4
   Bind 8.2.4 won't run correctly in detached mode. 
   With the util NoList you can hide the named.exe window completely. 
   http://hobbes.nmsu.edu/pub/os2/util/system/nolist2a.zip to download it.
   
   You can use ndc3.cmd to start/stop Bind and Nolist automatically hides the window
   Therefore it will be started bij ndc.exe
   changes by Peter Lindeman <peter@lindeman.org>
   
*/



kill  = '@emxkill'
named = '@ndc.exe -c 127.0.0.1/101 start -c '||value('ETC',,'OS2ENVIRONMENT')||'\named.conf 2>nul'

hide = '@nolist.exe /r '

PIDFILE = value('ETC',,'OS2ENVIRONMENT')||'\named.pid'

parse upper arg command
signal on halt

if (stream(PIDFILE,'c','query exist')\='') then do
	pid = strip(linein(PIDFILE))
	rc = stream(PIDFILE, 'c', 'close')
	do while (queued()>0)
		pull .				/* Empty the queue	*/
	end
	'@pstat /P:'||d2x(pid)||' 2>nul |find /i /c "named.exe"|rxqueue'
	pull tmp
	RUNNING=strip(tmp)
	if (RUNNING=0) then PS='named (pid 'pid'?) not running'
	else PS='named is running (pid 'pid')'
end
else do
	PS='named (no pid file) not running'
	RUNNING=0
end


if ((arg() = 0)|(strip(command) = '')) then signal USAGE

select
	when (command = 'START') then nop
	when (command = 'STOP') then nop
	when (command = 'RESTART') then nop
otherwise
	if (RUNNING=0) then do 
		say PS
		exit 1
	end
end  /* select */

call RxFuncAdd'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
call SysLoadFuncs

select

	when (command = 'STATUS') then say PS
	when (command = 'DUMPDB')

		then do 

			kill '2' pid	/* SIGINT */

			say 'Dumping Database'

		end

	when (command = 'RELOAD')

		then do 

			kill '1' pid	/* SIGHUP */

			say 'Reloading Database'

		end

	when (command = 'STATS')

		then do 

			kill '4' pid	/* SIGILL */

			say 'Dumping Statistics'

		end

	when (command = 'TRACE')

		then do 

			kill '16' pid	/* SIGUSR1 */

			say 'Trace Level Incremented'

		end

	when (command = 'NOTRACE')

		then do 

			kill '17' pid	/* SIGUSR2 */

			say 'Tracing Cleared'

		end

	when (command = 'QUERYLOG')|(command = 'QRYLOG')

		then do 

			kill '20' pid	/* SIGWINCH */

			say 'Query Logging Toggled'

		end

	when (command = 'START')
    		then if (RUNNING=1) then
			say 'ndc: start: named (pid 'pid') already running'
		else do
			call SysFileDelete PIDFILE
			named
			
			if (stream(PIDFILE,'c','query exist')\='') then do
				pid = strip(linein(PIDFILE))
				rc = stream(PIDFILE, 'c', 'close')
				do while (queued()>0)
					pull .				/* Empty the queue	*/
				end
			end

			pidtohide = pid - 1 /* pidnr to hide has to be 1 lower */
			hide pidtohide ' 2 >nul'
			
			say 'Name server started'
		end
	when (command = 'STOP')
		then if (RUNNING=0) then
			say 'ndc: stop: named not running'
		else do
			kill '15' pid	/* SIGTERM */
			
			call SysFileDelete PIDFILE
			say 'Name Server Stopped'
		end
	when (command = 'RESTART')
		then do
     			if (RUNNING=1) then do
				kill '15' pid	/* SIGTERM */
				
			end
			call SysFileDelete PIDFILE
			named
			
			say 'Name Server Restarted'
		end /* do */
	otherwise

		signal USAGE

end	/* select */

return 0


USAGE:
 say

 say 'usage: ndc (status|dumpdb|reload|stats|trace|notrace|qrylog|start|stop|restart)'

 say

 exit 1

halt:
 say 'ndc aborted'
 exit 1
