#! /bin/sh
#pBurn - Burning gui
#Copyright 2007,2008,2009,2010,2011,2012,2013,2014
#Sigmund Berglund - Valåmoen, Norway.

export TEXTDOMAIN=pburn
export OUTPUT_CHARSET=UTF-8

#------------------------------
#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 version 2.

#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. <http://www.gnu.org/licenses/>.
#------------------------------

export VERSION=4.3.10 #REMEMBER to update $APPDIR/pburnrc
export AUDIO_FORMATS="\.aac$|\.ac3$|\.aif$|\.aiff$|\.ape$|\.au$|\.flac$|\.gsm$|\.m4a$|\.mp1$|\.mp2$|\.mp3$|\.mp4$|\.ogg$|\.ra$|\.shn$|\.wav$|\.wma$|\.wv"
export VIDEO_FORMATS="\.asf$|\.avi$|\.flv$|\.mov$|\.m4v$|\.mkv$|\.mp4$|\.mpeg$|\.mpg$|\.vob$|\.wmv$"
export IMAGE_FORMATS="\.bmp$|\.gif$|\.jpeg$|\.jpg$|\.png$|\.xpm$"
#define paths
export APPDIR=`dirname $0` #Pburn directory
[ $APPDIR = '.' ] && export APPDIR=`pwd`
export WORKDIR=$HOME/.pburn/tmp
export CONFIG=$HOME/.pburn/pburnrc
export LOG=$WORKDIR/log
#check pfilesearch
type pfilesearch > /dev/null 2>&1 && export BOOLEAN_PFILESEARCH=true
if [ "$BOOLEAN_PFILESEARCH" = "true" ]; then
	[ ! -f $HOME/.pfilesearch/pfilesearchrc ] && pfilesearch -i #install pfilesearch rc
	export CONFIG_SEARCH=$HOME/.pfilesearch/pfilesearchrc
fi
#---
DIR=$HOME/ #default browser dir
mkdir -p $WORKDIR/ > /dev/null 2>&1
mkdir $HOME/.pburn/mnt/ > /dev/null 2>&1
mkdir $HOME/my-documents/tmp > /dev/null 2>&1

#if type gtkdialog4 > /dev/null 2>&1; then export GTKDIALOG=gtkdialog4; else export GTKDIALOG=gtkdialog; fi #puppy specific
export GTKDIALOG=gtkdialog

#parameters
while [ $# != 0 ]; do
	I=1
	while [ $I -le `echo $# | wc -c` ]; do #check -xft
		case $1 in
		*.pbn*)
			TMP1=`dirname "$1"`
			[ $TMP1 = '.' ] && TMP1="`pwd`"
			TMP2=`basename "$1"`
			export OPEN_FILE="$TMP1/$TMP2"
			;;
		*.iso*)
			TMP1=`dirname "$1"`
			[ $TMP1 = '.' ] && TMP1="`pwd`"
			TMP2=`basename "$1"`
			export OPEN_ISO="$TMP1/$TMP2"
			;;
		*)
			case $1 in
				-c) export CHECK_MEDIA=false;;
				-f) export RAW_BURNMODE=true;;
				-h|--help) . $APPDIR/func_man; echo; pburn -D; exit;;
				-r) rm -fr $HOME/.pburn; pburn \&; exit;;
				-v) echo Pburn $VERSION; exit;;
				#all parameters that 'shifts'
				-d) BROWSER_DIR="$2"; shift;;
				-i) export CHOOSER="$2"; shift;; #import list
				-m) export BURN_MODE=$2; shift;;
				-D)	echo -e "  \e[04mR E Q U I R E D                            \e[0m"
				echo -n '    bash                                      '
				if type bash > /dev/null 2>&1; then echo -e "\e[01;32m[OK]\e[0m"; else echo -e "\e[01;31m[MISSING]\e[0m"; fi
				echo -n '    coreutils, awk, sed                       '
				if type cp > /dev/null 2>&1 && type awk > /dev/null 2>&1 && type sed > /dev/null 2>&1; then echo -e "\e[01;32m[OK]\e[0m"; else echo -e "\e[01;31m[MISSING]\e[0m"; fi
				#gtkdialog
				TMP=`$GTKDIALOG --version | grep -F version | cut -d' ' -f3`
				echo -n '    gtkdialog                        >= 0.8.4 '
				if type $GTKDIALOG > /dev/null 2>&1 && [ "`echo $TMP | tr -d '.'`" -ge 084 ]; then echo -e "\e[01;32m[OK]\e[0m"; else echo -e "\e[01;31m[MISSING]\e[0m"; fi
				echo -n '    cdrtools                                  '
				if type cdrecord > /dev/null 2>&1 && [ "$(grep -F ' 3.' <<< "$(cdrecord --version)")" ]; then echo -e "\e[01;32m[OK]\e[0m"; else echo -e "\e[01;31m[MISSING]\e[0m"; fi
				echo -n '    dvd+rw-tools                              '
				if type growisofs > /dev/null 2>&1; then echo -e "\e[01;32m[OK]\e[0m"; else echo -e "\e[01;31m[MISSING]\e[0m"; fi
				echo -n '    cddetect                                  '
				if type cddetect_quick > /dev/null 2>&1; then echo -e "\e[01;32m[OK]\e[0m"; else echo -e "\e[01;31m[MISSING]\e[0m"; fi
				#---
				echo -e "\n  \e[04mO T H E R S                              \e[0m"
				echo -n '    ffmpeg      (audio/video)                 '
				if type ffmpeg > /dev/null 2>&1; then echo -e "\e[01;32m[OK]\e[0m"; else echo -e "\e[01;36m[??]\e[0m"; fi
				echo -n '    dvdauthor   (burning video-DVD)           '
				if type dvdauthor > /dev/null 2>&1; then echo -e "\e[01;32m[OK]\e[0m"; else echo -e "\e[01;36m[MISSING]\e[0m"; fi
				echo -n '    vobcopy     (copy video-DVD)              '
				if type vobcopy > /dev/null 2>&1; then echo -e "\e[01;32m[OK]\e[0m"; else echo -e "\e[01;36m[MISSING]\e[0m"; fi
				echo -n '    vamps       (shrinking video-DVD)         '
				if type vamps > /dev/null 2>&1; then echo -e "\e[01;32m[OK]\e[0m"; else echo -e "\e[01;36m[MISSING]\e[0m"; fi
				echo -n '    libcdio     (validate multisession)       '
				if type cd-info > /dev/null 2>&1; then echo -e "\e[01;32m[OK]\e[0m"; else echo -e "\e[01;36m[MISSING]\e[0m"; fi
				echo -n '    vcdimager   (burning video-CD)            '
				if type vcdimager > /dev/null 2>&1; then echo -e "\e[01;32m[OK]\e[0m"; else echo -e "\e[01;36m[MISSING]\e[0m"; fi
				echo -n '    nrg2iso     (burn Nero images)            '
				if type nrg2iso > /dev/null 2>&1; then echo -e "\e[01;32m[OK]\e[0m"; else echo -e "\e[01;36m[MISSING]\e[0m"; fi
				echo -n '    pFilesearch (filesearch)                  '
				if type pfilesearch > /dev/null 2>&1; then echo -e "\e[01;32m[OK]\e[0m"; else echo -e "\e[01;36m[MISSING]\e[0m"; fi
				exit
				;;
			esac
			;;
		esac
		shift
		I=$(($I+1))
	done
done
#splash
[ ! -f $WORKDIR/splash_image ] && echo '<pixmap icon_size="6"><input file stock="gtk-cdrom"></input></pixmap>' > $WORKDIR/splash_image
echo > $WORKDIR/splashtext
$APPDIR/box_splash &

#version upgrade
[ ! -f $CONFIG ] && cp -f $APPDIR/pburnrc $HOME/.pburn/pburnrc #no config --> copy
[ `grep -F VERSION $CONFIG | cut -d= -f2` != $VERSION > /dev/null 2>&1 ] && cp -f $APPDIR/pburnrc $HOME/.pburn/pburnrc #Other than this release

#set variables
. $CONFIG 2> /dev/null
export LANGUAGE="$MY_LANGUAGE"
[ "$MY_LANGUAGE" = "auto" ] && export LANGUAGE="${LANG%%_*}"
[ "$BOOLEAN_PFILESEARCH" = "true" ] && . $CONFIG_SEARCH

#install helptext
TMP_LANG=$LANGUAGE
[ ! -s /usr/share/doc/pburn/$LANGUAGE.html ] && export HLPFILE=/usr/share/doc/pburn.html || export HLPFILE=/usr/share/doc/pburn/$LANGUAGE.html
$APPDIR/func -install #check help variables
export LANGUAGE=$TMP_LANG
[ -s $HOME/.pburn/pburnhlp-$LANGUAGE ] && . $HOME/.pburn/pburnhlp-$LANGUAGE || . $HOME/.pburn/pburnhlp-auto #read help text (preferable in installed language)

#autodetect burner drive if not present in pburnrc
if [ ! "$BURNDEV" ]; then
	. $APPDIR/func -find_devices
	export BURNDEV="`echo "$CD_DRIVES" | grep -m 1 -iE 'rw|writer' | cut -f 1 -d " " | head -n 1 | tr -d "\n"`"
	[ ! "$BURNDEV" ] && BURNDEV=/dev/cdrom
fi

#theme
if [ -s $APPDIR/themes/$THEME/heading-main.png ]; then
	export HEIGHT_HEADING=`file "$APPDIR/themes/$THEME/heading-main.png" | cut -f7 -d\ | cut -f1 -d,`
	export HEIGHT_TREE=$[$HEIGHT_TREE-$HEIGHT_HEADING]
fi
if [ -s $APPDIR/themes/$THEME/button-burn.png ]; then
	export LABEL_BUTTON_BURN="<input file>$APPDIR/themes/$THEME/button-burn.png</input>"
else
	export LABEL_BUTTON_BURN="<input file icon=\"gtk-apply\"></input>"
fi
if [ -s $APPDIR/themes/$THEME/splash.png ]; then
	export SPLASH="<pixmap><input file>$APPDIR/themes/$THEME/splash.png</input></pixmap>"
else
	export SPLASH='<pixmap icon_size="6"><input file stock="gtk-cdrom"></input></pixmap>'
fi

#Startup dialog CHECK1:
#- If temporary storage is inside a virtual filesystem (unionfs)
#- burndrive is /dev/cdrom
#- OUTDIR doesn't exits/mounted
if [ $SKIP_STARTUP_CHECK = false ]; then
	#if several optical devices is detected, user must choose manually. CD_DRIVES is only valid at first run ($BURNDEV is missing)
	[ "`echo "$CD_DRIVES" | wc -l`" -gt 1 ] && STARTUP_CHECK=true
	#---
	. $APPDIR/func_gui_options #run this after $CD_DRIVES check to avoid startup-check becomes true every startup. $CD_DRIVES are defined at first run because $BURNDEV is missing
	#check if Pburn has detected a burner device.
	if [ "$BURNDEV" = "/dev/cdrom" ] || [ "`echo "$CD_DRIVES" | wc -l`" -gt 1 ]; then
		GUI_DRV='
<frame '$(gettext 'Burner device')'>
 <text><label>'$(gettext 'pBurn has not detected your burner drive, and is set to /dev/cdrom. This will often work ok, but to be sure you should define (click on) the correct one.')'</label></text>
 <entry visible="false">
  <variable>BURNDEV</variable>
  <input>cat '$WORKDIR'/burner</input>
 </entry>
 <tree rules_hint="true" space-expand="true" space-fill="true">
  <variable>DEVICELIST</variable>
  <label>'$(gettext 'Device')'|'$(gettext 'Media')'</label>
  <item stock="gtk-cdrom">/dev/cdrom|'$(gettext 'Generic')'</item>
  '$ITEMS_DEVICES'
  <action signal="button-release-event">echo $DEVICELIST > '$WORKDIR'/burner</action>
  <action signal="button-release-event">refresh:BURNDEV</action>
 </tree>
</frame>'
	fi
	#let's check if free space is bigger than 10GB free space.
	. $APPDIR/func -available_size
	if [ $FREE_SIZE -lt 10000 ]; then
		GUI_LOW_TEMP="<text use-markup=\"true\"><label>\"$(gettext "pBurn's <b>temporary storage is small</b>. This can cause problems when burning a large amount of data. If running a system inside a virtual filesystem (like Puppylinux), We recommend you to change this setting to a directory on a mounted hard drive. 10Gb of free space would do for most burning tasks.

<b>Temporary storage on a Windows FAT partition works mostly, but will fail for audio/video burning.</b> NTFS is fine.")\"</label></text>$GUI_TEMPORARY_STORAGE" #'
		STARTUP_CHECK=true
	fi
	#missing temporary-storage directory
	if [ ! -d "$OUTDIR" ]; then
		GUI_LOW_TEMP="<text use-markup=\"true\"><label>\"$(gettext '<b>Important!</b> Temporary storage does not exist, or points to an unmounted partition. Please change the location, or mount the partition. <b>NOW!</b>')\"</label></text>$GUI_TEMPORARY_STORAGE"
		STARTUP_CHECK=true
	fi
	#---

	if [ "$STARTUP_CHECK" = "true" ]; then
		echo "$OUTDIR" > $WORKDIR/OUTDIR
		export pburn_startup="
		<window title=\"pBurn\" icon-name=\"gtk-cdrom\">
		<vbox>
		 <frame $(gettext 'Temporary storage')>
		  <pixmap icon_size=\"6\"><input file stock=\"gtk-dialog-question\"></input></pixmap>
		  $GUI_LOW_TEMP
		 </frame>
		 $GUI_DRV
		 <hbox>
		  <checkbox>
		   <label>$(gettext 'Skip this message at next startup')</label>
		   <variable>SKIP_STARTUP_CHECK</variable>
		  </checkbox>
		  <button ok></button>
		 </hbox>
		</vbox>
		</window>"
		I=$IFS; IFS=""
		for STATEMENTS in  $($GTKDIALOG -p pburn_startup --center); do
			eval $STATEMENTS
		done
		IFS=$I
		#update settings
		PUBLISHER="$(cut -c 2- <<< $HOME)"
		. $APPDIR/func -write_config
		. $CONFIG
		. $APPDIR/func -available_size
		export TMPDIR="$OUTDIR/pburn_symlink_tree"
		[ ! -d "$TMPDIR" ] && mkdir -p "$TMPDIR"
	fi
fi

#if temporary storage is defined on a VFAT partition, move symlink tree to /tmp/
export TMPDIR="$OUTDIR/pburn_symlink_tree" #where all symlinks are kept
TMP="`mount | grep vfat | cut -d ' ' -f 3`"
[ "`echo $OUTDIR | grep "$TMP"`" ] && export TMPDIR="/tmp/pburn_symlink_tree"
#Startup dialog CHECK2: Stop if pburn-symlinks exist, or log has been accessed the last 15 min
if [ "`find -P $TMPDIR/ -mindepth 1 -cmin -15 2> /dev/null`" ] || [ "`find $WORKDIR/log -cmin -15 2> /dev/null`" ]; then
	TXT1="$(gettext 'Pburn is already running, or it was previously exited incorrectly.
<b>Clear storage, and continue?</b>')"
	. $APPDIR/box -yesno
	if [ $EXIT = No ]; then 
		echo 100 > $WORKDIR/splash
		exit
	fi
fi

#clean up
[ ! -d  $TMPDIR ] && mkdir -p $TMPDIR
rm -fr "$TMPDIR"/*
ls -A1 $WORKDIR | grep -v splash > $WORKDIR/tmp #do not remove splash info
while read I; do rm -fr $WORKDIR/$I; done < $WORKDIR/tmp
rm -f "$OUTDIR"/pburn*.iso > /dev/null 2>&1
echo -n > $WORKDIR/burn
. $APPDIR/func -new
#set browser
if type defaulthtmlviewer > /dev/null 2>&1; then export BROWSER=defaulthtmlviewer
	elif type dillo > /dev/null 2>&1; then export BROWSER=dillo
	elif type netsurf > /dev/null 2>&1; then export BROWSER=netsurf
	elif type midori > /dev/null 2>&1; then export BROWSER=midori
	elif type opera > /dev/null 2>&1; then export BROWSER=opera
	elif type seamonkey > /dev/null 2>&1; then export BROWSER=seamonkey
	elif type firefox > /dev/null 2>&1; then export BROWSER=firefox
	elif type chromium > /dev/null 2>&1; then export BROWSER=chromium
	elif type chrome > /dev/null 2>&1; then export BROWSER=chrome
	elif type iron > /dev/null 2>&1; then export BROWSER=iron
fi
#---

echo "$OUTDIR" > $WORKDIR/OUTDIR
echo "$SHOW_HIDDEN" > $WORKDIR/SHOW_HIDDEN
[ "$BROWSER_DIR" ] && DIR="$BROWSER_DIR" #alternative browser dir, -d parameter is used 
echo > $WORKDIR/isosize #size calculation
echo "$TMPDIR" > $WORKDIR/burnlist_dir #TMPDIR is cd /
echo "$(gettext 'CD/DVD/Blu-ray Burner')" > $WORKDIR/open_file
rm $WORKDIR/end_while 2> /dev/null #start disc_checking_daemon (progressbar)
BURN_DATA=true; BURN_AUDIO=false; BURN_VIDEO=false
. $APPDIR/func -available_size
. $APPDIR/func -browser_go_to_dir
if [ "$OPEN_FILE" ]; then
	echo "$OPEN_FILE" > $WORKDIR/OPEN_FILE
	. $APPDIR/func -open #open files at start. Defined file from commandline
fi
. $APPDIR/func -info_burn
export CDRECORD="cdrecord"
export CDDA2WAV="cdda2wav"
export MKISOFS="mkisofs"
#---
echo "$SPLASH" > $WORKDIR/splash_image
#import list, -i parameter is used
[ "$CHOOSER" ] && . $APPDIR/func -add_list

#---
if [ "$OPEN_ISO" ]; then #Do NOT show main window. Iso-image is defined from commandline
	echo "|$OPEN_ISO|" > $WORKDIR/burn
	echo "$OPEN_ISO=$OPEN_ISO" > $WORKDIR/graft_points
	#none of these files contains 'correct' syntax, but enough to be recognized correct
	BURN_MODE=iso
fi
case $BURN_MODE in
	blank) 		echo 100 > $WORKDIR/splash; BURN=blank; 		. $APPDIR/func_burn_options;;
	copy-audio)	echo 100 > $WORKDIR/splash; BURN=copy_audio;	. $APPDIR/func_burn_options;;
	copy-data)	echo 100 > $WORKDIR/splash; BURN=copy;			. $APPDIR/func_burn_options;;
	copy-video)	echo 100 > $WORKDIR/splash; BURN=copy_video;	. $APPDIR/func_burn_options;;
	iso)		echo 100 > $WORKDIR/splash; BURN=iso;			. $APPDIR/func_burn_options;;
	info)		. $APPDIR/func -disc_info;;
	data)
		BURN_DATA=true; BURN_AUDIO=false; BURN_VIDEO=false
		[ "$CHOOSER" ] && . $APPDIR/func_burn_options || . $APPDIR/func_gui
		;;
	audio)
		BURN_DATA=false; BURN_AUDIO=true; BURN_VIDEO=false
		[ "$CHOOSER" ] && . $APPDIR/func_burn_options || . $APPDIR/func_gui
		;;
	video)
		BURN_DATA=false; BURN_AUDIO=false; BURN_VIDEO=true
		[ "$CHOOSER" ] && . $APPDIR/func_burn_options || . $APPDIR/func_gui
		;;
	*)
		. $APPDIR/func_gui
		;;
esac

#quit
. $APPDIR/func -new
echo end > $WORKDIR/end_while #stop disc_checking_daemon (progressbar)
for I in `ps -eo pid,command | grep pburn | awk '{print $1}'`; do kill -9 $I 2> /dev/null; done
exit 0
