#!/usr/bin/env python
#
# This file is part of SALI
#
# SALI 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.
#
# SALI 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 SALI.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2010-2013 SURFsara

import sys

try:
    from sali import general
    from sali import server
except ImportError:
    sys.path.append( '../' )

    from sali import general
    from sali import server

if __name__ == '__main__':

    if len( sys.argv ) == 3:
        CFGFILE = sys.argv[ 1 ]
        ACTION = sys.argv[ 2 ]
    else:
        print 'Could not execute command. Usage: %s /path/to/config start|stop|restart|fg|help' % sys.argv[ 0 ]
        sys.exit( 1 )

    cfg = general.Config( CFGFILE )

    if ACTION in [ 'start', 'stop', 'restart' ]:
        dc = general.DaemonCtl( server.sali_server, cfg )

        if ACTION == 'start':
            dc.start()
        elif ACTION == 'stop':
            dc.stop()
        elif ACTION == 'restart':
            dc.restart()
    elif ACTION == 'fg':
        dc = general.DaemonCtl( server.sali_server, cfg, foreground=True )
        dc.start()
    elif ACTION == 'help':
        print( 'Show some nice help' )
