#!/usr/bin/python                                                                                                                                      

# Copyright (C) 2011 by CAMd, DTU
# Please see the accompanying LICENSE file for further information.                                                                                    

import os
import sys
import tempfile


if len(sys.argv)>1 and sys.argv[1]=="--create-settings":
    repo = tempfile.mkdtemp()
    if not os.environ.has_key("CMR_SETTINGS_FILE") or not os.path.exists(os.environ["CMR_SETTINGS_FILE"]):
        os.environ["CMR_SETTINGS_FILE"] = os.path.join(repo, "cmr-settings-file")
        f = open(os.environ["CMR_SETTINGS_FILE"], "w")
        f.write("[user]\nauto_add_to_db=False\nplugin_paths=\ndefault_privacy=640\nlocation=cmr-test\n")
        f.close()
        created = True
    else:
        created = False
    
    from cmr.static import known_organizations
    print "You are about to create the CMR settings file. Please note that if some settings exist already, then they"
    print "will be overwritten. To continue answer with yes, otherwise answer with no and press enter."
    answer = raw_input("")
    if not answer =="yes":
        print "Aborted. You didn't answer with 'yes'."
        print "You can always (re-)create the settings file with:"
        print "$> cmr --create-settings"
    else:
        print "Please choose your group (this will be added as default to the db-file):"
        dct = {"plugin_paths":[],
             "default_privacy":"640",
             "auto_add_to_db":"False",
        }
        
        keys = known_organizations.keys()
        print str(0)+": Don't choose now. (CMR will exit.)"
        if len(keys)>0:
            for a in range(1, len(keys)+1):
                print str(a)+": "+known_organizations[keys[a-1]]
            print str(a+1)+": other"
            choice=None
        else:
            choice=0
        while choice is None:
            message = "Please select your location: "
            choice = raw_input(message)
            try:
                choice = int(choice)
                if choice<0 or choice>len(keys)+1:
                    choice=None
            except ValueError:
                choice=None
        if choice==0:
            raise Exception("Choosing settings aborted.")
        elif choice==len(keys)+1:
            location = None
            while location is None:
                message = "Please enter your group name: "
                location = raw_input(message)
                if len(location)==0:
                    location = None
                else:
                    dct["location"] = location
        else:
            location = known_organizations[keys[choice-1]]  
            dct["location"] = location
        
        from cmr.cmr_settings_file import CMRSettingsFile
        if created:
            os.environ.pop("CMR_SETTINGS_FILE")
    
        CMRSettingsFile(data = {"user":dct})
        print "\nSettings file created."
        if not os.environ.has_key("CMR_REPOSITORY"):
            print "Please continue now with the creation of a default cmr repository if you haven't done this before,"
            print "as described in https://wiki.fysik.dtu.dk/cmr/install/install.html#installation-with-a-package-manger"
        print "\nYou can check the current cmr setup with the command"
        print "  cmr --xversion"
else:
    from cmr.ui.command_line import main
    main()
    
