#!/usr/bin/lua50
-- -*- Lua -*-

-- This file is under the terms of the MIT licence. Do as you will.

-- Process the arg table
function usage()
   info();
   io.stderr:write([[Usage: lua-config50 <options>

  Valid options are:

  --include      Outputs the -I switches needed to find <lua.h> etc.

  --static       Outputs the full path to the static libraries

  --libs         Outputs the -L and -l switches needed to find the library
  --libs-only-L  Outputs only the -L switches
  --libs-only-l  Outputs only the -l switches

  --extralibs    Outputs the -l switches appropriate to the extra libs needed by lua

  Note that --static is mututally exclusive with the --libs* options

  Also, you can specify the following

  --vmonly       Outputs only the switches for finding the VM libraries
  --libonly      Outputs only the switches for finding the standard libraries
  --both         Outputs the switches for both [The default]

  Example:

  gcc `lua-config50 --include` my_prog.c -o my_prog `lua-config50 --libs`

]] );
   os.exit(1);
end

function version()
   io.stdout:write( [[5.0.0
]] );
   os.exit(0);
end

function info()
   io.stdout:write( [[lua-config version 1.10 (c) Daniel Silverstone 2002

lua-config was written for the Debian GNU/Linux project. This version
of lua-config will provide switches appropriate to Lua 5.0

]] );
end

if( table.getn(arg) == 0 ) then
   usage()
end

output_vm      = 1
output_lib     = 1

output_static  = 0
output_libs_l  = 0
output_libs_L  = 0
output_include = 0
output_extras  = 0

table.foreachi( arg,
	 function(n,param)
	    if( param == '--version' ) then
	       version()
	    end
	    if( param == '--help' ) then
	       usage()
	    end
	    if( param == '--include' ) then
	       output_include = 1;
	       return
	    end
	    if( param == '--libs' ) then
	       output_libs_l = 1;
	       output_libs_L = 1;
	       return
	    end
	    if( param == '--libs-only-L' ) then
	       output_libs_L = 1;
	       return
	    end
	    if( param == '--libs-only-l' ) then
	       output_libs_l = 1;
	       return
	    end
	    if( param == '--extralibs' ) then
	       output_extras = 1;
	       return
	    end
	    if( param == '--static' ) then
	       output_static = 1;
	       return
	    end
	    if( param == '--vmonly' ) then
	       output_vm = 1;
	       output_lib = 0;
	       return
	    end
	    if( param == '--libonly' ) then
	       output_lib = 1;
	       output_vm = 0;
	       return
	    end
	    if( param == '--both' ) then
	       output_lib = 1;
	       output_vm = 1;
	       return
	    end
	    io.stderr:write( "Unknown argument ", param );
	    usage();
	 end );

if( (output_extras + output_libs_l + output_libs_L + output_include + output_static) == 0 ) then
   usage()
end

if( (output_static + (output_libs_l or output_libs_L)) > 1 ) then
   usage();
end

outargs = {}

if( output_include == 1 ) then
   table.insert( outargs, "-I/usr/include/lua50" );
end

if( output_libs_L == 1 ) then
   table.insert( outargs, "-L/usr/include" );
end

if( output_libs_l == 1 ) then
   if( output_lib == 1 ) then
      table.insert( outargs, "-llualib50" );
   end
   if( output_vm == 1 ) then
      table.insert( outargs, "-llua50" );
   end
end

if( output_static == 1 ) then
   if( output_lib == 1 ) then
      table.insert( outargs, "/usr/lib/liblualib50.a" );
   end
   if( output_vm == 1 ) then
      table.insert( outargs, "/usr/lib/liblua50.a" );
   end
end

if( output_extras == 1 ) then
   table.insert( outargs, "-lm" );
end

io.stdout:write( outargs[1] );

for i=2,table.getn(outargs) do
   io.stdout:write( " ", outargs[i] );
end

io.stdout:write( "\n" );
