#/bin/bash
egpu_dir="/proc/egpu"
argument=""

while getopts "r:u:p:" opt; do
  case $opt in
    r) argument=${OPTARG};;
    \?)
        echo "Invalid or missing option: -$OPTARG" >&2
        echo "Usage: $0 -r [all|container_id]"
        exit 1
        ;;
  esac
done

if [[ -z $argument ]]; then
    echo "Error: No argument provided for -r."
    echo "Usage: $0 -r [all|\${container_id}]"
    exit 1
fi


for i in {0..15}
do

    current_dir="${egpu_dir}/${i}"
    
    if [[ -d ${current_dir} ]]; then
        cd ${current_dir}

        subdirs=($(find . -maxdepth 1 -type d -printf "%f\n" | tail -n +2))
        
        if [[ $argument == "all" ]]; then
            for subdir_name in "${subdirs[@]}"
            do
				echo -n ${subdir_name} > /proc/egpu/${i}/destroy_egpu
            done
        else
            if [[ " ${subdirs[@]} " =~ " ${argument} " ]]; then
				echo -n ${argument} > /proc/egpu/${i}/destroy_egpu
            fi
        fi
    fi
done
