#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import signal
import argparse
import time

def kill_process(pid, force=False):
    try:
        pid = int(pid)
    except ValueError:
        print(f"Invalid PID: {pid}")
        return

    # Check if process exists
    try:
        os.kill(pid, 0)
    except ProcessLookupError:
        print(f"Process {pid} does not exist.")
        return
    except PermissionError:
        print(f"Permission denied to signal process {pid}.")
        return

    if not force:
        print(f"Sending SIGTERM to PID {pid}...")
        os.kill(pid, signal.SIGTERM)
        time.sleep(1)

        # Check if still alive
        try:
            os.kill(pid, 0)
            print(f"Process {pid} still alive. Use --force to SIGKILL.")
        except ProcessLookupError:
            print(f"Process {pid} terminated cleanly.")
        return

    # Force kill
    print(f"Sending SIGKILL to PID {pid}...")
    os.kill(pid, signal.SIGKILL)
    print(f"Process {pid} force‑killed.")


def main():
    parser = argparse.ArgumentParser(description="Kill a Linux process by PID.")
    parser.add_argument("pid", help="Process ID to kill")
    parser.add_argument("--force", action="store_true", help="Force kill using SIGKILL")
    args = parser.parse_args()

    kill_process(args.pid, args.force)


if __name__ == "__main__":
    main()
