#!/bin/bash

# Program: pAVrecord version 0.9.4 (special version for Puppy Precise)
# Revision date (YMD): 2014/06/19 (original 2012/10/06                                      
# (C) Copyright 2009-2014+ William McEwan; GPLv3                 
#
# Depends on: +ffmpeg,+ffplay,+gtkdialog,+lame,+alsa-utils
#
# Changes:
# Version precise: using libvo_aacenc instead of libfaac
#	using -vcodec mpeg4 -vtag xvid instead of libxvid  
# v.0.9.3 2014/6/3 (YMD): uses xterm if rxvt not available (for DebianDog)
# (YMD)2014/01/26: trap ERR added
# (YMD)2014/06/19: function VU workaround to not overwrite /dev/null by SFR (Jake)

! command -v ffmpeg >/dev/null && { Xdialog --title "pavrecord" --msgbox "This program depends on ffmpeg to work.
Please, install ffmpeg first with Synaptic or apt-get.
Or use ffmpeg2sfs to create ffmpeg.sfs and load it." 0 0; exit 0; }

trap default INT TERM ERR EXIT

[ $(which rxvt) ] && export VTERMINAL="rxvt" || export VTERMINAL="xterm"

export TEXTDOMAIN=pavrecord
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8

Pversion_="0.9.4"
programtitle="pAVrecord $Pversion_"
export PROGRAMNAME="pavrecord"
export PROGINSTANCENAME=${PROGRAMNAME}
export PROGRAMHOME=${HOME}/.${PROGRAMNAME}
CFG_NAME=".pavrecordrc"
CONFIGFILE=$PROGRAMHOME/"${CFG_NAME}"

if [ ! -s /etc/${PROGRAMNAME}/"${CFG_NAME}" ];then
 printf "$(gettext "Default /etc/${PROGRAMNAME} ${CFG_NAME} config file missing, please reinstall ${PROGRAMNAME}\n")"
 exit 1
else
 if [ ! -d $PROGRAMHOME ];then
  cp -a /etc/${PROGRAMNAME} ${HOME}/.${PROGRAMNAME}
 else
  [ ! -s "$CONFIGFILE" ] && cp -a /etc/${PROGRAMNAME}/"${CFG_NAME}" $PROGRAMHOME
 fi
fi

[ ! -e /tmp/${PROGINSTANCENAME} ] && mkdir /tmp/${PROGINSTANCENAME}
echo $$ > /tmp/${PROGINSTANCENAME}/PIDstartbinsh

. "$CONFIGFILE" 

function helpsetup (){
  printf "$(gettext "Section A: Commandline usage

In a console enter command: 
pavrecord --help
for brief commandline usage.
")""$(gettext "
-----
Section B:

Take note that when not recording you can pop-up VU meter, audio mixer, and video preview window at any time by pressing record STOP button or MIXER button. Also be careful with timed recordings: once Pschedule is called up by pAVrecord, Pschedule will automatically make recordings every minute by default if you do not put in any values or cancel the entry properly.

-----
Setting up pAVrecord Example:

Select Advanced (big) User Interface.

Select Target Directory: /root

Select Record Duration: 0 (for unlimited).
Also set Play Duration to 0.

Enter Audio sync offset (seconds): 1.5

In AUDIO RECORD CONFIGURATION:

Select: mp3 (or ogg_q), cbr, bitrate 32k

In VIDEO RECORD CONFIGURATION:

Select video encoder: MPEG-4/10 (or Theora if ogg_q audio)

")""$(gettext "
Enter:
inRate(fps) 30, 
vINPUTFORMAT video4linux2,
vDEVICE /dev/video0 (if that is your video device),
vWidth 320, vHEIGHT 240.
Leave ffmpeg extra v o/p options blank
       
Select container for combined A and V track: avi (or ogv if ogg_q and Theora)

For Combine AV auto, select as true.
 
For Combine AV del, select as false. 
Note that the alternative true for checkbox del causes the separate A and V tracks to be automatically deleted after the final combined AV is produced.

Press Record button to record the A and V tracks (which, with above setting, are auto-combined into final avi container once Record Stop button is pressed). 

Three files are saved: the date-stamped video track, the date-stamped audio track and the combined AV video whose name begins pAV followed by date stamp.

")""$(gettext "
-----
To view/hear result:

Method 1: Use button -> to transfer the file name to the play side and press Play button. This method plays the individual video and audio tracks in parallel (allowing you to adjust the Audio sync offset).

Method 2: If audio sync offset set up is already fine, you can simply press the AV Play button (next to Combine AV area) to play final combined AV video in your system video player (assuming Combine AV auto was selected).
")""$(gettext "
-----
Section C: Pscheduler:

If Pscheduler is available, you can Add (or Edit) Task(s) of the form 

  pavrecord rec outfile.avi [duration]

to schedule when a recording session should start.

You can schedule any of the commands described by: pavrecord --help
")""$(gettext "
-----
Section D: Alsa setup

Recording:

1. For louder microphone recordings: 

   Press MIXER button to bring up audio mixer and the VU meter. Activate Mic boost. Using it greatly boosts your microphone sound recording level.
   
   If using alsamixer to do this, use the cursor keys, move far over to the right hand side of the alsamixer window, and keep going right (often off the edge of the window). Around that location you may (or should?) find the 'Mic Boost' control (keyboard key 'M' toggles its activation).

")""$(gettext "

2. For Microphone recording, you should also activate 'Mic' as the capture device (if using alsamixer use space bar to activate).

3. You should turn up the amplitude setting of the 'Capture' (to around maximum), but be careful you do not unwittingly turn up the Playback Mic volume (which you should inactivate) rather than the Capture one, or you may end up recording sound fed back to your mic from your blaring speakers! If using alsamixer, pressing the Tab key such that you come to the alsamixer Capture window, before setting capture devices, may help you to avoid unnecessarily increasing the playback amplitude controls rather than the record ones.
")""$(gettext "

4. As an alternative to recording from the microphone, you could select some other device to capture from (e.g. Line in). If you select 'Mix' (or perhaps 'Mono Mix' as the capture device; depending on what your sound card provides) you can record most anything passing through your sound card (for example: streaming radio as in 01micko pupRadio program).

5. For playback to work you need to activate and turn up PCM and your Master volume control.

6. You will be able to see from the running VU display window if, and when, you have your sound controls correctly set up (but turn off the VU display before actually recording or you will end up with a blank recording!).
")""$(gettext "

Playback: 

1. For Playback, you need to turn up the PCM device volume and at least the Master device volume. Note that key 'M' is also used to Mute devices.
-----
Tips:

To keep pAVrecord as the top window, right-click on its window bar, and select 
Layer -> Top")" > /tmp/${PROGINSTANCENAME}/helpsetup.txt
}

function default (){
  quit
  [ -f "$CONFIGFILE" ] &&  rm -f "$CONFIGFILE" 2>/dev/null
  [ -d /tmp/${PROGINSTANCENAME}/ ] && rm -rf /tmp/${PROGINSTANCENAME}/ 2>/dev/null
  exit
}

function dirfilefix (){
 SDuse="$1"; SFuse="$2"
 SDuse=$(echo "${SDuse}" | sed 's/^[ 	]*//;s/[ 	]*$//')
 SFuse=$(echo "${SFuse}" | sed 's/^[ 	]*//;s/[ 	]*$//')
 lfd=$(dirname "$SFuse")
 if [ "$lfd" != "." ];then
  SDuse="$lfd";SFuse=$(basename "$SFuse")
 fi
}

function play_combined (){
	dirfilefix "$1" "$2"
	AddExtensions
    if [ $(which gnome-mplayer 2>/dev/null) ];then
	 $VTERMINAL -geometry "1x1-0-0" -e gnome-mplayer "${SDuse}/pAV${SFuse%%.*}.${avformat}" &
    elif [ $(which mplayer 2>/dev/null) ];then
	 $VTERMINAL -geometry "1x1-0-0" -e mplayer -geometry "+0+0" -title "pAVrecord mplayer" "${SDuse}/pAV${SFuse%%.*}.${avformat}" &
    else
     ffplay -window_title "pAVrecord ffplay" -i "${SDuse}/pAV${SFuse%%.*}.${avformat}" >/dev/null &
    fi
}

function make_avideo (){
	dirfilefix "$1" "$2"
	AddExtensions
	vformat="avi"
	ffmpeg -y -i "${SDuse}/${SFuse%%.*}.${vformat}" -ss $AOFFSET -i "${SDuse}/${SFuse%%.*}.${aformat}" -acodec copy -vcodec copy "${SDuse}/pAV${SFuse%%.*}.${avformat}" && if [ $AUTODEL = "true" ];then rm -f "${SDuse}/${SFuse%%.*}.${vformat}";rm -f "${SDuse}/${SFuse%%.*}.${aformat}";fi&
}

function recVideoTrack (){
	if [ $vENCODE00 = "true" ];then vcodec="-vcodec mpeg1video" 
	elif [ $vENCODE01 = "true" ];then vcodec="-vcodec mpeg2video"
	elif [ $vENCODE02 = "true" ];then vcodec="-vcodec mpeg4 -vtag xvid"
	elif [ $vENCODE03 = "true" ];then vcodec="-vcodec libtheora -qscale 31"
	elif [ $vENCODE04 = "true" ];then vcodec="-vcodec libx264 -b 200k -bt 240k"
	elif [ $vENCODE05 = "true" ];then vcodec=""
	fi
  if [ $X11GRAB = "true" ];then $VTERMINAL -geometry "1x1-0-0" -e sh -c "ffmpeg -y $ffmpegtime -f x11grab $INvENC_OPTS -s ${X11WIDTH}x${X11HEIGHT} -i ${XDSPLY}+${XOFS},${YOFS} -r $xOUTFRATE $vcodec -f avi $OUTvENC_OPTS \"${SDuse}/${SFuse%%.*}.avi\"" &
  else
	if [ $vENCODE00 = "true" ];then $VTERMINAL -geometry "1x1-0-0" -e sh -c "ffmpeg -y $ffmpegtime -f \"$vINPUTFORMAT\" -s ${vWIDTH}x${vHEIGHT} $INvENC_OPTS -i \"$vDEVICE\" -r $OUTFRATE $vcodec -f avi $OUTvENC_OPTS - | tee \"${SDuse}/${SFuse%%.*}.avi\" | ffplay -window_title pAVrecord -" &
	elif [ $vENCODE01 = "true" ];then $VTERMINAL -geometry "1x1-0-0" -e sh -c "ffmpeg -y $ffmpegtime -f \"$vINPUTFORMAT\" -s ${vWIDTH}x${vHEIGHT} $INvENC_OPTS -i \"$vDEVICE\" -r $OUTFRATE $vcodec -f avi $OUTvENC_OPTS - | tee \"${SDuse}/${SFuse%%.*}.avi\" | ffplay -window_title pAVrecord -" &
	elif [ $vENCODE02 = "true" ];then $VTERMINAL -geometry "1x1-0-0" -e sh -c "ffmpeg -y $ffmpegtime -f \"$vINPUTFORMAT\" -s ${vWIDTH}x${vHEIGHT} $INvENC_OPTS -i \"$vDEVICE\" -r $OUTFRATE $vcodec -f avi $OUTvENC_OPTS - | tee \"${SDuse}/${SFuse%%.*}.avi\" | ffplay -window_title pAVrecord -" &
	elif [ $vENCODE03 = "true" ];then $VTERMINAL -geometry "1x1-0-0" -e sh -c "ffmpeg -y $ffmpegtime -f \"$vINPUTFORMAT\" -s ${vWIDTH}x${vHEIGHT} $INvENC_OPTS -i \"$vDEVICE\" -r $OUTFRATE $vcodec -f avi $OUTvENC_OPTS - | tee \"${SDuse}/${SFuse%%.*}.avi\" | ffplay -window_title pAVrecord -" &
	elif [ $vENCODE04 = "true" ];then $VTERMINAL -geometry "1x1-0-0" -e sh -c "ffmpeg -y $ffmpegtime -f \"$vINPUTFORMAT\" -s ${vWIDTH}x${vHEIGHT} $INvENC_OPTS -i \"$vDEVICE\" -r $OUTFRATE $vcodec -f avi -b 200k -bt 240k $OUTvENC_OPTS - | tee \"${SDuse}/${SFuse%%.*}.avi\" | ffplay -window_title pAVrecord -" &
	elif [ $vENCODE05 = "true" ];then OUTFRATE=1;vformat="jpeg";filename="_%05d";$VTERMINAL -geometry "1x1-0-0" -e sh -c "ffmpeg -y $ffmpegtime -f \"$vINPUTFORMAT\" -s ${vWIDTH}x${vHEIGHT} $INvENC_OPTS -i \"$vDEVICE\" -r $jOUTFRATE -f image2 $OUTvENC_OPTS \"${SDuse}/${SFuse%%.*}_%05d.jpeg\"" &
	fi
  fi
  echo $! > /tmp/${PROGINSTANCENAME}/PIDvidrec2
  sleep 0.2
  PIDvidrec=$(ps u | grep "ffmpeg -y $ffmpegtime-f video4linux2" | awk '$11 == "ffmpeg" {print $2}')
  echo $PIDvidrec > /tmp/${PROGINSTANCENAME}/PIDvidrec
}

function AddExtensions (){
	if [ $vCONTAINER00 = "true" ];then avformat="avi" 
	elif [ $vCONTAINER01 = "true" ];then avformat="mpeg"
	elif [ $vCONTAINER02 = "true" ];then avformat="mp2"
	elif [ $vCONTAINER03 = "true" ];then avformat="mp4"
	elif [ $vCONTAINER04 = "true" ];then avformat="ogv"
	elif [ $vCONTAINER05 = "true" ];then avformat="mkv"
	fi
	if [ $ENCODEMP3 = "true" ];then aformat="mp3" 
	elif [ $ENCODEOGGQ = "true" -o $ENCODEOGGMM = "true" ];then aformat="ogg"
	elif [ $ENCODEWAV = "true" ];then aformat="wav"
	elif [ $ENCODEAAC = "true" -o $ENCODEAACQ = "true" ];then aformat="aac"
	elif [ $ENCODEFLAC = "true" ];then aformat="flac"
	fi
}

function DateStamp1 (){
 if [ ! -e /tmp/${PROGINSTANCENAME}/PIDrec ];then
  vformat="avi"
  echo "$(date +%Y_%m_%d_%H%M%S).${vformat}"
 fi
}

function preview (){
	[ -e /tmp/${PROGINSTANCENAME}/PIDpreview ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDpreview 2>/dev/null) 2>/dev/null
	sleep 0.2
	$VTERMINAL -geometry "1x1-0-0" -e mplayer tv:// -tv driver=v4l2:width=$vWIDTH:height=$vHEIGHT:device=$vDEVICE -fps $OUTFRATE -geometry "+0+64" -title "pAVrecord $(gettext 'preview')" &
	echo $! > /tmp/${PROGINSTANCENAME}/PIDpreview 
}

function vu (){
 [ -e /tmp/${PROGINSTANCENAME}/PIDvu ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDvu 2>/dev/null) 2>/dev/null
 [ -e /tmp/${PROGINSTANCENAME}/PIDalsamixer ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDalsamixer 2>/dev/null) 2>/dev/null
 preview 
 $VTERMINAL -title "$(gettext 'VU peak level meter')" -geometry "64x1+0+0" -e arecord -vv /tmp/${PROGINSTANCENAME}/vu_tmp >/dev/null &
 echo $! > /tmp/${PROGINSTANCENAME}/PIDvu 
 { sleep 1; rm -f /tmp/${PROGINSTANCENAME}/vu_tmp; } &
 if [ $(which retrovol 2>/dev/null) ];then
  killall retrovol 2>/dev/null
  retrovol &
 else
  $VTERMINAL -geometry "80x24-0-25" -e alsamixer -V all &
  echo $! > /tmp/${PROGINSTANCENAME}/PIDalsamixer
 fi
 if [ -e $PROGRAMHOME/vuslave ];then
  [ -e /tmp/${PROGINSTANCENAME}/PIDvuslave ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDvuslave 2>/dev/null) 2>/dev/null
  $PROGRAMHOME/vuslave &
  echo $! > /tmp/${PROGINSTANCENAME}/PIDvuslave
 fi
}

function term_TD (){
 localTD="$1"
 localTD=$(echo "${localTD}" | sed 's/^[ 	]*//;s/[ 	]*$//')
 if [ "${localTD}" != "" ];then
   cd "${localTD}"
 else
   localTD="/"
   cd "${localTD}"
 fi
 [ $(which rox) ] && rox -d "${localTD}"
}

function rec (){
 if [ $(cat /tmp/${PROGINSTANCENAME}/PIDpauserec 2>/dev/null) -eq 1 2>/dev/null ];then
    unpauserec
 elif [ ! -e /tmp/${PROGINSTANCENAME}/PIDrec ];then
    echo 0 > /tmp/${PROGINSTANCENAME}/PIDpauserec 
    if [ -e /tmp/${PROGINSTANCENAME}/PIDpreview ];then
		kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDpreview 2>/dev/null) 2>/dev/null
		rm -f /tmp/${PROGINSTANCENAME}/PIDpreview
		sleep 0.2
	fi 
    if [ -e /tmp/${PROGINSTANCENAME}/PIDvu ];then
		kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDvu 2>/dev/null) 2>/dev/null
		rm -f /tmp/${PROGINSTANCENAME}/PIDvu
	fi    
	if [ $(which retrovol 2>/dev/null) ];then
     killall retrovol 2>/dev/null
   	 retrovol -hide &
	else
	 [ -e /tmp/${PROGINSTANCENAME}/PIDalsamixer ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDalsamixer 2>/dev/null) 2>/dev/null	
	fi
	vbrquality="";acodec="";cdquality="";aacquality="";compresslevel=""
    if [ $HWCARD = "false" ];then
      if [ $HWDEV = "false" ];then plughw="plughw:0,0"; else plughw="plughw:0,1";fi
    else
      if [ $HWDEV = "false" ];then plughw="plughw:1,0"; else plughw="plughw:1,1";fi
    fi
	ufmg=1;if [ $(which oggenc 2>/dev/null) ];then ufmg=0;fi

	if [ $ENCODEOGGQ != "true" ] || [ $ENCODEAACQ != "true" ];then
	 # oggenc could not handle 45 kbps
	 if [ $BITRATE032 = "true" ];then bitrate=32;[ $ufmg -eq 0 ] && oggquality="- -M 64" || oggquality="-ab 45k"
	 elif [ $BITRATE008 = "true" ];then
		bitrate=8
		[ $ENCODEWAV = "true" ] && acodec="-acodec pcm_u8"
		[ $ufmg -eq 0 ] && oggquality="- -M 64" || oggquality="-ab 45k"
	 elif [ $BITRATE016 = "true" ];then bitrate=16;[ $ufmg -eq 0 ] && oggquality="- -M 64" || oggquality="-ab 45k"
	 elif [ $BITRATE0CD = "true" ];then bitrate=44;cdquality="-f cd";[ $ufmg -eq 0 ] && oggquality="- -M 64" || oggquality="-ab 45k"
	 elif [ $BITRATE048 = "true" ];then bitrate=48;[ $ufmg -eq 0 ] && oggquality="- -M 64" || oggquality="-ab 45k"
	 elif [ $BITRATE064 = "true" ];then bitrate=64;[ $ufmg -eq 0 ] && oggquality="- -M 64" || oggquality="-ab 64k"
	 elif [ $BITRATE128 = "true" ];then bitrate=128;[ $ufmg -eq 0 ] && oggquality="- -M 128" || oggquality="-ab 128k"
	 elif [ $BITRATE160 = "true" ];then bitrate=160;[ $ufmg -eq 0 ] && oggquality="- -M 160" || oggquality="-ab 160k"
	 elif [ $BITRATE192 = "true" ];then bitrate=192;[ $ufmg -eq 0 ] && oggquality="- -M 192" || oggquality="-ab 192k"
	 # Wary-5.3 could handle 256 or 320 but Slacko-5.3.3 ffmpeg could not
	 elif [ $BITRATE256 = "true" ];then bitrate=256;[ $ufmg -eq 0 ] && oggquality="- -M 256" || oggquality="-ab 256k"
	 elif [ $BITRATE320 = "true" ];then bitrate=320;[ $ufmg -eq 0 ] && oggquality="- -M 320" || oggquality="-ab 320k"
	 fi
	 [ $ENCODEAAC = "true" ] && aacquality="-ab ${bitrate}k"
	fi
	if [ $ENCODEOGGQ = "true" ] || [ $ENCODEAACQ = "true" ] || [ $ENCODEMP3 = "true" -a $VBRMODE = "true" ] || [ $ENCODEFLAC = "true" ];then
	 if [ $OGGQUALITYN1 = "true" ];then
		[ $ufmg -eq 0 ] && oggquality="- -q -1" || oggquality="-aq -1"
		vbrquality="-V 0"
		aacquality="-aq 10"
		compresslevel="-compression_level 12"
	 elif [ $OGGQUALITY00 = "true" ];then
		[ $ufmg -eq 0 ] && oggquality="- -q 0" || oggquality="-aq 0"
		vbrquality="-V 0"
		aacquality="-aq 50"
		compresslevel="-compression_level 0"
	 elif [ $OGGQUALITY01 = "true" ];then
		[ $ufmg -eq 0 ] && oggquality="- -q 1" || oggquality="-aq 1"
		vbrquality="-V 1"
		aacquality="-aq 70"
		compresslevel="-compression_level 1"
	 elif [ $OGGQUALITY02 = "true" ];then
		[ $ufmg -eq 0 ] && oggquality="- -q 2" || oggquality="-aq 2"
		vbrquality="-V 2"
		aacquality="-aq 100"
		compresslevel="-compression_level 2"
	 elif [ $OGGQUALITY03 = "true" ];then
		[ $ufmg -eq 0 ] && oggquality="- -q 3" || oggquality="-aq 3"
		vbrquality="-V 3"
		aacquality="-aq 150"
		compresslevel="-compression_level 3"
	 elif [ $OGGQUALITY04 = "true" ];then
		[ $ufmg -eq 0 ] && oggquality="- -q 4" || oggquality="-aq 4"
		vbrquality="-V 4"
		aacquality="-aq 200"
		compresslevel="-compression_level 4"
	 elif [ $OGGQUALITY05 = "true" ];then
		[ $ufmg -eq 0 ] && oggquality="- -q 5" || oggquality="-aq 5"
		vbrquality="-V 5"
		aacquality="-aq 250"
		compresslevel="-compression_level 5"
	 elif [ $OGGQUALITY06 = "true" ];then
		[ $ufmg -eq 0 ] && oggquality="- -q 6" || oggquality="-aq 6"
		vbrquality="-V 6"
		aacquality="-aq 300"
		compresslevel="-compression_level 6"
	 elif [ $OGGQUALITY07 = "true" ];then
		[ $ufmg -eq 0 ] && oggquality="- -q 7" || oggquality="-aq 7"
		vbrquality="-V 7"
		aacquality="-aq 350"
		compresslevel="-compression_level 7"
	 elif [ $OGGQUALITY08 = "true" ];then
		[ $ufmg -eq 0 ] && oggquality="- -q 8" || oggquality="-aq 8"
		vbrquality="-V 8"
		aacquality="-aq 400"
		compresslevel="-compression_level 8"
	 elif [ $OGGQUALITY09 = "true" ];then
		[ $ufmg -eq 0 ] && oggquality="- -q 9" || oggquality="-aq 9"
		vbrquality="-V 9"
		aacquality="-aq 450"
		compresslevel="-compression_level 9"
	 elif [ $OGGQUALITY10 = "true" ];then
		[ $ufmg -eq 0 ] && oggquality="- -q 10" || oggquality="-aq 10"
		vbrquality="-V 9"									
		aacquality="-aq 500"
		compresslevel="-compression_level 10"
	 fi
    fi
	if [ $CHANNELSTEREO = "true" ];then 
	  channels_arecord=2;channels_lame="j"
	else 
	  channels_arecord=1;channels_lame="m"
	fi
	if [ "$3" = "" -o "$3" = "0" ];then
	  duration1="0"
	  affmpegtime="";ffmpegtime=""
	else
	  duration1="$(echo $3+$AOFFSET+1|bc)"
	  affmpegtime="-t $duration1";ffmpegtime="-t $3"
	fi
	dirfilefix "$1" "$2"
	if [ $ENCODEMP3 = "true" ];then
	  aformat="mp3"
      if [ $CBRMODE = "true" ];then mp3mode="-b"
      elif [ $ABRMODE = "true" ];then mp3mode="--abr"
	  elif [ $VBRMODE = "true" ];then mp3mode="--vbr-new -b" 
      fi
      arecord -D $plughw -f cd -c $channels_arecord -d $duration1 | lame $mp3mode $bitrate $vbrquality -h -m $channels_lame - "${SDuse}/${SFuse%%.*}.${aformat}" &
      echo $! > /tmp/${PROGINSTANCENAME}/PIDrec
	  recVideoTrack
    elif [ $ENCODEWAV = "true" ];then
	  aformat="wav"
	  arecord -D $plughw -t wav -r $bitrate $cdquality -c $channels_arecord -d $duration1 | cat > "${SDuse}/${SFuse%%.*}.${aformat}" &
      echo $! > /tmp/${PROGINSTANCENAME}/PIDrec
	  recVideoTrack
    elif [ $ENCODEAAC = "true" -o $ENCODEAACQ = "true" ];then
	  aformat="aac"    
	  ffmpeg -y $affmpegtime -f alsa -ac $channels_arecord -ar 48000 -i $plughw -f adts -acodec libvo_aacenc $aacquality "${SDuse}/${SFuse%%.*}.${aformat}" &
      echo $! > /tmp/${PROGINSTANCENAME}/PIDrec
	  recVideoTrack
    elif [ $ENCODEFLAC = "true" ];then
	  aformat="flac"    
	  ffmpeg -y $affmpegtime -f alsa -ac $channels_arecord -ar 48000 -i $plughw -f flac -acodec flac $compresslevel "${SDuse}/${SFuse%%.*}.${aformat}" &
      echo $! > /tmp/${PROGINSTANCENAME}/PIDrec
	  recVideoTrack
    else
	 aformat="ogg"
	 if [ $(which oggenc 2>/dev/null) ];then
      arecord -D $plughw -f cd -c $channels_arecord -d $duration1 | oggenc $oggquality -o "${SDuse}/${SFuse%%.*}.${aformat}" &
      echo $! > /tmp/${PROGINSTANCENAME}/PIDrec
	  recVideoTrack
	 else
	  ffmpeg -y $affmpegtime -f alsa -ac $channels_arecord -ar 44100 -i $plughw -f ogg $oggquality -acodec libvorbis "${SDuse}/${SFuse%%.*}.${aformat}" &
      echo $! > /tmp/${PROGINSTANCENAME}/PIDrec
	  recVideoTrack
	 fi
    fi
	if [ -e /tmp/${PROGINSTANCENAME}/PIDalsamixer ];then
		kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDalsamixer 2>/dev/null) 2>/dev/null
		rm -f /tmp/${PROGINSTANCENAME}/PIDalsamixer
	fi
	if [ -e $PROGRAMHOME/recslave ];then
	  [ -e /tmp/${PROGINSTANCENAME}/PIDrecslave ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDrecslave 2>/dev/null) 2>/dev/null
	  $PROGRAMHOME/recslave &
	  echo $! > /tmp/${PROGINSTANCENAME}/PIDrecslave
	fi
 fi
}

function play (){
 if [ $(cat /tmp/${PROGINSTANCENAME}/PIDpauseplay 2>/dev/null) -eq 1 2>/dev/null ];then
    unpauseplay
 elif [ ! -e /tmp/${PROGINSTANCENAME}/PIDplay ];then
      echo 0 > /tmp/${PROGINSTANCENAME}/PIDpauseplay
      if [ $HWCARD = "false" ];then
        if [ $HWDEV = "false" ];then plughw="plughw:0,0"; else plughw="plughw:0,1";fi
      else
        if [ $HWDEV = "false" ];then plughw="plughw:1,0"; else plughw="plughw:1,1";fi
      fi
      if [ "$3" != "" ];then
        duration2="$3"
      else
        duration2="0"
      fi
	  dirfilefix "$1" "$2"
	  AddExtensions
	  ffmpeg -ss $AOFFSET -i "${SDuse}/${SFuse%%.*}.${aformat}" -f au - | aplay -D $plughw -d $duration2 &
      echo $! > /tmp/${PROGINSTANCENAME}/PIDplay
      if [ $(which mplayer 2>/dev/null) ];then
		$VTERMINAL -geometry "1x1-0-0" -e mplayer -geometry "+0+64" -title "pAVrecord mplayer" "${SDuse}/${SFuse}" &
		echo $! > /tmp/${PROGINSTANCENAME}/PIDvidplay2
		sleep 0.2
		PIDvidplay=$(ps u | grep 'mplayer -geometry +0+0' | awk '$11 == "mplayer" {print $2}') 
		echo $PIDvidplay > /tmp/${PROGINSTANCENAME}/PIDvidplay
      else
       ffplay -window_title "pAVrecord ffplay" -i  "${SDuse}/${SFuse}" >/dev/null &
       echo $! > /tmp/${PROGINSTANCENAME}/PIDvidplay
      fi

	  if [ -e $PROGRAMHOME/playslave ];then
		[ -e /tmp/${PROGINSTANCENAME}/PIDplayslave ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDplayslave 2>/dev/null) 2>/dev/null
	    $PROGRAMHOME/playslave &
		echo $! > /tmp/${PROGINSTANCENAME}/PIDplayslave
	  fi	    
 fi
}

function stoprec (){
   echo 0 > /tmp/${PROGINSTANCENAME}/PIDpauserec
   if [ -e /tmp/${PROGINSTANCENAME}/PIDrec ];then
	kill -CONT $(cat /tmp/${PROGINSTANCENAME}/PIDvidrec 2>/dev/null) 2>/dev/null
	kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDvidrec 2>/dev/null) 2>/dev/null
	kill -CONT $(cat /tmp/${PROGINSTANCENAME}/PIDvidrec2 2>/dev/null) 2>/dev/null
	kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDvidrec2 2>/dev/null) 2>/dev/null
	sleep $AOFFSET
	kill -CONT $(cat /tmp/${PROGINSTANCENAME}/PIDrec 2>/dev/null) 2>/dev/null
	kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDrec 2>/dev/null) 2>/dev/null
#	killall arecord 2>/dev/null
	rm -f /tmp/${PROGINSTANCENAME}/PIDrec 2>/dev/null
	rm -f /tmp/${PROGINSTANCENAME}/PIDvidrec 2>/dev/null
	if [ $AUTOMAKE = "true" ];then make_avideo "$1" "$2";fi
	if [ -e $PROGRAMHOME/stoprecslave ];then
	  [ -e /tmp/${PROGINSTANCENAME}/PIDstoprecslave ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDstoprecslave 2>/dev/null) 2>/dev/null
	  $PROGRAMHOME/stoprecslave &
	  echo $! > /tmp/${PROGINSTANCENAME}/PIDstoprecslave
	fi
	preview
   fi
}

function stopplay (){
	echo 0 > /tmp/${PROGINSTANCENAME}/PIDpauseplay
	if [ -e /tmp/${PROGINSTANCENAME}/PIDplay ];then
		kill -CONT $(cat /tmp/${PROGINSTANCENAME}/PIDplay 2>/dev/null) 2>/dev/null
		kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDplay 2>/dev/null) 2>/dev/null
		kill -CONT $(cat /tmp/${PROGINSTANCENAME}/PIDvidplay 2>/dev/null) 2>/dev/null
		kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDvidplay 2>/dev/null) 2>/dev/null
		kill -CONT $(cat /tmp/${PROGINSTANCENAME}/PIDvidplay2 2>/dev/null) 2>/dev/null
		kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDvidplay2 2>/dev/null) 2>/dev/null
		rm -f /tmp/${PROGINSTANCENAME}/PIDplay 2>/dev/null
		rm -f /tmp/${PROGINSTANCENAME}/PIDvidplay 2>/dev/null
		if [ -e $PROGRAMHOME/stopplayslave ];then
			[ -e /tmp/${PROGINSTANCENAME}/PIDstopplayslave ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDstopplayslave 2>/dev/null) 2>/dev/null
			$PROGRAMHOME/stopplayslave &
			echo $! > /tmp/${PROGINSTANCENAME}/PIDstopplayslave
		fi
	fi
}

function pauserec (){
	if [ -e /tmp/${PROGINSTANCENAME}/PIDrec ];then
		pavrecordPIDrec=$(cat /tmp/${PROGINSTANCENAME}/PIDrec 2>/dev/null)
		pavrecordPIDvidrec=$(cat /tmp/${PROGINSTANCENAME}/PIDvidrec 2>/dev/null)
			kill -STOP $pavrecordPIDrec 2>/dev/null
			kill -STOP $pavrecordPIDvidrec 2>/dev/null
			kill -STOP $pavrecordPIDvidrec2 2>/dev/null
			echo 1 > /tmp/${PROGINSTANCENAME}/PIDpauserec
			if [ -e $PROGRAMHOME/pauserecslave0 ];then
				[ -e /tmp/${PROGINSTANCENAME}/PIDpauserecslave0 ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDpauserecslave0 2>/dev/null) 2>/dev/null
				$PROGRAMHOME/pauserecslave0 &
				echo $! > /tmp/${PROGINSTANCENAME}/PIDpauserecslave0
			fi
	fi
}

function unpauserec (){
	if [ -e /tmp/${PROGINSTANCENAME}/PIDrec ];then
		pavrecordPIDrec=$(cat /tmp/${PROGINSTANCENAME}/PIDrec 2>/dev/null)
		pavrecordPIDvidrec=$(cat /tmp/${PROGINSTANCENAME}/PIDvidrec 2>/dev/null)
			kill -CONT $pavrecordPIDrec 2>/dev/null
			kill -CONT $pavrecordPIDvidrec2 2>/dev/null
			kill -CONT $pavrecordPIDvidrec 2>/dev/null
			echo 0 > /tmp/${PROGINSTANCENAME}/PIDpauserec
			if [ -e $PROGRAMHOME/pauserecslave1 ];then
				[ -e /tmp/${PROGINSTANCENAME}/PIDpauserecslave1 ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDpauserecslave1 2>/dev/null) 2>/dev/null
				$PROGRAMHOME/pauserecslave1 &
				echo $! > /tmp/${PROGINSTANCENAME}/PIDpauserecslave1
			fi
	fi
}

function pauseplay (){
	if [ -e /tmp/${PROGINSTANCENAME}/PIDplay ];then
		pavrecordPIDplay=$(cat /tmp/${PROGINSTANCENAME}/PIDplay 2>/dev/null)
		pavrecordPIDvidplay=$(cat /tmp/${PROGINSTANCENAME}/PIDvidplay 2>/dev/null)
			kill -STOP $pavrecordPIDplay 2>/dev/null
			echo 1 > /tmp/${PROGINSTANCENAME}/PIDpauseplay
			kill -STOP $pavrecordPIDvidplay 2>/dev/null
			if [ -e $PROGRAMHOME/pauseplayslave0 ];then
				[ -e /tmp/${PROGINSTANCENAME}/PIDpauseplayslave0 ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDpauseplayslave0 2>/dev/null) 2>/dev/null
				$PROGRAMHOME/pauseplayslave0 &
				echo $! > /tmp/${PROGINSTANCENAME}/PIDpauseplayslave0
			fi
	fi
}

function unpauseplay (){
	if [ -e /tmp/${PROGINSTANCENAME}/PIDplay ];then
		pavrecordPIDplay=$(cat /tmp/${PROGINSTANCENAME}/PIDplay 2>/dev/null)
		pavrecordPIDvidplay=$(cat /tmp/${PROGINSTANCENAME}/PIDvidplay 2>/dev/null)
			kill -CONT $pavrecordPIDplay 2>/dev/null
			echo 0 > /tmp/${PROGINSTANCENAME}/PIDpauseplay
			kill -CONT $pavrecordPIDvidplay 2>/dev/null
			if [ -e $PROGRAMHOME/pauseplayslave1 ];then
				[ -e /tmp/${PROGINSTANCENAME}/PIDpauseplayslave1 ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDpauseplayslave1 2>/dev/null) 2>/dev/null
				$PROGRAMHOME/pauseplayslave1 &
				echo $! > /tmp/${PROGINSTANCENAME}/PIDpauseplayslave1
			fi
	fi
}

function quit (){
	stoprec "$TDIRFILE_DS1" "$DIRFILE1"
	stopplay
	if [ -e $PROGRAMHOME/quitslave ];then
		[ -e /tmp/${PROGINSTANCENAME}/PIDquitslave ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDquitslave 2>/dev/null) 2>/dev/null
		$PROGRAMHOME/quitslave &
		echo $! > /tmp/${PROGINSTANCENAME}/PIDquitslave
	fi
	if [ $(which retrovol 2>/dev/null) ];then
     killall retrovol 2>/dev/null
   	 retrovol -hide &
	fi
	[ -e /tmp/${PROGINSTANCENAME}/PIDpreview ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDpreview 2>/dev/null) 2>/dev/null
	[ -e /tmp/${PROGINSTANCENAME}/PIDvu ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDvu 2>/dev/null) 2>/dev/null
	[ -e /tmp/${PROGINSTANCENAME}/PIDalsamixer ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDalsamixer 2>/dev/null) 2>/dev/null
	[ -e /tmp/${PROGINSTANCENAME}/PIDvuslave ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDvuslave 2>/dev/null) 2>/dev/null
	[ -e /tmp/${PROGINSTANCENAME}/PIDrecslave ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDrecslave 2>/dev/null) 2>/dev/null
	[ -e /tmp/${PROGINSTANCENAME}/PIDplayslave ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDplayslave 2>/dev/null) 2>/dev/null
	[ -e /tmp/${PROGINSTANCENAME}/PIDstoprecslave ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDstoprecslave 2>/dev/null) 2>/dev/null
	[ -e /tmp/${PROGINSTANCENAME}/PIDstopplayslave ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDstopplayslave 2>/dev/null) 2>/dev/null
	[ -e /tmp/${PROGINSTANCENAME}/PIDpauserecslave0 ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDpauserecslave0 2>/dev/null) 2>/dev/null
	[ -e /tmp/${PROGINSTANCENAME}/PIDpauserecslave1 ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDpauserecslave1 2>/dev/null) 2>/dev/null
	[ -e /tmp/${PROGINSTANCENAME}/PIDpauseplayslave0 ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDpauseplayslave0 2>/dev/null) 2>/dev/null
	[ -e /tmp/${PROGINSTANCENAME}/PIDpauseplayslave1 ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDpauseplayslave1 2>/dev/null) 2>/dev/null
	[ -e /tmp/${PROGINSTANCENAME}/PIDquitslave ] && kill -TERM $(cat /tmp/${PROGINSTANCENAME}/PIDquitslave 2>/dev/null) 2>/dev/null
    rm -rf /tmp/${PROGINSTANCENAME}
}
export -f rec
export -f play
export -f stoprec
export -f stopplay
export -f pauserec unpauserec
export -f pauseplay unpauseplay
export -f vu term_TD DateStamp1 dirfilefix AddExtensions recVideoTrack make_avideo play_combined preview
export -f helpsetup
export DURATION1 DURATION2 HWCARD HWDEV ENCODEMP3 ENCODEOGGQ ENCODEOGGMM ENCODEWAV ENCODEAAC ENCODEAACQ ENCODEFLAC
export BITRATE006 BITRATE008 BITRATE016 BITRATE032 BITRATE0CD BITRATE048 BITRATE064 BITRATE128 BITRATE160 BITRATE192 BITRATE256 BITRATE320
export OGGQUALITYN1 OGGQUALITY01 OGGQUALITY02 OGGQUALITY03 OGGQUALITY04 OGGQUALITY05
export OGGQUALITY06 OGGQUALITY07 OGGQUALITY08 OGGQUALITY09 OGGQUALITY10
export CHANNELSTEREO CBRMODE ABRMODE VBRMODE DIRFILE1 DIRFILE2 TDIRFILE_DS1
export aformat vformat avformat
export vHWCARD vHWDEV 
export vENCODE00 vENCODE01 vENCODE02 vENCODE03 vENCODE04 vENCODE05 X11GRAB
export vBITRATE006 vBITRATE008 vBITRATE016 vBITRATE032 vBITRATE0CD vBITRATE048 vBITRATE064 vBITRATE128 vBITRATE160 vBITRATE192 vBITRATE256 vBITRATE320
export vCONTAINER00 vCONTAINER01 vCONTAINER02 vCONTAINER03 vCONTAINER04 vCONTAINER05
export vINPUTFORMAT vDEVICE vWIDTH vHEIGHT X11WIDTH X11HEIGHT AOFFSET OUTFRATE jOUTFRATE xOUTFRATE OUTvENC_OPTS INvENC_OPTS AUTOMAKE AUTODEL XOFS YOFS XDSPLY

small_dialog_ (){

export MAIN_DIALOG="
 <window title=\"$programtitle $(gettext 'Audio/Video recorder and player')\" icon-name=\"gtk-media-record\" width-request=\"740\">
  <hbox>
   <button tooltip-text=\"$(gettext 'RECORD A and V tracks')\" width-request=\"24\">
    <input file stock=\"gtk-media-record\"></input> 
    <variable>_REC</variable>
    <action>refresh:DIRFILE1</action>
    <action>disable:_PLAY</action>
"'  <action>rec "$TDIRFILE_DS1" "$DIRFILE1" "$DURATION1"</action>'"
    <action>enable:_STOPREC</action>
    <action>enable:_PAUSEREC</action>
    <action>disable:_REC</action>
   </button>
   <button tooltip-text=\"$(gettext 'PAUSE record')\" width-request=\"24\">
    <input file stock=\"gtk-media-pause\"></input>
    <variable>_PAUSEREC</variable>
    <action>pauserec</action>
    <action>enable:_REC</action>
   </button>
   <button tooltip-text=\"$(gettext 'STOP record. If no record in process, pop up MIXER, VU display and video preview')\" width-request=\"24\">
    <input file stock=\"gtk-media-stop\"></input>
    <variable>_STOPREC</variable>
"'  <action>if [ ! -e /tmp/${PROGINSTANCENAME}/PIDrec ];then vu; else sleep 1; stoprec "$TDIRFILE_DS1" "$DIRFILE1"; fi</action>'"
    <action>enable:_REC</action>
    <action>enable:_PLAY</action>
   </button>
   <button relief=\"2\" tooltip-text=\"$(gettext 'PLAY A and V tracks')\" width-request=\"24\">
    <input file stock=\"gtk-media-play\"></input>
    <variable>_PLAY</variable>
"'  <action>play "$TDIRFILE_DS1" "$DIRFILE1"</action>'"
    <action>enable:_STOPPLAY</action>
    <action>enable:_PAUSEPLAY</action>
    <action>disable:_PLAY</action>
   </button>
   <button relief=\"2\" tooltip-text=\"$(gettext 'PAUSE A and V track play')\" width-request=\"24\">
    <input file stock=\"gtk-media-pause\"></input>
    <variable>_PAUSEPLAY</variable>
    <action>pauseplay</action>
    <action>enable:_PLAY</action>
   </button>
   <button relief=\"2\" tooltip-text=\"$(gettext 'STOP A and V track play')\" width-request=\"24\">
    <input file stock=\"gtk-media-stop\"></input>
    <variable>_STOPPLAY</variable>
    <action>stopplay</action>
    <action>enable:_PLAY</action>
   </button>
     <button tooltip-text=\"$(gettext 'PLAY combined AV file')\" width-request=\"24\">
      <input file stock=\"gtk-media-play\"></input>
      <variable>_PLAYAV</variable>
"'    <action>play_combined "$TDIRFILE_DS1" "$DIRFILE1"</action>'"
     </button>   
   <button tooltip-text=\"$(gettext 'Opens Rox filemanager at target directory of recording')\" width-request=\"24\">
    <input file stock=\"gtk-home\"></input>
"'	<action>term_TD "$TDIRFILE_DS1"</action>'"
   </button>
   <button tooltip-text=\"$(gettext 'Advanced User Interface and pAVrecord configuration')\">
    <input file stock=\"gtk-preferences\"></input>
"'  <action>echo "$TDIRFILE_DS1" >/tmp/${PROGINSTANCENAME}/TDIRFILE_DS1</action>
	<action>echo "$DIRFILE1" >/tmp/${PROGINSTANCENAME}/DIRFILE1</action>'"
    <action>stopplay</action>
"'    <action>stoprec "$TDIRFILE_DS1" "$DIRFILE1"</action>'"
    <action type=\"exit\">REALLY-CONFIG</action>
   </button>
   <button tooltip-text=\"$(gettext 'Browse and select the target directory for the file to record or play')\">
    <input file stock=\"gtk-open\"></input>
    <variable>PATHTOFILE</variable>
    <action type=\"fileselect\">TDIRFILE_DS1</action>
   </button>
   <entry  accept=\"directory\" tooltip-text=\"$(gettext 'Browse, drag and drop, copy and paste, or type the target directory for the file to record or play')\">
    <variable>TDIRFILE_DS1</variable>
"'	<input>echo "${TDIRFILE_DS1}"</input>'"
   </entry>
   <hbox width-request=\"164\">
   <entry accept=\"directory\" tooltip-text=\"$(gettext 'File name is an auto datestamp. Note that a separate sound track will also be recorded in the background. For playback, you can also enter a filename, or drag and drop one from filemanager')\">
    <variable>DIRFILE1</variable>
	<input>DateStamp1</input>
   </entry>
   </hbox>
       <checkbox tooltip-text=\"$(gettext 'select for automatically encapsulate audio and video tracks in single AV container when record stop is pressed')\">
        <variable>AUTOMAKE</variable>
        <label>$(gettext 'auto')</label>
        <default>$AUTOMAKE</default>
		<action>if true disable:COMBINE</action>
		<action>if false enable:COMBINE</action>
       </checkbox>
	<button tooltip-text=\"$(gettext 'Encapsulate audio and video tracks in single AV container')\">
	 <input file stock=\"gtk-convert\"></input>
	 <variable>COMBINE</variable>
"'   <action>[ $AUTOMAKE = "false" ] && make_avideo "$TDIRFILE_DS1" "$DIRFILE1"</action>'"
	</button>
       <checkbox tooltip-text=\"$(gettext 'select for automatically delete the separate audio and video tracks once the combined AV file is created')\">
        <variable>AUTODEL</variable>
        <label>$(gettext 'del')</label>
        <default>$AUTODEL</default>
       </checkbox>
   <button tooltip-text=\"$(gettext 'QUIT')\" width-request=\"24\">
    <input file stock=\"gtk-quit\"></input>
"'  <action>echo "$TDIRFILE_DS1" >/tmp/${PROGINSTANCENAME}/TDIRFILE_DS1</action>
	<action>echo "$DIRFILE1" >/tmp/${PROGINSTANCENAME}/DIRFILE1</action>'"
    <action type=\"exit\">EXIT-QUIT</action>
   </button>
  </hbox>
</window>"
preview
gtkdialog --program=MAIN_DIALOG -G +276+0 > /tmp/${PROGINSTANCENAME}/maindialog
MAIN_DIALOG=$(</tmp/${PROGINSTANCENAME}/maindialog)
TDIRFILE_DS1="$(cat /tmp/${PROGINSTANCENAME}/TDIRFILE_DS1 2>/dev/null)"
DIRFILE1="$(cat /tmp/${PROGINSTANCENAME}/DIRFILE1 2>/dev/null)"
sed -i -e '/^TDIRFILE_DS1=[^ ]*/ d' "$CONFIGFILE"
echo "TDIRFILE_DS1=""\"$TDIRFILE_DS1\"" >> "$CONFIGFILE"
sed -i -e '/^DIRFILE1=[^ ]*/ d' "$CONFIGFILE"
echo "DIRFILE1=""\"$DIRFILE1\"" >> "$CONFIGFILE"
}

config_dialog_ (){

  export HELP_SETUP="
  <window title=\"$programtitle $(gettext 'Setup Help')\" icon-name=\"gtk-media-record\">
   <vbox>
    <edit wrap-mode=\"2\"><width>312</width><height>500</height>
     <variable>HELPTEXTSETUP</variable>
     <input file>/tmp/${PROGINSTANCENAME}/helpsetup.txt</input>
    </edit>
    <hbox>
     <button ok>
      <variable>HELP_SETUP</variable> 
      <action type=\"closewindow\">HELP_SETUP</action>
     </button>
    </hbox>
   </vbox>
  </window>"
  
export MAIN_DIALOG="
<window width-request=\"642\" title=\"$programtitle $(gettext 'Audio/Video recorder and player')\" icon-name=\"gtk-media-record\">
 <vbox>
  <hbox width-request=\"642\">
  <hbox width-request=\"336\">
   <frame $(gettext 'Record A and V tracks')>
	<frame $(gettext 'Target Directory')           $(gettext 'Duration(seconds)')>
    <hbox>
     <button tooltip-text=\"$(gettext 'Browse and select the target directory for the file to record')\">
      <input file stock=\"gtk-directory\"></input>
      <variable>TARGET_DIRECTORY1</variable>
      <action type=\"fileselect\">TDIRFILE_DS1</action>
     </button>
     <entry editable=\"true\" accept=\"directory\" tooltip-text=\"$(gettext 'Browse, drag and drop, copy and paste, or type the target directory for the file to record')\">
      <variable>TDIRFILE_DS1</variable>
"'	  <input>echo "$TDIRFILE_DS1"</input>'"
     </entry>
     <hbox width-request=\"50\">
      <entry tooltip-text=\"$(gettext 'Recording length. 0 = no time limit')\" xalign=\"1\">
       <variable>DURATION1</variable>
"'     <input>echo "$DURATION1"</input>'"
      </entry>
     </hbox>
     <button tooltip-text=\"$(gettext 'PAUSE record')\">
      <input file stock=\"gtk-media-pause\"></input>
      <variable>_PAUSEREC</variable>
      <action>pauserec</action>
      <action>enable:_REC</action>
      <action>enable:_DS1</action>
     </button>
     <button tooltip-text=\"$(gettext 'STOP record. If no record in process, pop up MIXER, VU display and video preview')\">
      <input file stock=\"gtk-media-stop\"></input>
      <variable>_STOPREC</variable>
"'    <action>if [ ! -e /tmp/${PROGINSTANCENAME}/PIDrec ];then vu; else sleep 1; stoprec "$TDIRFILE_DS1" "$DIRFILE1"; fi</action>'"
      <action>enable:_REC</action>
      <action>enable:_DS1</action>  
      <action>enable:_PLAY</action>
      <action>enable:_DS2</action> 
     </button>
    </hbox>
	</frame>
	
    <hbox>
	  <text><label>$(gettext 'File:')</label></text>
      <entry tooltip-text=\"$(gettext 'File name is an auto datestamp. Note that a separate sound track will also be recorded in the background')\">
       <variable>DIRFILE1</variable>
      <input>DateStamp1</input>
      </entry>
      <button tooltip-text=\"$(gettext 'Opens Rox filemanager at target directory of recording')\">
		<input file stock=\"gtk-home\"></input>
"'		<action>term_TD "$TDIRFILE_DS1"</action>'"
      </button>
     <button tooltip-text=\"$(gettext 'RECORD A and V tracks')\">
      <input file stock=\"gtk-media-record\"></input> 
      <variable>_REC</variable>
      <action>refresh:DIRFILE1</action>
      <action>disable:_PLAY</action>
      <action>disable:_DS2</action>
"'    <action>rec "$TDIRFILE_DS1" "$DIRFILE1" "$DURATION1"</action>'"
      <action>enable:_STOPREC</action>
      <action>enable:_PAUSEREC</action>
      <action>disable:_DS1</action>
      <action>disable:_REC</action>
     </button>
     <button tooltip-text=\"$(gettext 'Copy Record file name entry to Play filename entry')\" width-request=\"28\">
      <label>\"->\"</label>
      <action type=\"refresh\">DIRFILE2</action>
     </button>
    </hbox>


   </frame>
  </hbox>
  <hbox width-request=\"290\">
   <frame $(gettext 'Play A and V tracks')>
	<frame $(gettext 'Audio sync offset and play duration')>
    <hbox>
	  <text><label>$(gettext 'offset:')</label></text>
      <hbox width-request=\"30\">
      <entry tooltip-text=\"$(gettext 'Offset audio track by this amount. For example: 1.2 (in seconds)')\">
       <variable>AOFFSET</variable>
"'     <input>echo "$AOFFSET"</input>'"
      </entry>
     </hbox>
	  <text><label>$(gettext 'Duration:')</label></text>
     <hbox width-request=\"50\">
      <entry tooltip-text=\"$(gettext 'Playback length. 0 = no time limit')\" xalign=\"1\">
       <variable>DURATION2</variable>
"'     <input>echo "$DURATION2"</input>'"
      </entry>
     </hbox>
     <button tooltip-text=\"$(gettext 'PAUSE play')\">
      <input file stock=\"gtk-media-pause\"></input>
      <variable>_PAUSEPLAY</variable>
      <action>pauseplay</action>
      <action>enable:_PLAY</action>
      <action>enable:_DS2</action>
     </button>
     <button tooltip-text=\"$(gettext 'STOP play')\">
      <input file stock=\"gtk-media-stop\"></input>
      <variable>_STOPPLAY</variable>
      <action>stopplay</action>
      <action>enable:_PLAY</action>
      <action>enable:_DS2</action>
     </button>
    </hbox>
	</frame>

    <hbox>
     <button tooltip-text=\"$(gettext 'Browse for SOURCE File to play')\">
      <input file stock=\"gtk-open\"></input>
      <variable>PATHTOFILE</variable>
      <action type=\"fileselect\">DIRFILE2</action>
     </button>
     <entry tooltip-text=\"$(gettext 'Bring filename over from Record filename entry or: Browse, drag and drop, copy and paste, or type name (including path) of the video file to play. Note that, if available, the associated sound track will also be played')\">
      <variable>DIRFILE2</variable>
"'	  <input>echo "${DIRFILE1}"</input>'"
     </entry>
     <button tooltip-text=\"$(gettext 'PLAY the separate A and V tracks at same time')\">
      <input file stock=\"gtk-media-play\"></input>
      <variable>_PLAY</variable>
"'    <action>play "$TDIRFILE_DS1" "$DIRFILE2" "$DURATION2"</action>'"
      <action>enable:_STOPPLAY</action>
      <action>enable:_PAUSEPLAY</action>
      <action>disable:_DS2</action>
      <action>disable:_PLAY</action>
     </button>
    </hbox>
   </frame>
  </hbox>
</hbox>
<hbox>
  <hbox width-request=\"632\" homogeneous=\"true\">  
   <frame $(gettext 'AUDIO RECORD CONFIGURATION:')>
    <hbox width-request=\"632\">

     <hbox width-request=\"476\">    
      <frame $(gettext 'select audio format')>
       <hbox homogeneous=\"true\">    
        <radiobutton tooltip-text=\"$(gettext 'use mp3 encoder type')\">
         <variable>ENCODEMP3</variable>
         <label>mp3</label>
         <default>$ENCODEMP3</default>
        </radiobutton>
        <radiobutton tooltip-text=\"$(gettext 'use wav encoder type')\">
         <variable>ENCODEWAV</variable>
         <label>wav</label>
         <default>$ENCODEWAV</default>
        </radiobutton>
        <radiobutton tooltip-text=\"$(gettext 'use ogg managed maximum encoder type')\">
         <variable>ENCODEOGGMM</variable>
         <label>ogg_mm</label>
         <default>$ENCODEOGGMM</default>
        </radiobutton>
        <radiobutton tooltip-text=\"$(gettext 'use ogg unmanaged quality encoder type')\">
         <variable>ENCODEOGGQ</variable>
         <label>ogg_q</label>
         <default>$ENCODEOGGQ</default>
        </radiobutton>
        <radiobutton tooltip-text=\"$(gettext 'use aac bitrate mode encoder type')\">
         <variable>ENCODEAAC</variable>
         <label>aac</label>
         <default>$ENCODEAAC</default>
        </radiobutton>
        <radiobutton tooltip-text=\"$(gettext 'use aac quality mode encoder type')\">
         <variable>ENCODEAACQ</variable>
         <label>aac_q</label>
         <default>$ENCODEAACQ</default>
        </radiobutton>
        <radiobutton tooltip-text=\"$(gettext 'use flac lossless encoder type')\">
         <variable>ENCODEFLAC</variable>
         <label>flac</label>
         <default>$ENCODEFLAC</default>
        </radiobutton>
       </hbox> 
      </frame>
     </hbox>
    <hbox width-request=\"136\">
     <frame $(gettext 'select mp3 mode')>
      <hbox homogeneous=\"true\">    
       <radiobutton tooltip-text=\"$(gettext 'use Constant Bit Rate mode when mp3 encoding')\">
        <variable>CBRMODE</variable>
        <label>cbr</label>
        <default>$CBRMODE</default>
       </radiobutton>
       <radiobutton tooltip-text=\"$(gettext 'use Average Bit Rate mode when mp3 encoding')\">
        <variable>ABRMODE</variable>
        <label>abr</label>
        <default>$ABRMODE</default>
       </radiobutton>
       <radiobutton tooltip-text=\"$(gettext 'use Variable Bit Rate mode when mp3 encoding')\">
        <variable>VBRMODE</variable>
        <label>vbr</label>
        <default>$VBRMODE</default>
       </radiobutton>
      </hbox> 
     </frame>
    </hbox>
   </hbox>
    <hbox width-request=\"640\"> 
     <frame $(gettext 'sample rate for wav; bitrate for mp3, aac or ogg_mm')>
      <hbox homogeneous=\"true\" width-request=\"601\">
       <radiobutton>
        <variable>BITRATE320</variable>
        <label>320k</label>
        <default>$BITRATE320</default>
       </radiobutton>
       <radiobutton>
        <variable>BITRATE256</variable>
        <label>256k</label>
        <default>$BITRATE256</default>
       </radiobutton>
       <radiobutton>
        <variable>BITRATE192</variable>
        <label>192k</label>
        <default>$BITRATE192</default>
       </radiobutton>
       <radiobutton>
        <variable>BITRATE160</variable>
        <label>160k</label>
        <default>$BITRATE160</default>
       </radiobutton>
       <radiobutton>
        <variable>BITRATE128</variable>
        <label>128k</label>
        <default>$BITRATE128</default>
       </radiobutton>
       <radiobutton>
        <variable>BITRATE064</variable>
        <label>64k</label>
        <default>$BITRATE064</default>
       </radiobutton>
       <radiobutton>
        <variable>BITRATE048</variable>
        <label>48k</label>
        <default>$BITRATE048</default>
       </radiobutton>
       <radiobutton>
        <variable>BITRATE0CD</variable>
        <label>44.1k</label>
        <default>$BITRATE0CD</default>
       </radiobutton>
       <radiobutton>
        <variable>BITRATE032</variable>
        <label>32k</label>
        <default>$BITRATE032</default>
       </radiobutton>
       <radiobutton>
        <variable>BITRATE016</variable>
        <label>16k</label>
        <default>$BITRATE016</default>
       </radiobutton>
       <radiobutton>
        <variable>BITRATE008</variable>
        <label>8k</label>
        <default>$BITRATE008</default>
       </radiobutton>
      </hbox>
     </frame>
    </hbox>
    <hbox width-request=\"640\">       
     <frame $(gettext 'audio quality for: mp3 vbr, aac_q or ogg_q; compression_level for flac (N1=12)')>
      <hbox homogeneous=\"true\" width-request=\"601\">
       <radiobutton>
        <variable>OGGQUALITY10</variable>
        <label>10</label>
        <default>$OGGQUALITY10</default>
       </radiobutton>
       <radiobutton>
        <variable>OGGQUALITY09</variable>
        <label>09</label>
        <default>$OGGQUALITY09</default>
       </radiobutton>
       <radiobutton>
        <variable>OGGQUALITY08</variable>
        <label>08</label>
        <default>$OGGQUALITY08</default>
       </radiobutton>
       <radiobutton>
        <variable>OGGQUALITY07</variable>
        <label>07</label>
        <default>$OGGQUALITY07</default>
       </radiobutton>
       <radiobutton>
        <variable>OGGQUALITY06</variable>
        <label>06</label>
        <default>$OGGQUALITY06</default>
       </radiobutton>
       <radiobutton>
        <variable>OGGQUALITY05</variable>
        <label>05</label>
        <default>$OGGQUALITY05</default>
       </radiobutton>
       <radiobutton>
        <variable>OGGQUALITY04</variable>
        <label>04</label>
        <default>$OGGQUALITY04</default>
       </radiobutton>
       <radiobutton>
        <variable>OGGQUALITY03</variable>
        <label>03</label>
        <default>$OGGQUALITY03</default>
       </radiobutton>
       <radiobutton>
        <variable>OGGQUALITY02</variable>
        <label>02</label>
        <default>$OGGQUALITY02</default>
       </radiobutton>
       <radiobutton>
        <variable>OGGQUALITY01</variable>
        <label>01</label>
        <default>$OGGQUALITY01</default>
       </radiobutton>
       <radiobutton>
        <variable>OGGQUALITY00</variable>
        <label>00</label>
        <default>$OGGQUALITY00</default>
       </radiobutton>
       <radiobutton>
        <variable>OGGQUALITYN1</variable>
        <label>N1</label>
        <default>$OGGQUALITYN1</default>
       </radiobutton>       
      </hbox>
     </frame>
    </hbox>
   </frame>
  </hbox>
</hbox>
<hbox>
  <hbox width-request=\"632\" homogeneous=\"true\">  
   <frame $(gettext 'VIDEO RECORD CONFIGURATION:')>

    <hbox width-request=\"640\">
      <frame $(gettext 'select video encoder. (Note: video track is first recorded alone into an avi container)')>
       <hbox homogeneous=\"true\" width-request=\"601\">    
        <radiobutton>
         <variable>vENCODE00</variable>
         <label>MPEG-1</label>
         <default>$vENCODE00</default>
        </radiobutton>
        <radiobutton>
         <variable>vENCODE01</variable>
         <label>MPEG-2</label>
         <default>$vENCODE01</default>
        </radiobutton>
        <radiobutton>
         <variable>vENCODE02</variable>
         <label>MPEG-4/10</label>
         <default>$vENCODE02</default>
        </radiobutton>
        <radiobutton>
         <variable>vENCODE03</variable>
         <label>Theora</label>
         <default>$vENCODE03</default>
        </radiobutton>
        <radiobutton>
         <variable>vENCODE04</variable>
         <label>x264</label>
         <default>$vENCODE04</default>
        </radiobutton>
        <radiobutton tooltip-text=\"$(gettext 'jpeg sequence for output')\">
         <variable>vENCODE05</variable>
         <label>JPEGS</label>
         <default>$vENCODE05</default>
        </radiobutton>

       </hbox> 
      </frame>
   </hbox>

   <frame $(gettext 'InputFormat          Extra I/P options              vDEVICE            W x H             Extra O/P options')>
    <hbox>
     <hbox width-request=\"90\">
      <entry tooltip-text=\"$(gettext 'For example: video4linux2 for webcam')\">
       <variable>vINPUTFORMAT</variable>
"'     <input>echo "$vINPUTFORMAT"</input>'"
      </entry>
     </hbox>
     <hbox width-request=\"160\">
      <entry tooltip-text=\"$(gettext 'leave blank or add additional ffmpeg input options')\">
       <variable>INvENC_OPTS</variable>
"'     <input>echo "$INvENC_OPTS"</input>'"
      </entry>
     </hbox>
     <hbox width-request=\"90\">
      <entry tooltip-text=\"$(gettext 'For example: /dev/video0 if input format video4linux2')\">
       <variable>vDEVICE</variable>
"'     <input>echo "$vDEVICE"</input>'"
      </entry>
     </hbox>
     <hbox width-request=\"40\">
      <entry tooltip-text=\"$(gettext 'For example 320')\">
       <variable>vWIDTH</variable>
"'     <input>echo "$vWIDTH"</input>'"
      </entry>
     </hbox>
     <hbox width-request=\"40\">
      <entry tooltip-text=\"$(gettext 'For example 240')\">
       <variable>vHEIGHT</variable>
"'     <input>echo "$vHEIGHT"</input>'"
      </entry>
     </hbox>
     <hbox width-request=\"160\">
      <entry tooltip-text=\"$(gettext 'leave blank or, for example: -b 200k -bt 240k')\">
       <variable>OUTvENC_OPTS</variable>
"'     <input>echo "$OUTvENC_OPTS"</input>'"
      </entry>
     </hbox>
    </hbox>
   </frame>

<hbox>
   <frame $(gettext 'Output frame rate(fps)')>
    <hbox>
	  <text><label>$(gettext 'video:')</label></text>
      <hbox width-request=\"24\">
      <entry tooltip-text=\"$(gettext 'For example 26')\">
       <variable>OUTFRATE</variable>
"'     <input>echo "$OUTFRATE"</input>'"
      </entry>
     </hbox>
	 <text><label>$(gettext 'JPEGS:')</label></text>
      <hbox width-request=\"30\">
      <entry tooltip-text=\"$(gettext 'For example 0.2')\">
       <variable>jOUTFRATE</variable>
"'     <input>echo "$jOUTFRATE"</input>'"
      </entry>
     </hbox>
	 <text><label>$(gettext 'x11grab:')</label></text>
      <hbox width-request=\"24\">
      <entry tooltip-text=\"$(gettext 'For example 25')\">
       <variable>xOUTFRATE</variable>
"'     <input>echo "$xOUTFRATE"</input>'"
      </entry>
     </hbox>
    </hbox>
   </frame>

   <frame $(gettext 'X11GRAB: WxH and DISPLAY+Xoffset,Yoffset')>
    <hbox>
	  <checkbox tooltip-text=\"$(gettext 'select for screencast')\">
		<variable>X11GRAB</variable>
		<label> $(gettext 'x11grab')</label>
		<default>$X11GRAB</default>
	 </checkbox>
     <hbox width-request=\"40\">
      <entry tooltip-text=\"$(gettext 'Window capture width. For example 1024')\">
       <variable>X11WIDTH</variable>
"'     <input>echo "$X11WIDTH"</input>'"
      </entry>
     </hbox>
	 <text><label>x</label></text>
     <hbox width-request=\"40\">
      <entry tooltip-text=\"$(gettext 'Window capture height. For example 768')\">
       <variable>X11HEIGHT</variable>
"'     <input>echo "$X11HEIGHT"</input>'"
      </entry>
     </hbox>
	 <text><label>$(gettext 'and')</label></text>
     <hbox width-request=\"32\">
      <entry tooltip-text=\"$(gettext 'X11 Display. For example :0.0')\">
       <variable>XDSPLY</variable>
"'     <input>echo "$XDSPLY"</input>'"
      </entry>
     </hbox>
	 <text><label>+</label></text>
     <hbox width-request=\"40\">
      <entry tooltip-text=\"$(gettext 'Window capture position Xoffset. For example 0')\">
       <variable>XOFS</variable>
"'     <input>echo "$XOFS"</input>'"
      </entry>
     </hbox>
	 <text><label>,</label></text>
     <hbox width-request=\"40\">
      <entry tooltip-text=\"$(gettext 'Window capture position Yoffset. For example 0')\">
       <variable>YOFS</variable>
"'     <input>echo "$YOFS"</input>'"
      </entry>
     </hbox>
    </hbox>
   </frame>
</hbox>


    <hbox width-request=\"640\">       
     <frame $(gettext 'select container for combined audio and video track')>
      <hbox homogeneous=\"true\" width-request=\"601\">
       <radiobutton>
        <variable>vCONTAINER00</variable>
        <label>avi</label>
        <default>$vCONTAINER00</default>
       </radiobutton>
       <radiobutton>
        <variable>vCONTAINER01</variable>
        <label>mpeg</label>
        <default>$vCONTAINER01</default>
       </radiobutton>
       <radiobutton>
        <variable>vCONTAINER02</variable>
        <label>mp2</label>
        <default>$vCONTAINER02</default>
       </radiobutton>
       <radiobutton>
        <variable>vCONTAINER03</variable>
        <label>mp4</label>
        <default>$vCONTAINER03</default>
       </radiobutton>
       <radiobutton>
        <variable>vCONTAINER04</variable>
        <label>ogv</label>
        <default>$vCONTAINER04</default>
       </radiobutton>
       <radiobutton>
        <variable>vCONTAINER05</variable>
        <label>mkv</label>
        <default>$vCONTAINER05</default>
       </radiobutton>       
      </hbox>
     </frame>

<hbox><frame $(gettext 'Combine AV')><hbox>
       <checkbox tooltip-text=\"$(gettext 'select for automatically encapsulate audio and video tracks in selected container when record stop is pressed')\">
        <variable>AUTOMAKE</variable>
        <label>$(gettext 'auto')</label>
        <default>$AUTOMAKE</default>
		<action>if true disable:COMBINE</action>
		<action>if false enable:COMBINE</action>
       </checkbox>
	<button tooltip-text=\"$(gettext 'Encapsulate audio and video tracks in selected single AV container')\">
	 <input file stock=\"gtk-convert\"></input>
	 <variable>COMBINE</variable>
"'   <action>[ $AUTOMAKE = "false" ] && make_avideo "$TDIRFILE_DS1" "$DIRFILE1"</action>'"
	</button>
       <checkbox tooltip-text=\"$(gettext 'select for automatically delete the separate audio and video tracks once the combined AV file is created')\">
        <variable>AUTODEL</variable>
        <label>$(gettext 'del')</label>
        <default>$AUTODEL</default>
       </checkbox>
</hbox></frame></hbox>
<hbox><frame $(gettext 'AV')><hbox>
     <button tooltip-text=\"$(gettext 'PLAY combined AV file')\" width-request=\"24\">
      <input file stock=\"gtk-media-play\"></input>
      <variable>_PLAYAV</variable>
"'    <action>play_combined "$TDIRFILE_DS1" "$DIRFILE1"</action>'"
     </button> 
</hbox></frame></hbox>

    </hbox>
   </frame>
  </hbox>
</hbox>

  <hbox homogeneous=\"true\">
   <frame $(gettext 'General Configuration')>
    <hbox>
     <hbox width-request=\"77\">
      <frame $(gettext 'channel')>
       <checkbox tooltip-text=\"$(gettext 'select for stereo recording')\">
        <variable>CHANNELSTEREO</variable>
        <label> $(gettext 'stereo')</label>
        <default>$CHANNELSTEREO</default>
       </checkbox>
      </frame>
     </hbox>

     <hbox width-request=\"135\"> 
     <frame $(gettext 'select soundcard')>
      <hbox width-request=\"135\">
       <checkbox tooltip-text=\"$(gettext 'If you soundcard is not card 0 click checkbox')\">
        <variable>HWCARD</variable>
        <label> $(gettext 'card')</label>
        <default>$HWCARD</default>
       </checkbox>
       <checkbox tooltip-text=\"$(gettext 'If you soundcard is not subdevice 0 click checkbox')\">
        <variable>HWDEV</variable>
        <label> $(gettext 'device')</label>
        <default>$HWDEV</default>
       </checkbox>
      </hbox>
     </frame>
     </hbox>
<frame>
<hbox>
     <button tooltip-text=\"$(gettext 'Return to Small GUI')\">
      <input file stock=\"gtk-leave-fullscreen\"></input>
      <action>stopplay</action>
"'      <action>stoprec "$TDIRFILE_DS1" "$DIRFILE1"</action>'"
      <action type=\"exit\">REALLY-CONFIG</action>
     </button>
     <button tooltip-text=\"$(gettext 'Help')\">
      <input file stock=\"gtk-help\"></input>
      <variable>HELPSETBUT</variable>
	  <action>launch:HELP_SETUP</action>
     </button>
" 
if [ $(which pschedule) ];then
     MAIN_DIALOG=${MAIN_DIALOG}"
    <hbox>
     <button height-request=\"30\" tooltip-text=\"$(gettext 'Run Pschedule task on time')\">
      <input file>/usr/local/share/icons/mini-clock.xpm</input>
	  "' <action>pschedule "pavrecord rec '\''${TDIRFILE_DS1}'\'' '\''${DIRFILE1}'\'' $DURATION1" &</action>'"
     </button>
    </hbox>
"
fi
MAIN_DIALOG=${MAIN_DIALOG}"
     <button height-request=\"30\" tooltip-text=\"$(gettext 'Pop up MIXER, VU display and video preview')\">
      <input file stock=\"gtk-select-color\"></input>
   	  <action>vu</action>
     </button>
     <button height-request=\"30\" tooltip-text=\"$(gettext 'QUIT')\">
      <input file stock=\"gtk-quit\"></input>
      <action type=\"exit\">EXIT-QUIT</action>
     </button>
</hbox>
</frame>
    </hbox>
   </frame>
  </hbox>
 </vbox>
</window>"
preview
gtkdialog --program=MAIN_DIALOG -G +372+0 >/tmp/${PROGINSTANCENAME}/maindialog
MAIN_DIALOG=$(</tmp/${PROGINSTANCENAME}/maindialog)
echo $MAIN_DIALOG > "$CONFIGFILE"
}

if [ -z "$1" ];then
	_argv1=""
else
	_argv1="$1"
fi
if [ -z "$2" ];then
	_argv2="TDIRFILE_DS1"
else
	_argv2="$2"
fi
if [ -z "$3" ];then
	_argv3="$DIRFILE1"
else
	_argv3="$3"
fi
if [ -z "$4" ];then
	_argv4="$DURATION1"
else
	_argv4="$4"
fi

set -u
# process commandline arguments
# read from stdin
if [ "$_argv1" = "-" ];then
	read _argv1 _argv2 _argv3 _argv4
	[ -z "$_argv1" ] && _argv1=""
	[ -z "$_argv2" ] && _argv2="$TDIRFILE_DS1"
	[ -z "$_argv3" ] && _argv3="$DIRFILE1"
	[ -z "$_argv4" ] && _argv4="$DURATION1"
	TDIRFILE_DS1="$_argv2";DIRFILE1="$_argv3";DURATION1="$_argv4"
fi
case "$_argv1" in
  "")
	helpsetup
	;;
  "rec")
    stoprec "$_argv2" "$_argv3"
	rec "$_argv2" "$_argv3" "$_argv4"
	trap - INT TERM EXIT
	exit;;
  "pauserec")
	pauserec
	trap - INT TERM EXIT
	exit;;
  "stoprec")
	stoprec "$_argv2" "$_argv3"
	trap - INT TERM EXIT
	exit;;
  "play")
	stopplay
	play "$_argv2" "$_argv3" "$_argv4"
	trap - INT TERM EXIT
	exit;;
  "pauseplay")
	pauseplay
	trap - INT TERM EXIT
	exit;;
  "stopplay")
	stopplay
	trap - INT TERM EXIT
	exit;;
  "quit")
	quit
	trap - INT TERM EXIT
	exit;;
  '--help'|'-?')
	printf "$(gettext "After you have configure pavrecord 
via the GUI, you can also control it
from the commandline using:

pavrecord [option] [path to file] [file] [duration]

where [duration] is a time limit for the record
capture or play process:
 
0 means no time limit;
entering no duration results in use of
duration pre-configured for record.
")""$(gettext "

Commandline Arguments:

rec target_directory outfile [duration] : record to outfile.
pauserec : pause/continue recording (toggle).  
stoprec target_directory outfile : stop recording.
play target_directory infile [duration] : play infile.
pauseplay : pause/continue playback (toggle).
stopplay : stop playback.
quit : clean up all and quit program.
--help : this brief usage help.
-? : this brief usage help.
- : read arguments from stdin (e.g. pipe)

Note that pavrecord automatically keeps
track of the running process IDs.

If no options are entered the pAVrecord GUI
will start.\n")"
	trap - INT TERM EXIT
	exit;;
  *)
	helpsetup
	;;
esac

config_dialog_
stoprec "$TDIRFILE_DS1" "$DIRFILE1"
stopplay
while [ "$(echo "$MAIN_DIALOG" | grep REALLY-CONFIG)" != "" ]; do
	. "$CONFIGFILE"
	small_dialog_
	stoprec "$TDIRFILE_DS1" "$DIRFILE1"
	stopplay
	if [ "$(echo "$MAIN_DIALOG" | grep REALLY-CONFIG)" != "" ];then
	  . "$CONFIGFILE"
	  config_dialog_
	  stoprec "$TDIRFILE_DS1" "$DIRFILE1"
	  stopplay
	fi
done # end of swapping gtkdialog menu screens.
if [ "$(echo "$MAIN_DIALOG" | grep 'EXIT="abort"')" != "" ];then
  rm -f "$CONFIGFILE"
fi
quit
trap - INT TERM ERR EXIT
exit 0
