import os
import platform
from shutil import copyfile
import sys

demos = {
  "pdf2office"
}

def copy_lib():
    arch = platform.architecture()
    cur_sys = platform.system()
    
    if arch[0] == "32bit" and cur_sys == "Windows":
        src_lib_path = "../../FoxitPDFConversionSDKPython2/x86_vc17/_fpdfconversionsdk.pyd"
    elif arch[0] == "64bit" and cur_sys == "Windows":
        src_lib_path = "../../FoxitPDFConversionSDKPython2/x64_vc17/_fpdfconversionsdk.pyd"
    elif arch[0] == "32bit" and cur_sys == "Linux":
        src_lib_path = "../../FoxitPDFConversionSDKPython2/x86/_fpdfconversionsdk.so"
    elif arch[0] == "64bit" and cur_sys == "Linux":
        src_lib_path = "../../FoxitPDFConversionSDKPython2/x64/_fpdfconversionsdk.so"
    else:
        src_lib_path = None
    
    if cur_sys == "Windows":
        dest_lib_path = "../../FoxitPDFConversionSDKPython2/_fpdfconversionsdk.pyd"
    else:
        dest_lib_path = "../../FoxitPDFConversionSDKPython2/_fpdfconversionsdk.so"
        
    if src_lib_path is not None:
        copyfile(src_lib_path, dest_lib_path)

def run_single_demo(demo):
    os.chdir(demo)
    democmd = 'python -u ' + demo + '.py'
    os.system(democmd)


def run_all_demos():
    if not os.path.exists('demo_log_py'):
        os.mkdir('demo_log_py')

    for demo in demos:
        os.chdir(demo)
        democmd = 'python -u ' + demo + '.py >> ../demo_log_py/' + demo + '.log 2>&1'
        os.system(democmd)
        print('demo ' + demo + ' finished !')
        os.chdir('../')

if __name__ == '__main__':
    major = sys.version_info.major
    minor = sys.version_info.minor
    if not (major == 2 and minor == 7) and not (major == 3):
        print("Run error: you need Python2.7 or Python3.x to run this py.")
        sys.exit(0)
    if major == 2 and minor == 7:
        copy_lib()

    if len(sys.argv) > 1:
        run_single_demo(sys.argv[1])
    else:
        run_all_demos()