#!/usr/bin/env python

import os
import sys
import argparse
import subprocess
import math
import pandas as pd
from scipy import stats
import run_matlab.commands as commands
from run_matlab.version import __version__

def remove_prefix(input_string, prefix):
    if prefix and input_string.startswith(prefix):
        return input_string[len(prefix):]
    return input_string

def remove_suffix(input_string, suffix):
    if suffix and input_string.endswith(suffix):
        return input_string[:-len(suffix)]
    return input_string

class run_matlab(object):

    def __init__(self):
        parser = commands.run_matlab_parser()

        option_ix = 1
        while (option_ix < len(sys.argv) and
               sys.argv[option_ix].startswith('-')):
            option_ix += 1
                
        args = parser.parse_args(sys.argv[1:option_ix+1])
        
        if args.command is None or not hasattr(self, args.command):
            print('Unrecognized command')
            parser.print_help()
            exit(1)
        
        command = " ".join(sys.argv)
        
        import run_matlab
        
        getattr(self, args.command)([sys.argv[0]] + sys.argv[option_ix:])
            
    def install(self, argv):
        parser = commands.install_parser()

        args = parser.parse_args(argv[2:])
        
        os.makedirs(os.path.abspath(args.installation_dir), exist_ok = True)
        
        link = 'https://github.com/hanjunlee21/run_matlab/archive/refs/tags/v.' + __version__ + '.tar.gz'
        tar_gz_path = os.path.abspath(args.installation_dir) + '/v.' + __version__ + '.tar.gz'
        subprocess.call(['wget', '-O', tar_gz_path, link])
        subprocess.call(['tar', '-C', os.path.abspath(args.installation_dir), '-x', '-v', '-z', '-f', tar_gz_path])
        subprocess.call(['rm', tar_gz_path])
        
        with open(os.path.abspath(args.installation_dir) + '/run_matlab-v.' + __version__ + '/run_matlab/install.sh', 'r') as file:
            filedata = file.read()
        filedata = filedata.replace('TOREPLACE_INSTALLATION_DIR', os.path.abspath(args.installation_dir))
        filedata = filedata.replace('TOREPLACE_MATLAB_VER', args.matlab_version)
        filedata = filedata.replace('TOREPLACE_RUNTIME_VER', args.runtime_version)
        with open(os.path.abspath(args.installation_dir) + '/run_matlab-v.' + __version__ + '/run_matlab/install_task.sh', 'w') as file:
            file.write(filedata)
        
        subprocess.call(['bash', os.path.abspath(args.installation_dir) + '/run_matlab-v.' + __version__ + '/run_matlab/install_task.sh'])
        subprocess.call(['rm', os.path.abspath(args.installation_dir) + '/run_matlab-v.' + __version__ + '/run_matlab/install_task.sh'])
        
    def run(self, argv):
        parser = commands.run_parser()

        args = parser.parse_args(argv[2:])
        
        with open(os.path.abspath(args.installation_dir) + '/run_matlab-v.' + __version__ + '/run_matlab/run.sh', 'r') as file:
            filedata = file.read()
        filedata = filedata.replace('TOREPLACE_INSTALLATION_DIR', os.path.abspath(args.installation_dir))
        filedata = filedata.replace('TOREPLACE_MATLAB_VER', args.matlab_version)
        filedata = filedata.replace('TOREPLACE_RUNTIME_VER', args.runtime_version)
        filedata = filedata.replace('TOREPLACE_FUNCTION_DIR', args.function_dir)
        filedata = filedata.replace('TOREPLACE_FUNCTION_NAME', args.function_name)
        #filedata = filedata.replace('TOREPLACE_FUNCTION_ARGS', args.function_args)
        args.function_args
        with open(os.path.abspath(args.installation_dir) + '/run_matlab-v.' + __version__ + '/run_matlab/run_task.sh', 'w') as file:
            file.write(filedata)
            
        subprocess.call(['bash', os.path.abspath(args.installation_dir) + '/run_matlab-v.' + __version__ + '/run_matlab/run_task.sh'])
        subprocess.call(['rm', os.path.abspath(args.installation_dir) + '/run_matlab-v.' + __version__ + '/run_matlab/run_task.sh'])
            
if __name__ == '__main__':
    run_matlab()
