#!/bin/bash
# Write a CD imagem to hard disk from a CD

FREE_SPC=$(echo $[`df -m | grep "${IMG_PARTITION}" | awk '{print $4}'`/655])

#--------[ Check free space before to write the image ]------------
if [ ${FREE_SPC} = 0 ];then
 ${DIALOG}  --backtitle $"CDcontrol v${CDC_VERSION}" --title $"Alert!" --clear \
   --msgbox $"You don't have enought free space on your partition to write a new,\n\
CD image. Select the 'Erase' option from the main menu or free up some space\n\
on your disk to write CDControl disk image. Press <Enter>" 0 0 
 exit 1
fi

#-------------------[ Dialog Processing ]----------------------------
L_TMPFILE=`mktemp /tmp/fileXXXXXX`

${DIALOG}  --backtitle $"CDcontrol v${CDC_VERSION}" \
        --title $"Type the CD name" --clear \
       --inputbox $"Type the CD name in the space bellow and press\n\
<ENTER>. There is space for ${FREE_SPC} full (740MB) CD images\n\
on your disk." \
        10 51 2> ${L_TMPFILE}
retval=$?
CD_NAME=`cat ${L_TMPFILE}|sed -e s/' '/'_'/g`
rm -f ${L_TMPFILE}
case $retval in
  0)
   if [ -z ${CD_NAME} ];then
     ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --msgbox $"Canceled by user." 7 40
    exit 1
   fi
    ;;
  1)
     ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --msgbox $"Canceled by user." 7 40
    exit 1;;
  255)
     ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --msgbox $"Canceled by user." 7 40
    exit 1;;
esac
#----------------------------------------------------------------------------------

if [ ! -d "${IMG_DIR}/${CD_NAME}" -a "${IMG_TYPE}" = "A" ];then 
 mkdir -p "${IMG_DIR}/${CD_NAME}" >/dev/null
fi

if [ "${IMG_TYPE}" = "A" ];then
 if ! cd "${IMG_DIR}/${CD_NAME}" >/dev/null;then
     ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --msgbox $"Could not possible write to disk, chech if the access permissions are ok and try again" 0 0
  exit 1
 fi
elif [ "${IMG_TYPE}" = "D" ];then
 cd "${IMG_DIR}"
fi

clear
if [ "${IMG_TYPE}" = "A" ];then
     ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --infobox $"Reading the audio CD \"${CD_NAME}\". Please wait..." 7 60
elif [ "${IMG_TYPE}" = "D" ];then
     ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --infobox $"Reading the data CD \"${CD_NAME}\". Please wait..." 7 60
fi

if [ "${IMG_TYPE}" = "A" ];then
 cdda2wav -x -v255 -D${CD_READER} -B -S${R_SPEED} -Owav track
elif [ "${IMG_TYPE}" = "D" ];then
 dd if=${CD_READER} of="${CD_NAME}.iso"
fi

# Check if the image was written with sucesfull checking the las
# program exitcode
if [ "$?" = "0" ];then
 clear
 ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
   --msgbox $"CD \"${CD_NAME}\" sucessfully written to the memory." 7 40
else
 ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
   --msgbox $"Failed to write \"${CD_NAME}\" to the memory. The most probably cause
   for this is disk errors or a CD drive failing to read." 7 40
 if [ "${IMG_TYPE}" = "A" ];then
  rm -rf "${IMG_DIR}/${CD_NAME}" >/dev/null
 elif [ "${IMG_TYPE}" = "D" ];then
  rm -rf "${IMG_DIR}/${CD_NAME}.iso" >/dev/null
 fi
fi
exit 0
