#!python
import argparse
import subprocess
from pathlib import Path
import os
import signal
import sys

STOPPING_INS = f"""
        1) You can either press CTRL+C here
                    Or
        2) Use a seperate terminal and type `lpw stop`
"""

def stop_lpw():
    if os.path.exists(f'{home_dir}/.lpw'):
        pid = int(open(f'{home_dir}/.lpw', 'r').read())
        os.kill(pid, signal.SIGINT)
        os.remove(f'{home_dir}/.lpw')
    else:
        print(f'lpw does not seems to be running')

def signint_handler(sig, frame):
    print('Received CTRL+C !')
    print('Stopping LPW.... Please wait')
    stop_lpw()
    print('Stopped...')

current_script_path = os.path.abspath(__file__)
current_script_dir = os.path.dirname(current_script_path)
#print(f'current_script_path : {current_script_path}, current_script_dir : {current_script_dir}')
home_dir = os.path.expanduser("~")

cmd = ['streamlit', 'run', f'{current_script_dir}/lpw_main.py']

parser = argparse.ArgumentParser()
parser.add_argument("action", choices=["start", "stop"], help="Specify 'start' or 'stop'.")
args = parser.parse_args()
signal.signal(signal.SIGINT, signint_handler)

if args.action == 'start':
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    with open(f'{home_dir}/.lpw', 'w') as f:
        f.write(str(process.pid))
    print('LPW Started.. browse http://localhost:8501 and start whispering to LPW')
    print('Instructions for stopping LPW :')
    print(STOPPING_INS)
    process.communicate()

if args.action == 'stop':
    stop_lpw()
    print('LPW Stopped...')
    #print(process.stdout.read().decode('utf-8'))