#!/bin/bash
###############################################################################
# $Id: gds-csv-info.sh,v 1.5 2010/09/26 21:54:24 tw Exp $
#
# Provide information about GDS CSV files
#
# Copyright (C) 2010 Thomas Weidenfeller
# 
# This file is part of gds2000tools.
# 
# gds2000tools is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
# 
# gds2000tools 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.
# 
# You should have received a copy of the GNU General Public License
# along with gds2000tools.  If not, see <http://www.gnu.org/licenses/>.
###############################################################################

function dofile () {
	case `basename "$1"` in
	*.csv|DS????.CSV|A????CH?.CSV|A????RF?.CSV|A????W??.CSV)
		;;
	*)	return
		;;
	esac

	echo "$1"
	awk -F, '
	BEGIN {
		time   = "?"
		source = "?"
		memlen = "?"
		mode   = "?"

		monthnum["Jan"] = 1
		monthnum["Feb"] = 2
		monthnum["Mar"] = 3
		monthnum["Apr"] = 4
		monthnum["May"] = 5
		monthnum["Jun"] = 6
		monthnum["Jul"] = 7
		monthnum["Aug"] = 8
		monthnum["Sep"] = 9
		monthnum["Oct"] = 10
		monthnum["Nov"] = 11
		monthnum["Dec"] = 12
	}
	FNR == 1 && $1 != "Memory Length" {
		ExitCode = 1
		exit ExitCode
	}
	$1 == "Waveform Data" {
		exit 0
	}
	n >= 4 {
		exit 0
	}
	$1 == "Memory Length" {
		memlen = $2
		n++
		next
	}
	$1 == "Source" {
		source = $2
		n++
		next
	}
	$1 == "Time" {
		split($2, t, "[ :-]*")
		time = sprintf("%04d-%02d-%02d %02d:%02d", 	\
			2000 + t[3], monthnum[t[2]], 0 + t[1],	\
			0 + t[4], 0 + t[5])
		n++
		next
	}
	$1 == "Horizontal Mode" {
		mode = $2
		n++
		next
	}
	END {
		if(ExitCode) exit ExitCode
		print time
		print source
		print memlen
		print mode
	}
	' "$1" 2>/dev/null
}

for f
do
	if [ -f "$f" ] ; then
		dofile "$f"
	elif [ -d "$f" -a -r "$f" ] ; then
		\ls -1 "$f" 2>/dev/null | while read ff ; do
			dofile "$f/$ff"
		done
	fi
done | zenity --list	\
		--title "GDS Oscilloscope CSV File Info"		\
		--text ""			\
		--width 600			\
		--multiple 			\
		--column "Name           "	\
		--column "Time                 "\
		--column "Source"		\
		--column "Length"		\
		--column "Mode"
