#!/bin/bash
# Erase a CD image already written to disk

if [ "${IMG_TYPE}" = "A" ];then
 if ! ls -F ${IMG_DIR}|grep '/' >/dev/null;then
  ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --msgbox $"'No one CD image was found, please select the read disk option on the main 
menu to create one. Press <Enter> to return." 0 0
  exit 1
 fi
elif [ "${IMG_TYPE}" = "D" ];then
 if ! ls -F ${IMG_DIR}|grep -v '/' >/dev/null;then
  ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --msgbox $"'No one CD image was found, please select the read disk option on the main 
menu to create one. Press <Enter> to return." 0 0
  exit 1
 fi
fi

# Create the temporary file that will be used to hold the return value
# of dialog menus
L_TMPFILE=`mktemp /tmp/fileXXXXXX`


# Erase a CD image
function cdcontrol_apaga_img {
 while ls $3|grep 'recorder'|grep 'busy' >/dev/null;do
  sleep 10
 done
 ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" --infobox $"Deleting $2..." 0 0
 rm -rf $1/$2
 if [ $? = 0 ];then
  clear
  ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --infobox $"'$2' sucessfully deleted. Press <Enter> to return..." 7 60
 else
  clear
  ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --infobox $"Unable to delete $2, press <Enter> to return..." 7 60
 fi
 exit 0
}


#------------------[ Inicio do menu de seleo de Imagem ]--------------------------
if [ "${IMG_TYPE}" = "A" ];then
 ${DIALOG}  --backtitle $"CDcontrol v${CDC_VERSION}" \
         --clear --title $"Delete a written CD image from memory" \
         --menu $"Use the up/down arrows and press <Enter>\n\
Press <Esc> or select 'Cancel' to exit:" 20 60 10 \
 `ls -F ${IMG_DIR} | grep '/' | sed -e s/"^"/"\""/ | \
  sed -e s/"\/$"/'\" \"\" \\ '/` 2>${L_TMPFILE}
elif [ "${IMG_TYPE}" = "D" ];then
 ${DIALOG}  --backtitle $"CDcontrol v${CDC_VERSION}" \
         --clear --title $"Delete a written CD image" \
         --menu $"Use the up/down keys and press <Enter>\n\
Press <Esc> or select 'Cancel' to exit:" 20 60 10 \
 `ls -F ${IMG_DIR} | grep -v '/' | sed -e s/"^"/"\""/ | \
  sed -e s/"\.iso$"/'\.iso\" \"\" \\ '/` 2>${L_TMPFILE}
fi
 L_RETVAL=$?
 L_MAT_NAME=`cat ${L_TMPFILE}`
 rm -f ${L_TMPFILE} 

 case ${L_RETVAL} in
   1)
     ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --msgbox $"Canceled by user." 7 30
     exit 1
     ;;
   255)
     ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --msgbox $"Canceled by user." 7 30
     exit 1
     ;;
 esac
 # Cut out "" from selection
 L_MAT_NAME=`echo ${L_MAT_NAME}|sed -e s/"\""//g`


 ${DIALOG}  --backtitle $"CDcontrol v${CDC_VERSION}" --clear --title $"Confirmation" \
         --yesno $"Do you want to delete the CD image '${L_MAT_NAME}'" 0 0

case $? in
  0)
   if ls ${TMP_DIR}|grep 'recorder'|grep 'busy' >/dev/null;then
    ${DIALOG} --clear --title $"One recording is running" \
    --yesno $"CDcontrol has detected at least one recording process running, do you want that \n\
the CDcontrol schedule this delete to happen after all writting be\n\
finished?" 0 0
    case $? in
      0)
       cdcontrol_apaga_img ${IMG_DIR} ${L_MAT_NAME} ${TMP_DIR} &
       exit 0
       ;;
      1)
       clear
       ${DIALOG}  --backtitle $"CDcontrol v${CDC_VERSION}" --sleep 5 \
       --infobox $"'${L_MAT_NAME}' was not deleted, canceled by user..." 7 70
       clear
       exit 1
       ;;
      255)
       clear
       ${DIALOG}  --backtitle $"CDcontrol v${CDC_VERSION}" --sleep 5 \
         --infobox $"'${L_MAT_NAME}' was not deleted, canceled by user..." 7 70
       clear
       exit 1
       ;;
     esac
   fi
   cdcontrol_apaga_img ${IMG_DIR} ${L_MAT_NAME} ${TMP_DIR}
    ;;
  1)
   clear
   ${DIALOG}  --backtitle $"CDcontrol v${CDC_VERSION}" --sleep 5 \
     --infobox $"'${L_MAT_NAME}' was not deleted, canceled by user..." 7 70
   clear
   exit 1
   ;;
  255)
   clear
   ${DIALOG}  --backtitle $"CDcontrol v${CDC_VERSION}" --sleep 5 \
     --infobox $"'${L_MAT_NAME}' was not deleted, canceled by user..." 7 70
   clear
   exit 1
   ;;
esac

exit 0
