#------------------------------------------------------------------------------
# Originally cxfreeze-postinstall
#   Script run after installation on Windows to fix up the Python location in
# the script as well as create batch files.
#------------------------------------------------------------------------------

import distutils.sysconfig
import glob, os, sys

vars = distutils.sysconfig.get_config_vars()
prefix = vars["prefix"]
syspython = sys.executable #os.path.join(prefix, "python.exe")

# Default shebang for python - used for Windows cases
# As normally, the windows python path has spaces, and shebang notation breaks
execpython = '/usr/bin/env python'
# But linux paths don't always expand, so we use the system path instead
if sys.platform != 'win32':
    execpython = syspython

scriptDir = ''
# Almost verbatim from Pip's sourcecode.
if sys.platform == 'win32':
    scriptDir = os.path.join(sys.prefix, 'Scripts')
else:
    # This is different because it's just plain wrong on Debian.
    #scriptDir = os.path.join(sys.prefix, 'bin')
    scriptDir = os.path.curdir

# Keep in sync with setup.py.
scripts = [
    'dmm',
    'dmi',
	'dmi_compile',
    'dmindent',
    'dmmrender',
    'dmmfix',
    'ss13_gettechlevels',
    
    # Our post-install.  Now run on Linux, as well.
    "byondtools-postinstall"
]
print(" Checking {} for things to fix...".format(scriptDir))
for fileName in glob.glob(os.path.join(scriptDir, "*.py")):
    # skip already created batch files if they exist
    name, ext = os.path.splitext(os.path.basename(fileName))
    if name not in scripts or name == 'byondtools-postinstall':
        continue

    print('Running post-install for {}.'.format(name))
    # copy the file with the first line replaced with the correct python
    fullName = os.path.join(scriptDir, fileName)
    strippedName = os.path.join(scriptDir, name)
    lines = open(fullName).readlines()
    startidx=1
    if not lines[0].strip().startswith('#!'):
        print('WARNING: {} does not have a shebang.'.format(name))
        startidx=0
    
    targetFile = strippedName
    if sys.platform == 'win32':
        targetFile = fullName
    with open(targetFile, "w") as outFile:
        outFile.write("#!{}\n".format(execpython)) # Required for bash instances inside Windows
        for line in lines[startidx:]:
            outFile.write(line.rstrip('\r\n \t')+"\n") # Shit happens on Linux if this isn't done.

    if sys.platform == 'win32':
        # create the batch file
        batchFileName = strippedName + ".bat"
        command = "{} {} %*".format(syspython, targetFile) # Note system python path use here for batch
        open(batchFileName, "w").write("@echo off\n\n{}".format(command))
    else:
        os.chmod(strippedName, 0o755)
        print('CHMOD 755 {}'.format(strippedName))
