#!/bin/bash
# CDControl main module
# Author: Gleydson Mazioli da Silva <gleydson@debian.org>
# Licence: Licenced under the terms of GPL (General Public Licence)

source /etc/cdcontrolrc
TMP_DIR=`mktemp -d /tmp/cdcontrol.XXXXXX`
LOG_DIR=/var/log/cdcontrol
export DIALOG TMP_DIR LOG_DIR IMG_DIR IMG_TYPE CFG_DIR IMG_PARTITION R_SPEED \
       CD_READER WRITTERS W_SPEED CACHE EXTRA_AREC_OPT EXTRA_DREC_OPT VERBOSE \
       CDC_VERSION WRITTER1 WRITTER2 WRITTER3 WRITTER4 WRITTER5 WRITTER6 WRITTER7 \
       WRITTER8 WRITTER9 WRITTER10 WRITTER11 WRITTER12 WRITTER13 WRITTER14

DIALOG=dialog
CDC_VERSION="1.90"

#
# Check the CDControl configuration
#
checkcdc_config() {
	if [ ! -x ${LOG_DIR}/tec ]; then mkdir -p ${LOG_DIR}/tec;fi
	if [ ! -x ${TMP_DIR} ];     then mkdir -p ${TMP_DIR};    fi
	if [ ! -x ${CFG_DIR} ];     then mkdir -p ${CFG_DIR};    fi

	SYS_MEM=`free -m|grep -E "^Mem"|awk '{print $2}'`
	if [ $(echo \(${SYS_MEM}-\(${CACHE}*${WRITTERS}\)\)-4|bc) -lt 0 ];then
		echo $"The maximum cache size for all writters is bigger than the amount"
		echo $"of system memory. Please fix the problem in the configuration file"
		echo $"and run this utility again."
		exit 1
	fi

	#
	# The second expression result should always be bigger or equal than first
	#
	if [ $(echo \(${SYS_MEM}-\(${CACHE}*${WRITTERS}\)\)-4|bc) -lt \
	$(echo 6*\(${W_SPEED}/4\)|bc) ];then
		echo $"The resources specified in the configuration file are greather than"
		echo $"the allowed by your system, because of it CDcontrol stoped."
		echo $"Possible solutions are que decreasing of the write cache for the writters"
		echo $"or adding more RAM memory to the system. Check your system resources."
		exit 1
	fi

	echo $"RAM Memory  .........: ${SYS_MEM}"
	echo $"Writters ............: ${WRITTERS}"
	echo $"Recording Speed .....: ${W_SPEED}"
	echo $"Cache Memory used ..: $[${CACHE}*${WRITTERS}] (${CACHE}MB by ${WRITTERS} writters)"
}


#
# Should be run on CDControl startup
#
check_status() {
 local L_WRITTER L_WRITTER_S
 for L_WRITTER in `seq 1 ${WRITTERS}`;do
  L_WRITTER_S=$(echo `eval echo "\$\WRITTER"${L_WRITTER}""`)
  # Remove previous lockfiles, before start writting
  if [ -f ${TMP_DIR}/recorder${L_WRITTER}-busy ];then
   echo $"Removing lockfile for drive ${L_WRITTER}..."
   rm -f ${TMP_DIR}/recorder${L_WRITTER}-busy
  fi
  # Onle show the message that drive will be disabled once, the true 
  # disabling is done on selwritters function
  if [ -z `eval echo ${L_WRITTER_S}` ];then
   echo $"Configuration for writter ${L_WRITTER} not found, writter disabled."
  else
   # Create the file for enabled writter if don't exist
   if [ ! -f ${CFG_DIR}/Recorder${L_WRITTER}.cfg ];then
    echo $"Creating a configuration file to writter ${L_WRITTER}..."
    echo "ENABLED=1" >${CFG_DIR}/Recorder${L_WRITTER}.cfg
    echo "ERRORS=2" >>${CFG_DIR}/Recorder${L_WRITTER}.cfg
    echo "OBS=" >>${CFG_DIR}/Recorder${L_WRITTER}.cfg
   fi
  fi
  rm -f ${TMP_DIR}/.Gravados-*.lock ${TMP_DIR}/.Falhas-*.lock
 done
}


#
# Check Program dependencies
#
check_depends() {
 local L_EXIT
 L_EXIT=0
 if ! which which &>/dev/null;then
  echo $"The which program cound't be found or is out of current path"
  exit 1
 fi
 echo $"Checking dependencies... "
 if ! which awk &>/dev/null;then echo $"Could not found the awk program";L_EXIT=1;fi
 if ! which bc &>/dev/null;then echo $"Could not found the bc program";L_EXIT=1;fi
 if ! which cdda2wav &>/dev/null;then echo $"Could not found the cdda2wav program";L_EXIT=1;fi
 if ! which cdrecord &>/dev/null;then echo $"Could not found the cdrecord program";L_EXIT=1;fi
 if ! which cut &>/dev/null;then echo $"Could not found the cut program";L_EXIT=1;fi
 if ! which dc &>/dev/null;then echo $"Could not found the dc program";L_EXIT=1;fi
 if ! which df &>/dev/null;then echo $"Could not found the df program";L_EXIT=1;fi
 if ! which dialog &>/dev/null;then echo $"Could not found the dialog program";L_EXIT=1;fi
 if ! which grep &>/dev/null;then echo $"Could not found the grep program";L_EXIT=1;fi
 if ! which lockfile &>/dev/null;then echo $"Could not found the lockfile program";L_EXIT=1;fi
 if ! which ls &>/dev/null;then echo $"Could not found the ls program";L_EXIT=1;fi
 if ! which ps &>/dev/null;then echo $"Could not found the ps program";L_EXIT=1;fi 
 if ! which sed &>/dev/null;then echo $"Could not found the sed program";L_EXIT=1;fi
 if ! which seq &>/dev/null;then echo $"Could not found the seq program";L_EXIT=1;fi 
 if ! which sleep &>/dev/null;then echo $"Could not found the sleep program";L_EXIT=1;fi
 if ! which tail &>/dev/null;then echo $"Could not found the tail program";L_EXIT=1;fi
 if ! which touch &>/dev/null;then echo $"Could not found the touch program";L_EXIT=1;fi
 if ! which uname &>/dev/null;then echo $"Could not found the uname program";L_EXIT=1;fi 

 # Exit if any dependency wasn't found
 if [ ${L_EXIT} != 0 ];then 
  exit 1
 else
  echo $"All dependencies are OK, you can comment the 'check_depends' line"
  echo $"at the end of this file do speed up the program start"
  sleep 4
 fi
}

#
# Start main menu
#
main_menu() {
 local L_EXIT L_RETVAL L_OPCAO L_TEMPFILE
 L_TEMPFILE=`mktemp /tmp/temp-XXXXXX`
 L_EXIT=0
 ${DIALOG} --title $"Select the Image type" --item-help --clear --backtitle "CDControl v${CDC_VERSION}" \
--trim --menu \
$"Choose the type of CD Image that you want to work on in this session." 0 0 2 \
"1" $"Data CD" $"CDControl will act to handle normal data CDs" \
"2" $"Audio CD" $"CDControl will act to handle audio tracks" 2>${L_TEMPFILE} 
L_RETVAL=$?
 case ${L_RETVAL} in
     1)
     L_EXIT=1
     ;;
     255)
     L_EXIT=1
     ;;
 esac
L_OPCAO=`cat ${L_TEMPFILE}`

case ${L_OPCAO} in
    1)
    IMG_TYPE="D"
    ;;

    2)
    IMG_TYPE="A"
    ;;
esac

 while [ "${L_EXIT}" != 1 ];do
  ${DIALOG} --title $"Main Menu" --item-help --clear --backtitle "CDControl v${CDC_VERSION}" \
--trim --menu \
$"Use the arrow keys to select bellow the the action that you want to do and press <ENTER> to confirm." 0 0 8 \
"1" $"Read the CD and write to disk" $"Read a CD and write a image on the disk to make copies" \
"2" $"Write a CD image to one or more CD Writters" $"Burn copies of the CD image" \
"3" $"Erase a disk image" $"Erase a already written image from the disk to free disk space" \
"4" $"Report of written disk, errors, etc." $"Full report of actions made by CDControl" \
"5" $"Configure CD Writters (enable, disable)" $"Allow you to configure options for you CD Writter" \
"6" $"Exit" $"Exit CDControl" 2>${L_TEMPFILE} 
L_RETVAL=$?
 case ${L_RETVAL} in
     1)
     L_EXIT=1
     ;;
     255)
     L_EXIT=1
     ;;
 esac
L_OPCAO=`cat ${L_TEMPFILE}`

case ${L_OPCAO} in
    1)
    /usr/lib/cdcontrol/cd-memory
    ;;

    2)
    /usr/lib/cdcontrol/memory-cd
    ;;

    3)
    /usr/lib/cdcontrol/cdcontrol-erase
    ;;

    4)
    /usr/lib/cdcontrol/cdcontrol-rel
    ;;

    5)
    /usr/lib/cdcontrol/config-cdcontrol
    ;;
    
    6)
    L_EXIT=1
    ;;
esac
done
clear
rm -f ${L_TEMPFILE};rm -rf ${TMP_DIR}
echo $"Thanks for using CDControl!"
echo $"Gleydson Mazioli da Silva <gleydson@debian.org>"
echo ""
}

checkcdc_config
check_depends
check_status
main_menu
