diff --git a/src/scripts/utils/dvd/dvd-storage-action b/src/scripts/utils/dvd/dvd-storage-action new file mode 100644 index 0000000..b8f5f8c --- /dev/null +++ b/src/scripts/utils/dvd/dvd-storage-action @@ -0,0 +1,93 @@ +#!/bin/bash + +ACTION=$1 +DEVNAME=$2 + +SOCKET_PATH="/tmp/usb-Sono-Socket.socket" +LOG_FILE="/tmp/tmpDvd.log" +DEBOUNCE_FILE="/tmp/dvd_debounce_$(echo $DEVNAME | tr '/' '-')" +DEBOUNCE_TIME=10 # seconds + +current_time=$(date +%s) + +# Function to log messages +log_message() { + local message=$1 + echo $message | socat - UNIX-CONNECT:$SOCKET_PATH + echo $message >> $LOG_FILE +} + +# Function to get the size of the DVD +get_dvd_size() { + udisksctl info -b $DEVNAME | grep 'Size:' | awk '{print $2}' | head -n 1 +} + +# Function to check for debounce +check_debounce() { + SIZE=$(get_dvd_size) + if [SIZE -eq 0]; then + ATTEMPTS=3 + while [ "$SIZE" -eq 0 ] && [ $ATTEMPTS -gt 0 ]; do + sleep 2 + SIZE=$(get_dvd_size) + log_message "Debounced: Retried size of $DEVNAME is $SIZE" + ATTEMPTS=$((ATTEMPTS - 1)) + done + elif [SIZE -eq 0]; then + log_message "exit on: Retried size of $DEVNAME is $SIZE" + exit 0 + fi + + if [ -f "$DEBOUNCE_FILE" ]; then + last_run_time=$(cat $DEBOUNCE_FILE) + elapsed_time=$((current_time - last_run_time)) + if [ $elapsed_time -lt $DEBOUNCE_TIME ]; then + log_message "Debounced: $ACTION $DEVNAME" + exit 0 + fi + fi + echo $current_time > $DEBOUNCE_FILE +} + +check_debounce + + + +if [ "$ACTION" == "change" ]; then + # Introduce a delay before checking the device + sleep 2 # Wait for 2 seconds before proceeding + + # Check if the device is already mounted + MOUNTED=$(lsblk -o MOUNTPOINT -nr $DEVNAME) + if [ -z "$MOUNTED" ]; then + # Device is not mounted, check if it is a raw DVD + SIZE=$(get_dvd_size) + + # Debug output for size + log_message "DEBUG: Initial size of $DEVNAME is $SIZE" + + # Retry logic for getting the size + ATTEMPTS=3 + while [ "$SIZE" -eq 0 ] && [ $ATTEMPTS -gt 0 ]; do + sleep 2 + SIZE=$(get_dvd_size) + log_message "DEBUG: Retried size of $DEVNAME is $SIZE" + ATTEMPTS=$((ATTEMPTS - 1)) + done + + if [ "$SIZE" -gt 0 ]; then + log_message "success insert raw DVD $DEVNAME" + else + # Proceed to mount the device + udisksctl mount -b $DEVNAME + if [ $? -eq 0 ]; then + log_message "success mount $DEVNAME" + else + log_message "failed to mount $DEVNAME" + fi + fi + else + log_message "$DEVNAME is already mounted at $MOUNTED" + fi + log_message "success insert $DEVNAME" +fi