#!/bin/sh

# disable filename globbing

 echo Content-type: text/html
 echo
 BASE=`pwd`
{
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
TZ=America/Santiago
export PATH APPLDIR DATADIR TZ
T=/tmp/T$$
T1=/tmp/T1$$
T2=/tmp/T2$$
LN=./tmp/LN
./get >$T
awk -F= '
$0=="" || substr($1,1,1)=="T" || /DOCstatusclientes/ || $1=="scroll" { next }
$2!=""  ' $T > $T1
QRY=`awk -F= ' $1== "B1" {print $2} ' $T1 `
FRM=`awk -F= ' $1== "statusclientesH01" {print $2} ' $T1 `
if [ "$QRY" = Consultar ]
then
	COMMAND=`awk -F= ' BEGIN { printf "./ihd/bin/ihmq statusclientes " }
	index($1,"statusclientes") {
	if (NR==1 && substr($1,length($1)-1) !="00") printf "H00^ "
	if (index($1,"D")) printf "D%s^%s ",substr($1,length($1)-1),adv($2)
	else printf "H%s^%s ",substr($1,length($1)-1),adv($2)
}
function adv(txt) { while (substr(txt,1,1)==" ") txt=substr(txt,2); return (txt) }
END {
if (NR==1) print "H00^ "
print "" } ' $T1 `
$COMMAND | sort -t\^ -k2 -k1 -u  >$T2
NF=`awk ' END { print NR } ' $T2`
if [ $NF = 1 ]
then
		REG=`awk -F"^" ' { print $1 } ' $T2`
		./statusclientesget.cgi $REG
	else
	./statusclienteslist.cgi $T1 $T2
	fi
fi
if [ "$QRY" = Actualizar -o "$QRY" = FRM ]
then
	COMMAND=`awk -F= ' END { printf "./ihd/bin/ihmu statusclientes %s -N0\n","'"$T2"'" } ' `
	awk -F= '
	/statusclientesF/ { printf "H%s%s\n",substr($1,length($1)-1),substr($0,length($1)+2) }
	/statusclientesH/ { printf "H%s%s\n",substr($1,length($1)-1),substr($0,length($1)+2) }
	/statusclientesD/ { printf "D%s%s\n",substr($1,length($1)-3),substr($0,length($1)+2) }
	END { print " " }
	' $T1 > $T2
	$COMMAND 
	
	l data/statusclientes/INFO | awk ' { printf "document.getElementById(\"Misc01\").innerHTML=\"LUPD: %s %s %s\"\n",$6,$7,$8 } ' >../js/Rstatusclientes.js
	cat data/statusclientes/INFO | awk ' { printf "document.getElementById(\"Misc02\").innerHTML=\"RH: %.5d &nbsp;&nbsp;&nbsp;LD: %.6d\"",$1,$2 } ' >>../js/Rstatusclientes.js
	if [ "$QRY" = FRM ]
	then
		[ $FRM != newform ] && ./statusclientes.web newform 
		./ihd/bin/ihdm newform h00= h01= >$LN
		./statusclientes.cg $FRM $T
		./dlin
	else 
	cat ../statusclientes
	fi
fi
if [ "$QRY" = Ejecutar ]
then
	TZ=America/Santiago
	export TZ
	l -c ./statusclientes.rep | awk ' { printf "document.getElementById(\"Misc02\").innerHTML=\"%s %s %s\"\n",$6,$7,$8 } ' >../js/Rstatusclientes.js
	awk ' END { printf "document.getElementById(\"Misc01\").innerHTML=\"ULTIMA EMISION\"",$1,$2 } ' >>../js/Rstatusclientes.js
	./statusclientes.rep $T1
fi
if [ "$QRY" = CLEAR ]
then
        cat ../statusclientes
fi
 } 2> ./error/statusclientes
 cat ./error/statusclientes
rm $T $T1 $T2
