#!/bin/bash

function selwritters {
 local L_WRITTER L_WRITTER_S L_TOTAL L_TEMPO L_TEMPOE L_TEMPOR L_IMGSIZE L_SEG
 while [ ${C_TOTAL} -gt 0 ];do
   C_TOTAL=`cat ${TMP_DIR}/.c_total.temp`
   L_TOTAL=${C_TOTAL}
   ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --infobox $"Doing selection of recorders to be used in write process..." 7 70
   for L_WRITTER in `seq 1 ${WRITTERS}`;do
    L_WRITTER_S=$(echo `eval echo "\$\WRITTER"${L_WRITTER}""`)
    if [ ! -z `eval echo ${L_WRITTER_S}` ];then
     if cat ${CFG_DIR}/Recorder${L_WRITTER}.cfg|grep 'ENABLED=1' >/dev/null;then
      if [ ${L_TOTAL} -gt 0 ];then
       /usr/lib/cdcontrol/writtercontrol "$1" ${L_WRITTER} ${L_WRITTER_S} &
       ps ax --sort=ppid|grep cdrecord|grep -v 'grep cdrecord' | \
       tail -n 1|awk '{print $1}' >${TMP_DIR}/recorder${L_WRITTER}-busy
       let L_TOTAL=L_TOTAL-1
      fi
     fi
    fi
   done

   # Create variables to check writing time
   # Check if will be done a audio writting or data writting and calc the
   # imagem size according
    if [ "${IMG_TYPE}" = "A" ];then
      L_IMGSIZE=$(du -h `echo "$1"|sed -e s/'\/track_\*\.wav'//` | \
                  awk '{print $1}'| sed -e s/'M$'//)
     elif [ "${IMG_TYPE}" = "D" ];then
      L_IMGSIZE=`du -h $1 | awk '{print $1}' | sed -e s/'M$'//`
    fi
    L_TEMPO=$(echo $[(${L_IMGSIZE}/(8*${W_SPEED}))*60])
    L_TEMPOE=$(echo `echo "${L_IMGSIZE}/(8*${W_SPEED})" | bc -l | \
             sed -e s/'.*\.'/"0."/ | cut -b 1-4`*60 | bc | sed -e s/'\..*'//)
    let L_TEMPO=L_TEMPO+L_TEMPOE-10
    L_SEG=0

    sleep 10; clear
    L_TEMPOR=$[(${L_TEMPO}-${L_SEG})/60]
    echo -en $"\033[1;37mWritting, remaining time: ${L_TEMPOR} min, \033[0m"

   # Only allow a new writting if no one writting is already running currently.
   while ls ${TMP_DIR}|grep 'recorder'|grep 'busy' >/dev/null;do 
    sleep 6
    if [ ${L_SEG} -lt ${L_TEMPO} ];then let L_SEG=L_SEG+6;fi
    if [ ${L_TEMPOR} -gt $[(${L_TEMPO}-${L_SEG})/60] ];then
     let L_TEMPOR=L_TEMPOR-1
     if [ ${L_TEMPOR} -gt 1 ];then
      echo -en $"\033[1;37m${L_TEMPOR}, \033[0m"
     else
      echo -en $"\033[1;37mless than 1 minute.\033[0m"
     fi
    fi 
   done

   echo;echo -e $"\033[1;36m* Finished, Press ENTER to continue...\033[0m";read
   C_TOTAL=`cat ${TMP_DIR}/.c_total.temp`
   if [ ${C_TOTAL} -gt 0 ];then
    ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
        --title $"Write the rest of copies" \
        --yesno $"There are ${C_TOTAL} copies to be done, press <Enter> when\n\
you be ready to continue. Press <Esc> ou select 'NO' to cancel. :" 7 64
    case $? in
     1)
      return
      ;;
     255)
      return
      ;;
    esac
   fi 
 done
}


#
# Start function
#
function start_memory_cd {
 local L_MAT_NAME L_TMPFILE L_RETVAL
 if [ "${IMG_TYPE}" = "A" ];then
  ls -F ${IMG_DIR}|grep '/' >/dev/null
 elif [ "${IMG_TYPE}" = "D" ];then
  ls -F ${IMG_DIR}|grep -v '/' >/dev/null
 fi
 if [ $? != 0 ];then
   ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --msgbox $"No one CD image was found, use the read disk option on the main menu to create one." 0 0
  exit 1
 fi

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

 #------------------[ Start of image selection menu ]--------------------------
if [ "${IMG_TYPE}" = "A" ];then
 ${DIALOG}  --backtitle $"CDcontrol v${CDC_VERSION}" --clear \
         --title $"Select a CD image to write" \
         --menu $"Use the arrow keys 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 $"Select a CD image to write" \
         --menu $"Use the arrow 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}`

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

# Select the number of copies to do
${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" --clear --title $"Number of Copies" \
        --inputbox $"Type the wanted number of copies of the CD image\n\
'${L_MAT_NAME}' and press <Enter>\n\
to start the writing. Press <Esc>, select 'Cancel' or\n\
press <Enter> without typing a value to exit." 10 60 2>${L_TMPFILE}
L_RETVAL=$?
C_TOTAL=`cat ${L_TMPFILE}`

case ${L_RETVAL} in
  0)
   if [ -z "${C_TOTAL}" ];then
     ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --msgbox $"No one typed value." 7 40
    rm -f ${L_TMPFILE}
    exit 1
   fi
   echo ${C_TOTAL} >${TMP_DIR}/.c_total.temp
   if [ "${IMG_TYPE}" = "A" ];then
    selwritters "${IMG_DIR}/${L_MAT_NAME}/track_*.wav"
   elif [ "${IMG_TYPE}" = "D" ];then
    selwritters "${IMG_DIR}/${L_MAT_NAME}"
   fi
   ;;
  1)
   ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --msgbox $"Canceled by user." 7 40
   rm -f ${L_TMPFILE}
   exit 1
   ;;
  255)
   ${DIALOG} --backtitle $"CDcontrol v${CDC_VERSION}" \
         --msgbox $"Canceled by user." 7 40
   rm -f ${L_TMPFILE}
   exit 1
   ;;
esac
rm -f ${L_TMPFILE}

exit 0
}

start_memory_cd

