#!/bin/bash

if ! [ -f "$1" ]; then
	exit
fi

export TEXTDOMAIN=attributes
export OUTPUT_CHARSET=UTF-8

who=`whoami`
tmpfile=/tmp/$who-ffplay-gtk

##find a suitable version of gtkdialog (needs 0.7.20 or greater) currently (as of puppy 4 and maybe before)
##the executable is named gtkdialog3. Exit with a warning incase gtkdialog isn't found.
GTKDIALOG=""
if [ "`which gtkdialog4`" != "" ]; then
	GTKDIALOG=gtkdialog4
elif [ "`which gtkdialog3`" != "" ]; then
	GTKDIALOG=gtkdialog3
elif [ "`which gtkdialog`" != "" ]; then
	GTKDIALOG=gtkdialog
elif [ "$GTKDIALOG" = "" ]; then
	echo $(gettext 'gtkdialog not found')
	exit 1
fi

echo "$1" > $tmpfile/attributes

codec_name=`ffprobe -select_streams a:0 -show_entries stream=codec_name -of csv=s=x:p=0 "$1" 2>/dev/null`
echo audio codec name="$codec_name" >> $tmpfile/attributes

sample_format=`ffprobe -select_streams a:0 -show_entries stream=sample_fmt -of csv=s=x:p=0 "$1" 2>/dev/null`
echo audio sample formate="$sample_format" >> $tmpfile/attributes

sample_rate=`ffprobe -select_streams a:0 -show_entries stream=sample_rate -of csv=s=x:p=0 "$1" 2>/dev/null`
echo audio sample rate="$sample_rate" >> $tmpfile/attributes

bit_rate=`ffprobe -select_streams a:0 -show_entries stream=bit_rate -of csv=s=x:p=0 "$1" 2>/dev/null | sed 's/000$/k/'`
if [ "$bit_rate" = N/A ]; then
	bit_rate=`ffprobe -v error -show_format -show_streams "$1" 2>/dev/null | grep bit_rate | tr -c -d [0-9] | sed 's/000$/k/'`
fi
if [ "$bit_rate" = "" ]; then
	bit_rate=N/A
fi
echo audio bit rate="$bit_rate" >> $tmpfile/attributes

layout=`ffprobe -select_streams a:0 -show_entries stream=channel_layout -of csv=s=x:p=0 "$1" 2>/dev/null`
if [ "$layout" = "" ] || [ "$layout" = unknown ]; then
	layout=`ffprobe -select_streams a:0 -show_entries stream=channels -of csv=s=x:p=0 "$1" 2>/dev/null`
fi
if [ "$layout" = 1 ]; then
	layout=mono
fi
if [ "$layout" = 2 ]; then
	layout=stereo
fi
echo audio channel layout="$layout" >> $tmpfile/attributes

sample=`ffprobe -select_streams a:0 -show_entries stream=bits_per_raw_sample -of csv=s=x:p=0 "$1" 2>/dev/null`
if [ "$sample" = "" ] || [ "$sample" = "N/A" ]; then
	sample=`ffprobe -select_streams a:0 -show_entries stream=bits_per_sample -of csv=s=x:p=0 "$1" 2>/dev/null`
fi
if [ "$sample" = 0 ]; then
	sample="N/A"
fi
echo audio bits per sample="$sample" >> $tmpfile/attributes

echo >> $tmpfile/attributes

ffprobe -v error -show_format -show_streams "$1" >> $tmpfile/attributes
	
export MAIN_DIALOG="
<window title=\"ffplay-gtk $(gettext 'file attributes')\" resizable=\"true\">
	<vbox>
		<list>
			<variable>LIST</variable>
			<width>750</width>
			<height>750</height>
			<input file>$tmpfile/attributes</input>
		</list>
		<hbox>
		<button>
			<input file icon=\"gtk-quit\"></input>
        <label>$(gettext 'Exit')</label>
        <action type=\"exit\">EXIT_NOW</action>
      </button>
      </hbox>
	</vbox>
</window>
"
results="`$GTKDIALOG --program=MAIN_DIALOG --center`"
