#!/bin/sh
# Version: 0.2.0
# Author: Dimitrios Dimitrakos mail@dimitrios.de
# $Id: status.sh,v 1.6 2005/01/11 17:00:54 dimi Exp $

LANG=C
unset COLUMNS
#set -x
vco_user=`id -n -u`
homedir=`cat /etc/passwd|grep "^${vco_user}:"|cut -f6 -d ':'`
. $homedir/.vdrconvert/vdrconvert.env

#[ "$DEBUG" = "yes" ] && set -x 

if [ ! -z "$SERVER" ]
then
	ssh $SERVER $0
else
OUTF=/tmp/vcops.$$
/bin/ps -aef > $OUTF
for i in vdrconvert $CONVERT_JOBS
do

	[ `cat $OUTF|grep ${i}.sh|grep -v grep|wc -l` -gt 0 ] && printf "%-10s\t\t%s\n" "$i" "Running "  >> /tmp/status.$$ || printf "%-10s\t\t%s" "$i" "Not running" >> /tmp/status.$$
	if [ -f "`cat $OUTF|grep ${i}.sh|grep -v grep|awk '{print ($10)}'`" ]
	then
		activ=`cat $OUTF|grep ${i}.sh|grep -v grep|awk '{print ($10)}'`
		if [ "$activ" != "vdr" ]
		then
		    toprint=`cat $activ`
		    let zeilen=1
		    doczeilen=`cat $activ|wc -l`
		    while [ $zeilen -le $doczeilen ]
		    do
			[ $zeilen -le $doczeilen ] && printf "  %3d)\t" $zeilen >> /tmp/status.$$
			Line=`cat $activ|head -n  $zeilen|tail -n 1`
			for w in $Line
			do
		    	    spalten=`echo $w|sed 's/\// /g'|wc -w`
		       	    Record=`echo $w|sed 's/\%//g'|cut -f $spalten -d '/'`
		    	    printf "%s\n        " $Record >> /tmp/status.$$
	        	done
			printf "\r" >> /tmp/status.$$
			let zeilen=zeilen+1
		    done
		fi
	fi
	#printf "\n" >> /tmp/status.$$
	let zzeilen=zeilen-1
	FILE=$VCOQUEUEDIR/$i
	if [ -f $VCOQUEUEDIR/$i ]
	then
	    cat $FILE|grep '^/' > $FILE.$$
	    if [ -s $FILE.$$ ]
	    then
		printf "\n%-10s\t\t%s\n" "" "Waiting" >> /tmp/status.$$
	
		let zeilen=1
		doczeilen=`cat $FILE.$$|wc -l`
		while [ $zeilen -le $doczeilen ]
		do
			[ $zeilen -le $doczeilen ] && printf "  %3d)\t" $[$zeilen+$zzeilen] >> /tmp/status.$$
			Line=`cat $FILE.$$|head -n $zeilen|tail -n 1`
			for w in $Line
			do
	    		    spalten=`echo $w|sed 's/\// /g'|wc -w`
	        	    Record=`echo $w|sed 's/\%//g'|cut -f $spalten -d '/'`
	    		    printf "%s\n        " $Record >> /tmp/status.$$
        		done
			printf "\r" >> /tmp/status.$$
			let zeilen=zeilen+1
		done
	    fi
	fi
	printf "\n" >> /tmp/status.$$
        cat /tmp/status.$$
        rm -f /tmp/status.$$
        rm -f $FILE.$$
done
rm -f $OUTF
fi
