#!/bin/bash

. /usr/share/dosis2/bin/shell-support-functions

enforce_root

# Initialize variables
timestamp=""
l_flag=false
s_flag=false

# Function to display usage
usage() {
    echo "Usage: $0 [-l or -s] -t <time stamp string>"
    exit 1
}

# Parse command-line options
while getopts "t:ls" opt; do
    case $opt in
        t)
            timestamp="$OPTARG"
            ;;
        l)
            l_flag=true
            ;;
        s)
            s_flag=true
            ;;
        *)
            usage
            ;;
    esac
done

# Check for required parameters
if [ -z "$timestamp" ]; then
    echo "Error: -t <time stamp string> is required."
    usage
fi

if ! $l_flag && ! $s_flag; then
    echo "Error: Either -l or -s is required."
    usage
fi

# Build the command as an array
sub_script_command=("/opt/dosis2/bin/python3-venv" "/opt/dosis-updater/updater_cli.py" "-p")

if $l_flag; then
    sub_script_command+=("-l")
fi

if $s_flag; then
    sub_script_command+=("-s")
fi

sub_script_command+=("-t" "$timestamp")

# Execute the sub_script command
nohup "${sub_script_command[@]}" > /dev/null 2>&1 &
logger "prep running in background"
