#!/usr/bin/env python
#
# Copyright (C) 2002 Gre7g Luterman <gre7g@wolfhome.com>
#
# This file is part of TMDA.
#
# TMDA is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.  A copy of this license should
# be included in the file COPYING.
#
# TMDA is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with TMDA; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

"""Pending queue manipulation tool.

Usage:  %(Program)s [OPTIONS]

Where:
    -c <file>
    --config-file <file>
       Specify a different configuration file other than ~/.tmda/config.

    -h
    --help
       Print this help message and exit.

    -i <path>
    --install-prefix <path>
       Specify path to tmda-cgi.py other than the currect directory.

    -m system-wide|single-user|no-su
    --mode system-wide|single-user|no-su
       Specify an operating mode.

    -n
    --no-su
       Compile a CGI to run in no-su mode.  Forces option "-m no-su".

    -t <file>
    --target <file>
       Compile as a file other than ./tmda-cgi.

By default, this program compiles tmda-cgi in the current directory and sets
the program to run as the user who compiled it.  If that user is root, the
resulting CGI will run in system-wide mode.  Otherwise it will run in single-
user mode. You may optionally specify the -n option to compile for no-su mode.

The -i and -m options are seldom needed.  These options are for the rare cases
when a user other than root needs to compile the program for use in system-
wide mode.  For the resulting code to run correctly, root will have to chown
the resulting program.

The target may be specified on the command line with the -t option.

Use the -c option to specify a different location for your TMDA configuration
file.  You may use a "~" anywhere in the path to specify "the current user".
For example:

    %(Program)s -c /var/tmda/~/config

Will look for configuration files at /var/tmda/<user>/config instead of the
usual ~<user>/.tmda/config.

If no ~ is specified, compile will make a "best guess".
"""

import getopt
import os.path
import string
import sys

Program = sys.argv[0]
Target  = "tmda-cgi"
Perm    = 04755
Path    = "."
if os.geteuid():
  Mode = "single-user"
else:
  Mode = "system-wide"

def Usage(Code, Msg=""):
  print __doc__ % globals()
  if Msg: print Msg
  sys.exit(Code)

try:
  Opts, Args = getopt.getopt(sys.argv[1:], "c:i:m:nht:",
    ["config-file=", "help", "install-prefix=", "mode=", "no-su", "target="])
except getopt.error, Msg:
  Usage(1, Msg)

for Opt, Arg in Opts:
  if Opt in ("-h", "--help"):
    Usage(0)
  elif Opt in ("-c", "--config-file"):
    os.environ["TMDARC"] = Arg
  elif Opt in ("-i", "--install-prefix"):
    Path = Arg
  elif Opt in ("-t", "--target"):
    Target = Arg
  elif Opt in ("-m", "--mode"):
    if not Arg in ("system-wide", "single-user", "no-su"):
      Usage(1, "Valid modes are system-wide, single-user, and no-su")
    Mode = Arg
    if Arg == "no-su": Perm = 0755
  elif Opt in ("-n", "--no-su"):
    Perm = 0755
    Mode = "no-su"

Path = os.path.abspath(Path)

# Create dirs.h
F = open("dirs.h", "w")
F.write("""#define PYTHON "%s"
#define INSTALL "%s"
#define MODE "%s"
""" % (sys.executable, Path, Mode))
if os.environ.has_key("TMDARC"):
  TMDARC = os.environ["TMDARC"]
  if TMDARC.find("~") < 0:
    TMDARC = string.replace(TMDARC, "/%s/" % os.environ["USER"], "/~/")
    print """NOTE:
tmda-cgi will look for config files at: %s
Where <user> will be replaced by the user's login name.
""" % string.replace(TMDARC, "/~/", "/<user>/")
  F.write("""#define TMDARC "%s"
""" % TMDARC)
F.close()

# Remove any old tmda-rc
try:
  os.unlink(Target)
except: pass

# Compile tmda-cgi
os.system("gcc tmda-cgi.c -o %s" % Target)

# Remove dirs.h
os.unlink("dirs.h")

# Set permissions
os.chmod(Target, Perm)

print "Compilation done."
