#!/bin/bash
########################################################
#                   ddCopy dvd                         #
#        Simple GUI DVD burner for ddCopy              #
#        © 2023 WTFPL lxgator@gmail.com 	 	       #
########################################################
Title='ddCopy'
Version='0.3.3'
ICON="/usr/share/icons/hicolor/48x48/apps/ddcopy-dvd.svg"
WICON="/usr/share/ddcopy/data/icons/error.svg"
CONF="/tmp/ddcopy"
MICON="/usr/share/ddcopy/data/icons/ddcopy-dvd.svg"

# Create a tmp folder
rm -rf /tmp/ddcopy 2>&1 > /dev/null
if ! test -d /tmp/ddcopy  ; then
mkdir -p /tmp/ddcopy
chmod 0777 /tmp/ddcopy
fi
function ddcopy_dvd {
if ! [ -s $PID ]; then
wodim dev=/dev/sr2 --devices | awk '0 == NR % 3' > $CONF/ddcopy_dvd
sleep 0.50
wodim dev=/dev/sr1 --devices | awk '0 == NR % 3' > $CONF/ddcopy_dvd
sleep 0.50
wodim dev=/dev/sr0 --devices | awk '0 == NR % 3' > $CONF/ddcopy_dvd
fi
sleep 1
if [ -s $CONF/ddcopy_dvd ]
then
   exec
rm -f $CONF/ddcopy_dvd.log 2>&1 > /dev/null
else
python3 /usr/lib64/ddcopy/dvd_notify.py &
rm -rf /tmp/ddcopy 2>&1 > /dev/null
exit 0
fi
$(yad \
--title="ddCopy" \
--window-icon=$MICON \
--image=$ICON \
--center --on-top --fixed \
--width=475 --height=100 \
--text="\nBurning iso file to a DVD-R with ddCopy\n\nThis option will take additional time... Be patient!\nEnsure that a Blank DVD-R is placed in the Tray\n" \
--button="Cancel"!/usr/share/ddcopy/data/icons/cancel.svg:1 \
--button="Ok"!/usr/share/ddcopy/data/icons/ok.svg:0) 
if [ $? = "1" ]; then
rm -rf /tmp/ddcopy 2>&1 > /dev/null
exit 0
else
exec
fi
FILENAME=$(yad \
--title="ddCopy dvd" \
--window-icon=$MICON \
--text="<b>Select iso to write to DVD</b>" \
--file --file-filter='ISO |*.iso' \
--center \
--width=750 \
--height=550 \
--button="Cancel"!/usr/share/ddcopy/data/icons/cancel.svg:3 \
--button="Ok"!/usr/share/ddcopy/data/icons/ok.svg:0)
if [ $? = "3" ]; then
rm -rf /tmp/ddcopy 2>&1 > /dev/null
exit 0
fi
if [ ${FILENAME: -4} == ".iso" 2>&1 ]; then
    exec
else
rm -rf /tmp/ddcopy 2>&1 > /dev/null
  exit 0
fi
SPEED=$(yad \
--title="ddCopy" \
--window-icon=$MICON \
--image=$ICON \
--center --fixed \
--width=375 --height=100 \
--button="Cancel"!/usr/share/ddcopy/data/icons/cancel.svg:5 \
--button="Ok"!/usr/share/ddcopy/data/icons/ok.svg:0 \
--entry \
--text="<b>Burning iso File to DVD</b>\n\nSelect a write speed:" \
"Write speed 3x" \
"Write speed 6x" \
"Write speed 12x")
if [ $? = "5" ]; then
exit 0
fi
if [ -n "${SPEED}" ];then
case $SPEED in
"Write speed 3x")
echo "$FILENAME" > $CONF/fls
CONF="/tmp/ddcopy"
while true; do 
if [ $1 ]
then
    date1=`date +%s`+$1;
else
    date1=`date +%s`;
fi
while true; do 
echo "#Burning the iso file... Please wait!   Elapsed time: $(date -u --date @$((`date +%s` - $date1)) +%M:%S) minutes" 2>/dev/null
sleep 1
done
sleep 10m
done | yad --window-icon=$ICON \
--image=$ICON \
--on-top --width=500 --progress --pulsate --title="ddCopy" --text="
<b>Burning the iso File to DVD-ROM Device</b>" \
--button=Close!/usr/share/ddcopy/data/icons/cancel.svg --auto-close &
DEVICE=(`egrep -o "'[^\.,;']+" $CONF/ddcopy_dvd | cut -c2- | awk 'NR==1{print $0}'`)
wodim -v -pad -eject speed=3 dev=${DEVICE} $FILENAME 2>&1 | tee /tmp/ddcopy/ddcopy_dvd.log
if [ -s /tmp/ddcopy/ddcopy_dvd.log ]
then
pkill -SIGKILL ddcopy_dvd 2>&1 > /dev/null &
notify-send -t 20000 -i $ICON "DVD Burn Finished!"
else
notify-send -t 20000 -i $WICON "Blank DVD Not Found!"
pkill -SIGKILL ddcopy_dvd 2>&1 > /dev/null
fi
 ;;
"Write speed 6x")
echo "$FILENAME" > $CONF/fls
CONF="/tmp/ddcopy"
while true; do 
if [ $1 ]
then
    date1=`date +%s`+$1;
else
    date1=`date +%s`;
fi
while true; do 
echo "#Burning the iso file... Please wait!   Elapsed time: $(date -u --date @$((`date +%s` - $date1)) +%M:%S) minutes" 2>/dev/null
sleep 0.1
done 
done | yad --window-icon=$ICON \
--image=$ICON \
--on-top --width=500 --height=100 --fixed --progress --pulsate --title="ddCopy" --text="
<b>Burning the iso File to DVD-ROM Device</b>" \
--button=Close!/usr/share/ddcopy/data/icons/cancel.svg --auto-close &
DEVICE=(`egrep -o "'[^\.,;']+" $CONF/ddcopy_dvd | cut -c2- | awk 'NR==1{print $0}'`)
wodim -v -pad -eject speed=6 dev=${DEVICE} $FILENAME 2>&1 | tee /tmp/ddcopy/ddcopy_dvd.log
if [ -s /tmp/ddcopy/ddcopy_dvd.log ]
then
pkill -SIGKILL ddcopy_dvd 2>&1 > /dev/null &
notify-send -t 20000 -i $ICON "DVD Burn Finished!"
else
notify-send -t 20000 -i $WICON "Blank DVD Not Found!"
pkill -SIGKILL ddcopy_dvd 2>&1 > /dev/null
fi
 ;;
"Write speed 12x")
echo "$FILENAME" > $CONF/fls
CONF="/tmp/ddcopy"
while true; do 
if [ $1 ]
then
    date1=`date +%s`+$1;
else
    date1=`date +%s`;
fi
while true; do 
echo "#Burning the iso file... Please wait!   Elapsed time: $(date -u --date @$((`date +%s` - $date1)) +%M:%S) minutes" 2>/dev/null
sleep 1
done
sleep 10m
done | yad --window-icon=$ICON \
--image=$ICON \
--on-top --width=500 --progress --pulsate --title="ddCopy" --text="
<b>Burning the iso File to DVD-ROM Device</b>" \
--button=Close!/usr/share/ddcopy/data/icons/cancel.svg --auto-close &
DEVICE=(`egrep -o "'[^\.,;']+" $CONF/ddcopy_dvd | cut -c2- | awk 'NR==1{print $0}'`)
wodim -v -pad -eject speed=12 dev=${DEVICE} $FILENAME 2>&1 | tee /tmp/ddcopy/ddcopy_dvd.log
if [ -s /tmp/ddcopy/ddcopy_dvd.log ]
then
pkill -SIGKILL ddcopy_dvd 2>&1 > /dev/null &
notify-send -t 20000 -i $ICON "DVD Burn Finished!"
else
notify-send -t 20000 -i $WICON "Blank DVD Not Found!"
pkill -SIGKILL ddcopy_dvd 2>&1 > /dev/null
fi
 ;;
esac
fi
 }
ddcopy_dvd
rm -rf /tmp/ddcopy 2>&1 > /dev/null
