#!/usr/bin/env luajit

--- Test script for polygLua. Test 'em all!
-- @script polygLua_test

if type(jit) == 'table' then
  print(arg[0] .. ' is being run by ' .. jit.version .. ' under ' .. jit.os .. ' on a ' .. jit.arch .. '.  Lua version ' .. _VERSION)
else
  print(arg[0] .. ' is being run by Lua version ' .. _VERSION)
end


local _ = require 'polygLua'


C'Starting from the top.'
E'Starting from the top.'
W'Starting from the top.'
T'Starting from the top.'
I'Starting from the top.'
D'Starting from the top.'

Help = [[
This is part of the blah blah blah...
]]

Version = '0.0 crap'

-- our options
local options =
{
    [''] =	{help = 'No actual command, do the default thing.',
	func = function(self, options, a, args, i)
	    I'Printing options table - '
	    P(_.table2string(options, 'options'))
	end
    },
    start =	{help = 'Command to start the service, for Sys V init.', value = 'blah'},
--    restart = {start},
--    ['force-reload'] = {start},
    status =	{help = 'Command to check the status of the service, for Sys V init.',},
    stop =	{help = 'Command to stop the service, for Sys V init.',},
    install =
    {
	help = 'Command to install polygLua.lua',
	func = function(self, options, a, args, i)
	    local w = _.who
	    local d = _.pwd
	    if 'root' ~= w then
		E'Need to be root user to install.'
	    else
		P('not INSTALLING!!!')
--[=[
		P('INSTALLING!!!')
		__[[
#!/bin/bash
		    echo "Installing polygLua"
		    ./polygLua.lua -vvvv
		]]:log():show():Do()
]=]
	    end
	    os.exit(0)
	end
    },
}
options['restart'] = options.start
options['force-reload'] = options.start

P(_.table2string(arg, 'arg'))
P('start = ' .. options.start.value)
_.parse(arg, options, 'polygLua_test')
P('start = ' .. options.start.value)
--P('stop = ' .. options.stop.value)

C'After parse.'
E'After parse.'
W'After parse.'
T'After parse.'
I'After parse.'
D'After parse.'


__[[
    echo -n "This'll print forking later.  "
    date
    sleep 2
]]:log():show():fork('echo -n "later is "; date')
P('')
P('')


local GUI = 'qjackctl'
if _.runnable'catia' then GUI = 'catia' end
P('' .. GUI)
P''

for i,l in ipairs(__'uname -a\nfree -h':log():Do().lines) do
    P(l)
end
__'uname -a':log():Then'free -h':show():Do()
__{'uname -a', 'free -h'}:log():show():Do()
P''


P(__[[
#!/bin/NoSuchCommand
    echo "Should not print."
]]:log():show():Do().status)

P(__[[
#!/usr/bin/env bash
    echo "G'day world from bash."
    exit 42
]]:log():show():Do().status)

P(__[[
#!/usr/bin/env tcc -run
    #include <tcclib.h>

    int main()
    {
	printf("G'day world from C.\n");
	exit(42);
    }
]]:log():show():Do().status)

P(__[[
#!/usr/bin/env tcc -run
    #include <tcclib.h>

    int main()
    {
	printf("G'day world from C.\n");
	return 42;
    }
]]:log():show():Do().status)

P(__[[
#!/usr/bin/env luajit
    print("G'day " .. "world " .. [=[from]=] .. ' Lua.')
    os.exit(42)
]]:log():show():Do().status)

__[[
#!/usr/bin/env perl
    print "G'day world from perl.\n";
]]:log():show():Do()

__[[
#!/usr/bin/env php
<?php
    print("G'day world from php.\n");
]]:log():show():Do()

-- Note no indent for Python, coz whitespace is significant.
__[[
#!/usr/bin/env python3
print("G'day world from python.")
]]:log():show():Do()

__[[#!/usr/bin/env ruby
    puts "G'day world from ruby."
]]:log():show():Do()
P''


P(__'true':log():show():Do().status)
P(__'false':log():show():Do().status)
P(__'exit 42':log():show():Do().status)
P(__'return 42':log():show():Do().status)
P(__'command NoSuchCommand':log():show():Do().status)
P''


--__'qpdfview':forkOnce()
--__'qpdfview':forkOnce()


__'date':log():show():Do()
-- Should get that "later" thing printed while the sleep is happening.
__'date; sleep 5; date':log():show():Do()
