#
#   Copyright 2009 Free Software Foundation, Inc.
#
#  This program 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 3 of the License, or
#  (at your option) any later version.
#
#  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# This is autogenerated by the configure script to yield a version
# with the correct default directories in

import pwd, os, os.path, copy, re, string

def parse_mconfig(mcf):
    """Parse master configuration file if it exists"""
    global Locs
    mcre = re.compile('(\w+)[:=](\S+)')
    try:
        mcfile = open(mcf)
        for l in mcfile:
            item = string.rstrip(l)
            item = re.sub('#.*', '', item)
            m = mcre.search(item)
            if m:
                Locs[m.group(1)] = m.group(2)
        mcfile.close()
    except IOError:
        pass

def findprogonpath(prog):
    """Find where program is on path"""
    for p in string.split(PATH, ':'):
        if not os.path.isabs(p): continue
        pr = os.path.join(p, prog)
        if os.access(pr, os.X_OK):
            return pr
    return prog

# Set up user and group ids

try:
    sp = pwd.getpwnam('@SPOOLUSER@')
    Spooluid = sp.pw_uid
    Spoolgid = sp.pw_gid
except KeyError:
    Spooluid = 50
    Spoolgid = 1

try:
    PATH = os.environ['PATH']
except KeyError:
    PATH = '/bin:/usr/bin:/usr/local/bin'

ppath = os.path.normpath(os.getcwd() + '/..')
PATH += ':' + ppath

# These are the default locations of things - set up by configure

Deflocs = dict(SPOOLDIR='@spooldir@',
               SPROGDIR='@pkgexecdir@',
               SPHELPDIR='@sphelpdir@',
               SPOOLPT='@spoolpt@',
	       datarootdir='@datarootdir@',
	       prefix='@prefix@',
	       exec_prefix='@exec_prefix@',
               masterconf='@sysconfdir@/gnuspool.conf')

# Massage those to get ${name} refs out
dplaces = Deflocs.keys()
mdir = re.compile('(.*)\$\{([^}]*)\}(.*)')
while '$' in string.join(Deflocs.values(),''):
    for d in dplaces:
        v = Deflocs[d]
        m = mdir.search(v)
        if m and m.group(2) in dplaces:
            Deflocs[d] = m.group(1) + Deflocs[m.group(2)] + m.group(3)
                                             
Locs = copy.copy(Deflocs)
parse_mconfig(Deflocs['masterconf'])

Programs = dict(ADDPROG = findprogonpath('gspl-padd'),
                DELPROG = findprogonpath('gspl-pdel'),
                LISTPROG = findprogonpath('gspl-plist'),
                STATPROG = findprogonpath('gspl_pstat'),
                HALTPROG = findprogonpath('gspl-stop'),
                CPLIST = findprogonpath('gspl-cplist'),
                GETSNMP = findprogonpath('gspl-getsnmp'),
                GS = findprogonpath('gs'),
                HPIJS = findprogonpath('hpijs'),
		ULIST = findprogonpath('gspl-ulist'),
		UCHANGE = findprogonpath('gspl-uchange'))
