#!/usr/bin/env python3
# GPL. (C) 2007-2009 Paolo Patruno.

# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
# 

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'autoradio.settings'
from django.conf import settings

from django.core import management
import autoradio.settings
import autoradio.autoradio_config
import sys, optparse, autoradio.daemon
from autoradio import  _version_


def initdb(cwd):
    p = optparse.OptionParser(usage="usage: %prog [options]",version="%prog "+_version_)
    p.add_option("--syncdb", action="store_true",dest="syncdb", help="initialize autoradio DB (default %default)", default=False)
    p.add_option("--changeuser", action="store_true",dest="changeuser", help="change user to the user in config file (default %default)", default=False)
    (options, args) = p.parse_args()


    import django
    django.setup()

    if (not options.syncdb):
        p.print_help()


    if (options.changeuser):
        dae=autoradio.daemon.Daemon()
        dae.switchuser(user=autoradio.autoradio_config.user,group=autoradio.autoradio_config.group,env=None)
        #os.chdir(cwd)

    if (options.syncdb):
        #management.call_command("migrate",no_initial_data=True )
        management.call_command("migrate" )


if __name__ == '__main__':

    cwd=os.getcwd()

    sys.exit(initdb(cwd))  # (this code was run as script)
