#!/usr/bin/python

import os
import os.path
import sys

def main():
    if len(sys.argv) != 2:
        print("Usage: %s path_to_build" % sys.argv[0])
        return
    imppath= os.path.split(os.path.split(sys.argv[0])[0])[0]
    try:
        imppath= os.path.relpath(imppath, sys.argv[1])
    except:
        print "Platform does not support os.path.relpath, links will be made using absolute paths. Sorry. You can replace the SConstruct and scons_tools links and the repository line in the config.py with relative paths by hand."
    path_to_build= sys.argv[1]
    print "IMP source found at ", imppath
    print "Setting up IMP build directory at ", path_to_build
    if os.path.isfile("SConstruct"):
        print "SConstruct file already exists in this directory."
        return
    os.symlink(os.path.join(imppath, "SConstruct"), os.path.join(path_to_build, "SConstruct"))
    os.symlink(os.path.join(imppath, "scons_tools"), os.path.join(path_to_build,  "scons_tools"))
    config= open(os.path.join(os.path.split(sys.argv[0])[0], "sample_config.py"), "r").read()
    config=config.replace("path_to_source", imppath)
    open(os.path.join(path_to_build,"config.py"), "w").write(config)

if __name__ == '__main__':
    main()
