#!/usr/bin/python
import argparse
import os
import subprocess
parser = argparse.ArgumentParser()
parser.add_argument('nv', action="store", choices=('itt', 'uat', 'prod'))
parser.add_argument('path', action="store")
try:
        args=parser.parse_args()
        uenv = args.nv
        fpath = args.path
except IOError as msg:
        parser.error(str(msg))
INT = ("el3934.bc","el3922.bc","el3922.bc")
ACC = ("el3988.ebc.local","el3987.ebc.local","el3987.ebc.local")
PRD = ("el4190.ebc.local","el4204.ebc.local","el4204.ebc.local")
myfile = open(fpath,'r')
fread = myfile.read()
if os.path.exists(fpath) and uenv == 'itt':
        newtext = fread.replace('${couchbase}',INT[0])
        newfile = open(fpath, 'w')
        newfile.write(newtext)
        newfile.close()
        myfile1 = open(fpath,'r')
        gread = myfile1.read()
        newtext1 = gread.replace('${greeter-hook}',INT[1])
        newwrite1 = open(fpath,'w')
        newwrite1.write(newtext1)
        newwrite1.close()
        myfile2 = open(fpath, 'r')
        hread = myfile2.read()
        newtext2 = hread.replace('${authorization-profiles}', INT[2])
        newwrite2 = open(fpath, 'w')
        newwrite2.write(newtext2)
        newwrite2.close()
elif os.path.exists(fpath) and uenv == 'uat':
        newtext = fread.replace('${couchbase}', ACC[0])
        newfile = open(fpath, 'w')
        newfile.write(newtext)
        newfile.close()
        myfile1 = open(fpath, 'r')
        gread = myfile1.read()
        newtext1 = gread.replace('${greeter-hook}', ACC[1])
        newwrite1 = open(fpath, 'w')
        newwrite1.write(newtext1)
        newwrite1.close()
        myfile2 = open(fpath, 'r')
        hread = myfile2.read()
        newtext2 = hread.replace('${authorization-profiles}', ACC[2])
        newwrite2 = open(fpath, 'w')
        newwrite2.write(newtext2)
        newwrite2.close()
elif os.path.exists(fpath) and uenv == 'prod':
        newtext = fread.replace('${couchbase}', PRD[0])
        newfile = open(fpath, 'w')
        newfile.write(newtext)
        myfile1 = open(fpath, 'r')
        gread = myfile1.read()
        newtext1 = gread.replace('${greeter-hook}', PRD[1])
        newwrite1 = open(fpath, 'w')
        newwrite1.write(newtext1)
        myfile2.close()
        myfile2 = open(fpath, 'r')
        hread = myfile2.read()
        newtext2 = hread.replace('${authorization-profiles}', PRD[2])
        newwrite2 = open(fpath, 'w')
        newwrite2.write(newtext2)
        myfile2.close()
#subprocess.call(["python ~/scripts/sgwplaceholders.py itt /tmp/sync_gateway.json"], shell=True)
#sys.exit()

