#!/bin/bash

SHUTDOWN_DELAY=60

while getopts "d:" opt; do
    case $opt in
        d) SHUTDOWN_DELAY="$OPTARG";;
        \?) echo "Invalid option: -$OPTARG" >&2; exit 1;;
    esac
done

# Shift past the options to get positional argument
shift $((OPTIND-1))

if [ $# -ne 1 ]; then
    echo "Usage: $0 [-d delay_seconds] process_name"
    exit 1
fi

# Configuration
PROCESS_NAME="$1"  # Name of the process to monitor

# Check if process is running
while true; do
    if ! pgrep -x "$PROCESS_NAME" > /dev/null; then
        echo "Process $PROCESS_NAME is no longer running"
        echo "System will shutdown in $SHUTDOWN_DELAY seconds"
        sleep $SHUTDOWN_DELAY
        shutdown -h now
    fi
    sleep 5
done
