#!/usr/bin/env python3

import os
import time
import signal
import subprocess
import sys


def get_odyssey_pids():
    print(subprocess.check_output(
        'ps aux | grep odyssey', shell=True).decode('utf-8'))

    try:
        return list(map(int, subprocess.check_output(['pgrep', 'odyssey']).split()))
    except subprocess.CalledProcessError:
        # non-zero exit code means that there is no odyssey pids
        return []


def terminate_gracefully(pid, timeout):
    try:
        os.kill(pid, signal.SIGTERM)
    except ProcessLookupError:
        print(f'Process {pid} already finished or doesnt ever existed')
        return

    print(f'Waiting {timeout} seconds to {
          pid} finish after SIGTERM...', end='')

    start = time.time()
    finished = False

    while time.time() - start < timeout:
        try:
            output = subprocess.check_output(['ps', '-p', str(pid)], )
            print(output.decode('utf-8'))
            if 'odyssey'.encode('utf-8') not in output:
                finished = True
                break
        except subprocess.CalledProcessError:
            # non-zero code means there is no process with that pid
            finished = True
            break

        print('.', end='')
        time.sleep(0.5)

    print()

    return finished


def main():
    timeout = 5

    if len(sys.argv) > 1:
        pids = [int(sys.argv[1])]
    else:
        pids = get_odyssey_pids()
    print('Odyssey pids to stop:', ', '.join(list(map(str, pids))))

    for pid in pids:
        if not terminate_gracefully(pid, timeout):
            print(f'Process {pid} didnt finish within {timeout} seconds')
            print(subprocess.check_output(['gdb', '-p', str(pid), '--batch', '-ex',
                  't a a bt', '-ex', 'source /gdb.py', '-ex', 'mmcoro all bt']).decode('utf-8'))
            exit(1)

    exit(0)


if __name__ == "__main__":
    main()
