import argparse
import subprocess
import re
import logging
import os
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

parser = argparse.ArgumentParser(description='Build Casa in a container')
parser.add_argument("-c", "--container", type=str, required=True,
                    help='Source code location f.e. /home/<user>/source')
parser.add_argument("-u", "--user", type=str, required=True, help='user')
parser.add_argument("-d", "--uid", type=str, required=True, help='uid')
parser.add_argument("--testtools", required=False, action='store_true', help='Run casatools tests')
parser.add_argument("--testtasks", required=False, action='store_true', help='Run casatasks tests')
parser.add_argument("-b", "--buildtarget", type=str, required=False, help='Use genmake to build casatools with. Default is bdist_wheel.')
parser.add_argument("-p", "--pythonversion", type=str, required=True, help='Python version. For example 3.6')
parser.add_argument('--debug', action='store_true', help='Print debug messages')

args = parser.parse_args()

logger = logging.getLogger()
if args.debug:
    logger.setLevel(logging.DEBUG)

logger.debug('Argument values:')
logger.debug(args.container)
logger.debug(args.user)
logger.debug(args.uid)
logger.debug(args.buildtarget)
logger.debug(args.pythonversion)

container = args.container
user = args.user
uid = args.uid
pythonversion = args.pythonversion
testtools = args.testtools
testtasks = args.testtasks

tools_target = " --relwithdebinfo --stripsyms bdist_wheel"
tasks_target = "bdist_wheel"
shell_target = "bdist_wheel"

if args.buildtarget =="genmake":
    tools_target = "genmake"
    tasks_target = "build"
    shell_target = "build"

mypath = os.path.dirname(os.path.abspath(__file__))
logger.debug("Script path: " + mypath)

def exec_as_shell(cmd):
    print("Executing: " + cmd)
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT, shell=True)
    #stdout, stderr = process.communicate()
    #logger.debug(stdout)
    while True:
        output = process.stdout.readline().decode('UTF-8')
        if output == '' and process.poll() is not None:
            break
        if output:
            print (output.strip())
    rc = process.poll()
    return rc

# Build a list of commands to execute
cmds = []
cmds.append('docker exec ' + container +' /bin/bash -c "useradd -u ' + uid + ' ' + user + '"')
cmds.append('docker exec -u ' + user + " " + container +' /bin/bash -c "mkdir ~/.casa && mkdir -p ~/.config/pip"')
cmds.append('docker cp ' + mypath + "/config/config.py " + container +':/home/' + user +'/.casa/config.py')
cmds.append('docker cp ' + mypath + "/config/pip.conf " + container +':/home/' + user +'/.config/pip/pip.conf')
cmds.append('docker exec -u ' + user + " " + container +' /bin/bash -c "ccache -M 50G"')
cmds.append('docker exec -u ' + user + " " + container +' /bin/bash -c "python'+ pythonversion + ' -m venv ~/venv" ')
cmds.append('docker exec -u ' + user + " " + container +' /bin/bash -c "source ~/venv/bin/activate &&  pip install --index-url=https://pypi.org/simple --upgrade pip wheel"')
cmds.append('docker exec -u ' + user + " " + container +' /bin/bash -c "source ~/venv/bin/activate &&  pip install --index-url=https://pypi.org/simple --upgrade packaging certifi auditwheel numpy==1.19.5 scipy matplotlib patchelf"')
cmds.append('docker exec -u ' + user + " " + container +' /bin/bash -c "source ~/venv/bin/activate &&  cd /source/casa6/casatools/ && autoconf && ./configure && python setup.py ' + tools_target + '"')
if tools_target=="genmake":
    cmds.append('docker exec -u ' + user + " " + container +' /bin/bash -c "export CCACHE_DIR=/ccache && cd /source/casa6/casatools/ && make -j4' + '"')
else:
    cmds.append('docker exec -u ' + user + " " + container + " /bin/bash -c 'source ~/venv/bin/activate && cd /source/casa6/casatools/dist && auditwheel repair `ls -t *.whl | head -1`'")
cmds.append('docker exec -u ' + user + " " + container +' /bin/bash -c "source ~/venv/bin/activate && export PYTHONPATH=/source/casa6/casatools/build/lib.linux-x86_64-'+ pythonversion + '/ && cd /source/casa6/casatasks/ && python'+ pythonversion + ' setup.py ' + tasks_target + '"')
cmds.append('docker exec -u ' + user + " " + container +' /bin/bash -c "source ~/venv/bin/activate && export PYTHONPATH=/source/casa6/casatools/build/lib.linux-x86_64-'+ pythonversion + '/:/source/casa6/casatasks/build/lib.linux-x86_64-'+ pythonversion + '/ && cd /source/casashell/ && python setup.py ' + tasks_target + '"')
cmds.append('docker exec -u ' + user + " " + container +' /bin/bash -c "mkdir /source/extralibs"')
cmds.append('docker exec -u ' + user + " " + container +' /bin/bash -c "cp /usr/lib64/libgfortran.so.3 /source/extralibs"')

# Build casatestutils
if testtools == True or testtasks != None:
    cmds.append('docker exec -u ' + user + " " + container +' /bin/bash -c "source ~/venv/bin/activate && export PYTHONPATH=/source/casa6/casatools/build/lib.linux-x86_64-'+ pythonversion + \
                 ':/source/casa6/casatasks/build/lib.linux-x86_64-'+ pythonversion +'/ && cd /source/casa6/casatestutils && python setup.py bdist_wheel"')

if testtools == True:
    cmds.append('docker exec -u ' + user + " " + container +' /bin/bash -c "source ~/venv/bin/activate && export PYTHONPATH=/source/casa6/casatools/build/lib.linux-x86_64-'+ pythonversion + \
    ':/source/casa6/casatestutils/build/lib'\
     '/ && cd /source/casa6/casatools/tests && python run.py"')
if testtasks == True:
    cmds.append('docker exec -u ' + user + " " + container +' /bin/bash -c "source ~/venv/bin/activate && export PYTHONPATH=/source/casa6/casatools/build/lib.linux-x86_64-'+ pythonversion + \
                 ':/source/casa6/casatestutils/build/lib'\
                 ':/source/casa6/casatasks/build/lib.linux-x86_64-' + pythonversion +'/ && cd /source/casa6/casatasks/tests && python run.py"')

# Execute commands
for cmd in cmds:
    exec_as_shell(cmd)