#!/usr/bin/env python
import sys
import os


args = ""
for i in range(1,len(sys.argv)):
    args += " " + sys.argv[i]
    
pythonPath = None
if(os.path.isfile(os.path.expanduser("~/.ravegen/installationPath"))):
    installationFile = open(os.path.expanduser("~/.ravegen/installationPath"), 'r')
    line = installationFile.readline().rstrip('\n')
    installationFile.close()
    
    pythonPath = line
else:
    command = "pip show ravegen > .tempPythonFile"
    os.system(command)
    tempFile = open(".tempPythonFile", 'r')
    for line in tempFile:
        tokens = line.split(" ")
        tokens[-1] = tokens[-1].rstrip('\n')
        if(tokens[0] == "Location:"):
            pythonPath = tokens[1]
            break
    tempFile.close()
    command = "rm -f .tempPythonFile"
    os.system(command)
    if pythonPath:
        command = "mkdir -p " + os.path.expanduser("~/.ravegen")
        os.system(command)
        command = "echo '" + pythonPath + "' > " + os.path.expanduser("~/.ravegen/installationPath")
        os.system(command)

if pythonPath == None:
    print("Something is wrong with installation")
    sys.exit()

command = "python " + pythonPath + "/ravegen/main.py " + args

os.system(command)

