#!/bin/bash
# Copyright (C) 2008 - 2015 vortexbox.org
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# Changelog
#
# Thurs July 10 2014 Ron Olsen <ronolsen@comcast.net>
# * Remove extra calls to /opt/vortexbox/checkcd $CDDEVICE since they are not needed,
# * and result in many "nodisk" messages in the system log.
# * Change cd-info command lines to avoid extraneous messages in "systemctl status vb-autoripper".
#
# Tues May 10 2011 Ron Olsen <ronolsen@comcast.net>
# * Use /opt/vortexbox/get-cddevice to get the CDDEVICE.
#
# Tues Mar 18 2014 Ron Olsen <ronolsen@comcast.net>
# * Make sure /usr/bin/flac is installed before calling ripit to rip an audio CD.

function echolog {
echo "`date +'%m/%d/%Y %H:%M:%S'` - $1"
}
echo "" >> /var/log/cdrip.log

CDDEVICES[0]="/dev/sr0";
CDDEVICES[1]="/dev/sr1";
CDDEVICES[2]="/dev/sr2";
CDDEVICES[3]="/dev/sr3";

# Eject slot load CD
if [ -f /etc/vortexbox/slotload ]; then
	/bin/eject
fi

echolog "<b>Restarting Auto Ripper...</b>" >> /var/log/cdrip.log
echolog "Ripping from $CDDEVICE" >> /var/log/cdrip.log
echolog "`/usr/bin/cd-info -q -C $CDDEVICE  --no-cddb --no-analyze --no-tracks --no-disc-mode 2>/dev/null | grep Vendor`" >> /var/log/cdrip.log
echolog "`/usr/bin/cd-info -q -C $CDDEVICE  --no-cddb --no-analyze --no-tracks --no-disc-mode 2>/dev/null | grep Model`" >> /var/log/cdrip.log
echolog "`/usr/bin/cd-info -q -C $CDDEVICE  --no-cddb --no-analyze --no-tracks --no-disc-mode 2>/dev/null | grep Revision`" >> /var/log/cdrip.log

echo "" >> /var/log/cdrip.log

if ping -c 1 -w 5 "google.com" &>/dev/null ; then
	echolog "Using your Internet connection to get metadata for ripped disks." >> /var/log/cdrip.log
else
	echolog "<b>Internet not available.</b> No track or disk info will be available." >> /var/log/cdrip.log
	echolog "(Your DNS may not be working)" >> /var/log/cdrip.log
fi	

echo "" >> /var/log/cdrip.log

while true
do
  for CDDEVICE in "${CDDEVICES[@]}";
  do

CDSTATUS=`/opt/vortexbox/checkcd $CDDEVICE`
############################################################################
# Rip CDs
# Rip CDs using ripit
############################################################################
 if [ "$CDSTATUS" = "ready" ]; then
   CDINFO=`/usr/bin/cd-info -C $CDDEVICE --no-cddb --no-analyze --no-device-info --no-tracks | grep "Disc mode is listed as" | cut -d \: -f 2`
   CDTYPE=${CDINFO/ /}

   # If the disk is an audio CD rip using ripit
   if [ "$CDTYPE" = "CD-DA" ]; then
	echolog "Optical disk detected - type : Audio CD" >> /var/log/cdrip.log
	if [ ! -f /usr/bin/flac ]; then
		echolog "Installing (required) flac ..." >> /var/log/cdrip.log
		dnf clean all
		dnf -y install flac >> /var/log/cdrip.log
	fi
	 /usr/bin/ripit -d $CDDEVICE 1-
	 /usr/bin/eject $CDDEVICE
   fi

############################################################################
# Rip DVDs or DVD-A
# Rip DVDs using makemkv
# Rip DVD audio disks using dvdae
############################################################################
   if [ "$CDTYPE" = "DVD-ROM" ]; then

	 if [ ! -f /usr/bin/ffmpeg ]; then
		echolog "Installing (required) ffmpg ..." >> /var/log/cdrip.log
		/usr/local/sbin/setup-mplayer >> /var/log/cdrip.log
	 fi
# Check to see if its a DVD or DVDa
	 HASH=`date +%s%N`
	 mkdir -p /tmp/dvdid$HASH
	 mount $CDDEVICE /tmp/dvdid$HASH &> /dev/null
	 if [ -f "/tmp/dvdid$HASH/AUDIO_TS/AUDIO_TS.IFO" ]; then
	   DVDANAME=`/usr/bin/volname $CDDEVICE | sed 's/ *$//g'`

       echolog "Optical disk detected - type : DVD-A (DVD audio)" >> /var/log/cdrip.log
	   echolog "Ripping $DVDANAME..."  >> /var/log/cdrip.log
	   
	   if [ ! -f /usr/bin/dvdae ]; then
		echolog "Installing (required) dvdae ..." >> /var/log/cdrip.log
		dnf clean all
		dnf -y install dvdae >> /var/log/cdrip.log
	   fi
	   
	   umount /tmp/dvdid$HASH
	   rm -rf /tmp/dvdid$HASH   
	   /usr/bin/dvdae --tag --output="/storage/music/flac/%ARTIST% - %ALBUM%" --name-format="%INDEX% %CHAPTER%" -fflac -c2 -b24 -w $CDDEVICE >> /var/log/cdrip.log 2>&1
	 else	 
	   umount /tmp/dvdid$HASH
	   rm -rf /tmp/dvdid$HASH
	   echolog "Optical disk detected - type : DVD" >> /var/log/cdrip.log
	   /usr/bin/php /opt/vortexbox/docd_helper.php >> /var/log/cdrip.log
	 fi

	 /usr/bin/eject $CDDEVICE
	 echolog "Done." >> /var/log/cdrip.log
   fi
   
############################################################################
# Rip Blu-ray
# Rip Blu-ray using makemkv
############################################################################
   if [ "$CDTYPE" = "CD-DATA (Mode 1)" ]; then
     echolog "Optical disk detected - type : Blu-ray" >> /var/log/cdrip.log

	 if [ ! -f /usr/bin/ffmpeg ]; then
		echolog "Installing (required) ffmpg ..." >> /var/log/cdrip.log
		/usr/local/sbin/setup-mplayer >> /var/log/cdrip.log
	 fi

	 /usr/bin/php /opt/vortexbox/docd_helper_BR.php >> /var/log/cdrip.log

	 /usr/bin/eject $CDDEVICE
	 echolog "Done." >> /var/log/cdrip.log
   fi   
 fi

 
sleep 2
  done

done
