#!/bin/bash
# Apaga uma imagem j gravada no disco

source /etc/cdcontrolrc

if [ "${IMG_TYPE}" = "A" ];then
 if ! ls -F ${IMG_DIR}|grep '/' >/dev/null;then
  echo "No one CD image was found, please use the 'cd-memoria' utility."
  exit 1
 fi
elif [ "${IMG_TYPE}" = "D" ];then
 if ! ls -F ${IMG_DIR}|grep -v '/' >/dev/null;then
  echo "No one CD image was found, please use the 'cd-memoria' utility"
  exit 1
 fi
fi

# Cria o arquivo temporrio que ser usado para armazenar o valor de retorno
# dos menus
L_TMPFILE=`mktemp /tmp/fileXXXXXX`


# Apaga a imagem de CD
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' sucesfully deleted. Press <Enter> to exit..." 0 0
 else
  clear
  dialog --backtitle "CDcontrol v${CDC_VERSION}" \
         --infobox "Unable to delete $2, press <Enter> to exit..." 0 0
 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)
     echo "Canceled by user."
     exit 1
     ;;
   255)
     echo "Canceled by user."
     exit 1
     ;;
 esac
 #Retira "aspas" da seleo
 L_MAT_NAME=`echo ${L_MAT_NAME}|sed -e s/"\""//g`


 dialog  --backtitle "CDcontrol v${CDC_VERSION}" --clear --title "Confirmation" \
         --yesno "Do you like to delete the CD image \n\
'${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 "Was detectec one recording running, do you want that \n\
the CDcontrol schedule the 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..." 0 0
       clear
       exit 1
       ;;
      255)
       clear
       dialog  --backtitle "CDcontrol v${CDC_VERSION}" --sleep 5 \
         --infobox "'${L_MAT_NAME}' was not deleted, canceled by user..." 0 0
       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..." 0 0
   clear
   exit 1
   ;;
  255)
   clear
   dialog  --backtitle "CDcontrol v${CDC_VERSION}" --sleep 5 \
     --infobox "'${L_MAT_NAME}' was not deleted, canceled by user..." 0 0
   clear
   exit 1
   ;;
esac

exit 0
