#! /bin/sh
#Pburn - Burning gui
#Copyright 2007,2008,2009
#Sigmund Berglund - Valåmoen, Norway.

#------------------------------
#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=3.1.3 #REMEMBER to update $APPDIR/pburnrc and version upgrade
export AUDIO_FORMATS="\.aiff$|\.aac$|\.ape$|\.au$|\.flac$|\.gsm$|\.m4a$|\.mp3$|\.mp4$|\.ogg$|\.ra$|\.shn$|\.wav$|\.wma$"
export VIDEO_FORMATS="\.asf$|\.avi$|\.flv$|\.mov$|\.m4v$|\.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
[ ! -f $HOME/.pfilesearch/pfilesearchrc ] && pfilesearch -i #install pfilesearch rc
export CONFIG_SEARCH=$HOME/.pfilesearch/pfilesearchrc
DIR=$HOME/ #default browser dir
mkdir -p $WORKDIR/ > /dev/null 2>&1
mkdir $HOME/.pburn/mnt/ > /dev/null 2>&1
if type gtkdialog3 > /dev/null; then export GTKDIALOG=gtkdialog3; else export GTKDIALOG=gtkdialog; fi #puppy specific
#parameters
while [ $# != 0 ]; do
	I=1
	while [ $I -le `echo $# | wc -c` ]; do #check -xft
		case $1 in *.pbn*) export OPEN_FILE="$1"; esac
		case $1 in *.iso*)
			TMP1="`pwd $1`"
			TMP2=`basename "$1"`
			export OPEN_ISO="|$TMP1/$TMP2"
		esac
		if [ "`echo $1 | grep -v '-' | grep '/'`" ]; then shift; I=$(($I+1)); continue; fi
		[ `echo $1 | grep c` ] && export CHECK_MEDIA=false
		[ `echo $1 | grep f` ] && export RAW_BURNMODE=true
		if [ `echo $1 | grep h` ]; then . $APPDIR/func_man; exit; fi
		if [ `echo $1 | grep r` ]; then rm -fr $HOME/.pburn; pburn \&; exit; fi
		if [ `echo $1 | grep v` ]; then echo Pburn $VERSION; exit; fi
		#all parameters that 'shifts'
		if [ `echo $1 | grep d` ]; then BROWSER_DIR="$2"; shift; fi
		if [ `echo $1 | grep g` ]; then BURN_ENGINE="$2"; shift; fi
		if [ `echo $1 | grep i` ]; then export CHOOSER="$2"; shift; fi #import list
		if [ `echo $1 | grep m` ]; then BURN_MODE=$2; shift; fi
		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
TMP=`grep VERSION $CONFIG 2> /dev/null`
[ `echo $TMP | sed -e s/^.*\=//g | tr -d "."` -lt 313 > /dev/null 2>&1 ] && \
	cp -f $APPDIR/pburnrc $HOME/.pburn/pburnrc  #Older than 3.1.1
#set variables
. $CONFIG 2> /dev/null
. $CONFIG_SEARCH
#autodetect burner drive if not present in pburnrc
if [ ! "$BURNDEV" ]; then
	export BURNDEV="`echo "$CD_DRIVES"  | grep -iE 'rw|writer' | cut -f 1 -d " " | head -n 1 | tr -d "\n"`"
	[ ! "$BURNDEV" ] && BURNDEV=/dev/cdrom
fi
#set language
[ "$LANGUAGE" = "auto" ] && TMP="`ls -1 $APPDIR/locals/ | grep $LANG`"
. $APPDIR/locals/en_US:english #always run to fill gaps in translation
[ "$TMP" != "en_US:english" ] && . $APPDIR/locals/$TMP 2> /dev/null
#set window height
export HEIGHT_SCREEN=`xwininfo -root | grep -m 1 '\geometry' | cut -f4 -d ' ' | cut -f2 -d 'x' | cut -f1 -d '+'`
[ ! "$HEIGHT_SCREEN" ] && export HEIGHT_SCREEN=600 #in case xwininfo doesn't return any worthy.
HEIGHT_TREE=$(($HEIGHT_SCREEN-319+$WINDOW_HEIGHT)) #maximize window by defining the height of burnlist <tree>
#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
. $APPDIR/func_gui_options
[ "$BURNDEV" = "/dev/cdrom" ] && GUI_DRV="<frame $LOC260><text><label>$LOC334</label></text>$GUI_DEV</frame>"
TXT="$LOC333"
if [ ! -d "$OUTDIR" ]; then STARTUP_CHECK=true; TXT="$LOC344"; fi
if [ -d '/initrd/pup_rw'"$OUTDIR" ] && [ ! "`mount | grep "$OUTDIR"`" ]; then STARTUP_CHECK=true; fi
#---
if [ "$STARTUP_CHECK" = "true" ]; then
	echo "$OUTDIR" > $WORKDIR/OUTDIR
	$APPDIR/func -available_size
	export Pburn_startup="
	<window title=\"Pburn\" icon-name=\"gtk-cdrom\">
	<vbox>
	 <frame $LOC239>
      <pixmap icon_size=\"6\"><input file stock=\"gtk-dialog-question\"></input></pixmap>
	  <text use-markup=\"true\"><label>\"$TXT\"</label></text>
	  $GUI_TEMPORARY_STORAGE
	 </frame>
	 $GUI_DRV
	 <hbox>
	  <button ok></button>
	 </hbox>
	</vbox>
	</window>"
	I=$IFS; IFS=""
	for STATEMENTS in  $($GTKDIALOG --program=Pburn_startup --center); do
		eval $STATEMENTS
	done
	IFS=$I
	#update settings
	. $APPDIR/func -write_config
	. $CONFIG
	. $APPDIR/func -available_size
	export TMPDIR="$OUTDIR/pburn_symlink_tree"
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="$LOC323"
	. $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 which defaulthtmlviewer > /dev/null 2>&1; then export BROWSER=defaulthtmlviewer
	elif which dillo; then export BROWSER=dillo
	elif which opera; then export BROWSER=opera
	elif which seamonkey; then export BROWSER=seamonkey
	elif which firefox; then export BROWSER=firefox
	elif which konqueror; then export BROWSER=konqueror
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 "$LOC001" > $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
[ "$OPEN_FILE" ] && . $APPDIR/func -open #open files at start. Defined file from commandline
. $APPDIR/func -info_burn
#check external programs
if [ "$BURN_ENGINE" ]; then #user has used the -e switch to choose cdrtools or cdrkit
	case $BURN_ENGINE in
		cdrtools) export CDRECORD="cdrecord"; export CDDA2WAV="cdda2wav"; export MKISOFS="mkisofs";;
		cdrkit) export CDRECORD="wodim"; export CDDA2WAV="icedax"; export MKISOFS="genisoimage";;
	esac
else #let Pburn define burn engine. cdrtools is prefered if both is present.
	export CDRECORD="wodim"; export CDDA2WAV="icedax"; export MKISOFS="genisoimage"
	if which cdrecord > /dev/null 2>&1; then export CDRECORD="cdrecord"; export CDDA2WAV="cdda2wav"; export MKISOFS="mkisofs"; fi
fi
if which normalize > /dev/null 2>&1; then export VISIBLE_NORMALIZE=enabled; else export VISIBLE_NORMALIZE=disabled; fi
#---
echo "$SPLASH" > $WORKDIR/splash_image
#import list, -i parameter is used
[ "$CHOOSER" ] && $APPDIR/func -add_list
#install helptext
$APPDIR/func -install #check help variables
. $HOME/.pburn/pburnhlp-$LANGUAGE #read help text (in installed language)
#---
if [ "$OPEN_ISO" ]; then #Do NOT show main window. Iso-image is defined from commandline
	echo "$OPEN_ISO" > $WORKDIR/burn
	BURN_MODE=iso
fi
case $BURN_MODE in
	audio) BURN_DATA=false; BURN_AUDIO=true; BURN_VIDEO=false; . $APPDIR/func_gui;;
	video) BURN_DATA=false; BURN_AUDIO=false; BURN_VIDEO=true; . $APPDIR/func_gui;;
	blank) BURN=blank; . $APPDIR/func_burn_options;;
	copy-audio) BURN=copy_audio; . $APPDIR/func_burn_options;;
	copy-data) BURN=copy; . $APPDIR/func_burn_options;;
	copy-video) BURN=copy_video; . $APPDIR/func_burn_options;;
	iso) BURN=iso; . $APPDIR/func_burn_options;;
	*) . $APPDIR/func_gui;;
esac
