#! /usr/bin/python2
#
# Copyright 2009-2016 The VOTCA Development Team (http://www.votca.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

VERSION='1.4.1 '
import sys
import os
import getpass
import socket
import commands as cmds
import numpy as np
import xml.dom.minidom as xmld
import time
import argparse
import re
import math
import sqlite3

PROGTITLE = 'THE VOTCA::XTP STATE-FILE UPDATER'
PROGDESCR = 'Updates the state file +'
VOTCAHEADER = '''\
==================================================
========   VOTCA (http://www.votca.org)   ========
==================================================

{progtitle}

please submit bugs to bugs@votca.org 
xtp_update, version {version}

'''.format(version=VERSION, progtitle=PROGTITLE)

def okquit(what=''):
	if what != '': print what
	sys.exit(0)
def xxquit(what=''):
	if what != '':
		cprint.Error("ERROR: {what}".format(what=what))
	sys.exit(1)
def sysexe(cmd, silent=False, devfile='/dev/null'):
	if VERBOSE: print "{0}@{1}$ {2}".format(USER, HOST, cmd)
	if silent: cmd += ' >> {0} 2>> {0}'.format(devfile)
	cdx = os.system(cmd)
	#SYSCMDS.write('{cmd} = {cdx}\n'.format(cmd=cmd, cdx=cdx))
	return cdx

# =============================================================================
# PROGRAM OPTIONS
# =============================================================================

class XtpHelpFormatter(argparse.HelpFormatter):
	def _format_usage(self, usage, action, group, prefix):
		return VOTCAHEADER
		
progargs = argparse.ArgumentParser(prog='xtp_testsuite',
    formatter_class=lambda prog: XtpHelpFormatter(prog,max_help_position=70),
	description=PROGDESCR)
	
progargs.add_argument('-f', '--file',
    dest='sqlfile',   
    action='store',
    required=False,
    type=str,
	default='',
    help='State file to update.')
    
OPTIONS = progargs.parse_args()
if OPTIONS.sqlfile == '':
	progargs.print_help()
	okquit("\nQuit here, because: State file not set (option -f/--file)")

# =============================================================================
# SQLITE3 EXECUTION
# =============================================================================

conn = sqlite3.connect(OPTIONS.sqlfile)
port = conn.cursor()

try:
	port.execute('ALTER TABLE pairs ADD COLUMN type INT;')
except:
	print "'pair' table already up-to-date"
	
try:
	port.execute('''CREATE TABLE superExchange 
           (_id INTEGER PRIMARY KEY AUTOINCREMENT,
            frame      INT NOT NULL,
            top        INT NOT NULL,
            type      TEXT NOT NULL)''')
except:
	print "'superExchange' table already up-to-date"

conn.commit()
conn.close()
sys.exit(0)

