text: #!/bin/sh
  # -*- python -*-
  
  ################################################################################
  # This file is python 2/3 bilingual. 
  # The line """:" starts a comment in python and is a no-op in shell.
  """:"
  # Shell code to find and run a suitable python interpreter.
  for cmd in python3 python python2; do
     command -v > /dev/null $cmd && exec $cmd $0 "$@"
  done
  
  echo "Error: Could not find a valid python interpreter --> exiting!" >&2
  exit 2
  ":""" # this line ends the python comment and is a no-op in shell.
  ################################################################################
  
  #-----------------------------------------------------------------------
  # XALT: A tool that tracks users jobs and environments on a cluster.
  # Copyright (C) 2013-2014 University of Texas at Austin
  # Copyright (C) 2013-2014 University of Tennessee
  # 
  # This library is free software; you can redistribute it and/or modify
  # it under the terms of the GNU Lesser General Public License as
  # published by the Free Software Foundation; either version 2.1 of 
  # the License, or (at your option) any later version. 
  #
  # This library 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
  # Lesser  General Public License for more details. 
  #
  # You should have received a copy of the GNU Lesser General Public
  # License along with this library; if not, write to the Free
  # Software Foundation, Inc., 59 Temple Place, Suite 330,
  # Boston, MA 02111-1307 USA
  #-----------------------------------------------------------------------
  
  from __future__ import print_function
  import os, sys, re, getpass, base64
  try:
    import configparser
  except:
    import ConfigParser as configparser
  
  try:
    input = raw_input
  except:
    pass
  
  dirNm, execName = os.path.split(sys.argv[0])
  sys.path.insert(1,os.path.abspath(os.path.join(dirNm, "../libexec")))
  sys.path.insert(1,os.path.realpath(os.path.join(dirNm, "../site")))
  
  import argparse
  
  class CmdLineOptions(object):
    def __init__(self):
      pass
    
    def execute(self):
      parser = argparse.ArgumentParser()
      parser.add_argument("--dbhost",  dest='dbhost',  action="store",  help="db host")
      parser.add_argument("--dbuser",  dest='dbuser',  action="store",  help="db user")
      parser.add_argument("--passwd",  dest='passwd',  action="store",  help="password")
      parser.add_argument("--dbname",  dest='dbname',  action="store",  help="name of db")
  
      args = parser.parse_args()
      
      return args
  class CreateConf(object):
    def __init__(self, args):
      self.__host   = args.dbhost 
      self.__user   = args.dbuser 
      self.__passwd = args.passwd 
      self.__db     = args.dbname 
      
    def __readFromFile(self):
      with open('/data/db_credentials.txt') as file:
        if (not self.__host):   self.__host   = file.readline().strip('\n\r')
        if (not self.__user):   self.__user   = file.readline().strip('\n\r')
        if (not self.__passwd): self.__passwd = file.readline().strip('\n\r')
        if (not self.__db):     self.__db     = file.readline().strip('\n\r')
  
    #def __readFromUser(self):
    #  if (not self.__host):   self.__host   = input("Database host: ")
    #  if (not self.__user):   self.__user   = input("Database user: ")
    #  if (not self.__passwd): self.__passwd = getpass.getpass("Database pass: ")
    #  if (not self.__db):     self.__db     = input("Database name: ")
      
    def __writeConfig(self):
      config=configparser.ConfigParser()
      config.add_section("MYSQL")
      config.set("MYSQL","HOST",self.__host)
      config.set("MYSQL","USER",self.__user)
      config.set("MYSQL","PASSWD",base64.b64encode(self.__passwd.encode()).decode())
      config.set("MYSQL","DB",self.__db)
  
      fn = self.__db + "_db.conf"
  
      f = open(fn,"w")
      config.write(f)
      f.close()
  
    def create(self):
  
      #self.__readFromUser()
      self.__readFromFile()
      self.__writeConfig()
      
  
  
  def main():
    args = CmdLineOptions().execute()
    createConf = CreateConf(args)
    createConf.create()
  
  
  if ( __name__ == '__main__'): main()
  