#! /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 FOR EXCITON EXTENSION'
PROGDESCR = 'Updates the state file for singlets and triplets +'
VOTCAHEADER = '''\
==================================================
========   VOTCA (http://www.votca.org)   ========
==================================================

{progtitle}

please submit bugs to bugs@votca.org 
xtp_update_exciton, 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_update_exciton',
    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 segments ADD COLUMN UnXnNs REAL;')
	port.execute('ALTER TABLE segments ADD COLUMN UnXnNt REAL;')
	port.execute('ALTER TABLE segments ADD COLUMN UxNxXs REAL;')
	port.execute('ALTER TABLE segments ADD COLUMN UxNxXt REAL;')
	port.execute('ALTER TABLE segments ADD COLUMN UxXnNs REAL;')
	port.execute('ALTER TABLE segments ADD COLUMN UxXnNt REAL;')
	port.execute('ALTER TABLE segments ADD COLUMN eSinglet REAL;')
	port.execute('ALTER TABLE segments ADD COLUMN eTriplet REAL;')
	port.execute('ALTER TABLE segments ADD COLUMN has_s REAL;')
	port.execute('ALTER TABLE segments ADD COLUMN has_t REAL;')
	port.execute('ALTER TABLE segments ADD COLUMN occPs REAL;')
	port.execute('ALTER TABLE segments ADD COLUMN occPt REAL;')
	port.execute('UPDATE segments SET UnXnNs=0,UnXnNt=0,UxNxXs=0,UxNxXt=0,UxXnNs=0,UxXnNt=0,eSinglet=0,eTriplet=0,has_s=0,has_t=0,occPs=-1,occPt=-1;')




except:
	print "'segments' table already up-to-date"
	
try:
	port.execute('ALTER TABLE pairs ADD COLUMN lOs REAL;')
	port.execute('ALTER TABLE pairs ADD COLUMN lOt REAL;')
	port.execute('ALTER TABLE pairs ADD COLUMN has_s REAL;')
	port.execute('ALTER TABLE pairs ADD COLUMN has_t REAL;')
	port.execute('ALTER TABLE pairs ADD COLUMN rate12s REAL;')
	port.execute('ALTER TABLE pairs ADD COLUMN rate21s REAL;')
	port.execute('ALTER TABLE pairs ADD COLUMN rate12t REAL;')
	port.execute('ALTER TABLE pairs ADD COLUMN rate21t REAL;')
	port.execute('ALTER TABLE pairs ADD COLUMN Jeff2s REAL;')
	port.execute('ALTER TABLE pairs ADD COLUMN Jeff2t REAL;')
	port.execute('UPDATE pairs SET IOs=0,IOt=0,rate12s=0,rate12t=0,rate21s=0,rate21t=0,Jeff2s=0,Jeff2t=0,has_s=0,has_t=0;')
except:
	print "'pairs' table already up-to-date"

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

