#!/usr/bin/env python
# -*- coding: ISO-8859-15 -*-
#
# Copyright (C) 2005-2007 David Guerizec <david@guerizec.net>
#
# Last modified: 2006 Jul 28, 01:29:47 by david
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

import sys, os, pwd
from optparse import OptionParser


from sshproxy import __version__

usage = """
  %prog [--config-dir CFGDIR]
      Normal run (in the foreground).
  
  %prog [--config-dir CFGDIR] --daemon [--user USER] [--pid PIDFILE]
      Run daemonized (in the background).
  
"""

version = "%prog " + __version__

parser = OptionParser(usage=usage, version=version)

##### runtime options
parser.add_option("-d", "--daemon", dest="daemon", default=False,
                    help="start in background.", action="store_true")
parser.add_option("-p", "--pid", dest="pid", default='/var/run/sshproxyd.pid',
                    help="set pid file (default: %default). "
                         "This option is only effective with --daemon.",
                    metavar="PIDFILE")
parser.add_option("-u", "--user", dest="user", default=None,
                    help="start as user USER, usefull with --daemon.",
                    metavar="USER")
parser.add_option("-c", "--config-dir", dest="config", default=None,
                    help="configuration directory (default: ~/.sshproxy).",
                    metavar="CFGDIR")

(options, args) = parser.parse_args()

#configdir = os.path.join(os.getcwd(), (options.config
#                                or os.environ.get('SSHPROXY_CONFIG', '')))
#os.environ['SSHPROXY_CONFIG'] = configdir
os.environ['SSHPROXY_CONFIG'] = (options.config
                                or os.environ.get('SSHPROXY_CONFIG', ''))
if not os.environ['SSHPROXY_CONFIG'] and options.user:
    os.environ['SSHPROXY_CONFIG'] = os.path.join(pwd.getpwnam(options.user)[5],
                                                    '.sshproxy')

#exclusive_options = ('daemon',)
#nb_opt = 0
#for opt in exclusive_options:
#    if getattr(options, opt.replace('-', '_'), None):
#        nb_opt += 1
#
#if nb_opt > 1:
#    print "Options %s are mutually exclusives." % ', '.join(exclusive_options)
#    sys.exit(1)


if os.getuid() == 0:
    if not options.user:
        print "Could not run as root."
        print "Please give an unpriviledged username with --user."
        sys.exit(1)
else:
    if options.user:
        print "Warning: the --user option has no effect if not run as root."

from sshproxy.daemon import run_daemon
run_daemon(options.daemon, options.user, options.pid)

