#!/usr/bin/env python

import os
import sys

cwd = os.getcwd()

try:
    sys.argv.remove('--amber')
    with_amber_tar = True
except ValueError:
    with_amber_tar = False

try:
    sys.argv.remove('--beta')
    beta_release = True
except ValueError:
    beta_release = False

if with_amber_tar:
    pytraj_dir = os.path.abspath('./AmberTools/src/pytraj/')
else:
    pytraj_dir = './'

os.chdir(pytraj_dir)

os.system('git clean -fdx .')

# change to release status 
with open('scripts/base_setup.py', 'r') as fh:
    old_content = fh.read()

    if beta_release:
        new_content = old_content.replace("'.dev1+' + GIT_REVISION[:7]",
                                          "'-beta'")
    else:
        new_content = old_content.replace('ISRELEASED = False', 'ISRELEASED = True') 

with open('scripts/tmp.py', 'w') as fh:
    fh.write(new_content)

os.system('mv scripts/tmp.py scripts/base_setup.py')
os.system('python scripts/cythonize.py')

if not with_amber_tar:
    # upload to pip
    os.system('python setup.py sdist')
    # restore to ISRELEASED = False
    # os.system('git checkout scripts/base_setup.py')

os.chdir(cwd)
