#!/bin/ksh
if [ $VALIDATE = 'y' ]
then
	echo "GDGEN : started in validation mode"
	exit 0
fi
if [ $# -ne 4 ]
then
	echo "GDGEN : started incorrectly"
	exit 1
fi
count=`expr $2 - 1`
count1=`expr ${count} - 1`
if [ ! -f $3 ]
then
	echo "GDGEN : $3 DELETED"
	exit 0
fi
#
#Uncomment these lines if you do not want to catalogue not opened dataset.
#if [ ! -s $3 ]
#then
#	echo "GDGEN : $3 NOT OPENED DELETED"
#	rm $3
#	exit 0
#fi
#
if [ "$4" = "DB" ]
then
	gen=`echo ${count} | awk ' {printf "%2.2d",$1}'`
	if [ -f $1_${gen} ]
	then
		rdmdl -f $1_${gen}
	fi
fi
while [ ${count} -ge 0 ] 
do
	gen=`echo ${count} | awk ' {printf "%2.2d",$1}'`
	gen1=`echo ${count1} | awk ' {printf "%2.2d",$1}'`
	if [ -f $1_${gen1} ]
	then
		if [ "$4" = "DB" ]
		then
			rdmmfa -f $1_${gen1} -nn $1_${gen}
		else
			mv $1_${gen1} $1_${gen}
		fi
	fi
	count=`expr $count - 1`
	count1=`expr $count1 - 1`
done
if [ -f $3 ]
then
	if [ "$4" = "DB" ]
	then
		rdmmfa -f $3 -nn $1_00
	else
		mv $3 $1_00
	fi
fi
echo "GDG $1 CATALOGUED"
exit 0
