#!/bin/bash
#
# Zephyr Config Set handler 
#
#
TAMPON="$ Thursday 12/01/05 11:28:48 md(root) mathieu.deschamps@com2gether.net$"
#
#
#
# RET = 0: all ok, 1: In function error, 127 : user interrupt 
#
# LOGPATH needs to be set properly
# PATH must be set properly
#
#
# ReadWrite settings ! 
#------------------------------------------------------------------#
SIMPLEEDIT="`whereis vim | cut -f2 -d" "`"
LANG="en_US.UTF-8"
SYSDATE=`echo $SPOST | cut -f1-3 -d"-"`
# ------------------------------------------------------------------#
## Readonly settings !
CMD="$0"
ZSS="";ACTION="";CFG_SET=;DTE=;
# To trigger SIMPLEEDITING anyway
NOAFF=1
# To trigger changing BR Config.in to match given config set
BR_DEF=0
# To trigger viewing after adding a configset
VIEW_AFTER_ADD=1
# ------------------------------------------------------------------#
#


# Crucial parameter check
if [ "x$SYSDATE" = "x" ]; then
	echo -e "SPOST variable must be set correctly at least formated likewise : YYYY-MM-DD\n -Exiting."
	return 0
fi
if [ "x$ROOTBUILD" != "x" ] && [ -d "$ROOTBUILD" -o -x "$ROOTBUILD" ]; then
	 echo "Buildroot home is $ROOTBUILD"
else
	 echo -e "Buildroot home path ROOTBUILD variable unset\nOr $ROOTBUILD is not a accessible directory\n -Exiting."
	 return 0;
fi


# Xdialog or dialog that is the question 

( dialog --version 1>/dev/null 2>&1)  && DIAL="dialog"   
( Xdialog --version 1>/dev/null 2>&1) && DIAL="Xdialog" ||\
(echo "No dialog lib. to display sorry, using $SIMPLEEDIT... " && NOAFF=1)



#
# Func which dialog display the log results
# ARG  $1 is the log file name
#
function aff
{
 FICH="$1"
if [ $NOAFF -eq 1 ]; then
	[ -f ${FICH} ] && $SIMPLEEDIT "${FICH}" || echo "le fichier ${FICH} n'existe pas."
else
	if  [ -f "${FICH}" ]; then

		LIG=`stty -a | head -1 | cut -f2 -d";" | cut -f3 -d" "`
		COL=`stty -a | head -1 | cut -f3 -d";" | cut -f3 -d" "`
		# Let's have some place off around
		COL=`expr $COL - 9`
		LIG=`expr $LIG - 9`

		 $DIAL  --backtitle "Kernel building status : View of "${FICH}" journal"\
		        --logbox  "${FICH}" $LIG $COL 2>/dev/null
			return $? 
	else
			echo "le fichier ${FICH} n'existe pas."
 		 	
	fi
fi
}


#
# Func return 0 and the Usage of this script
#
function usage ()
{
			echo "    Zephyr ConfigSet handler"
			echo " Usage : configset.sh [-s,-bd] dev|prod cmds <configset_date> "
			echo 
			echo "Options (triggers):"
			echo -e " --simple-edit,-s	\t:\t Edit/display simply (nodialog)"
			echo -e " --buildroot-default,-bd \t:\t Set buildroot's default config. to given configset (Config.in patching)"
			echo -e " --onadd-view,-av	\t:\t After adding, it enables viewing and modifing new configset"
			echo -e " --force, -f		\t:\t Do not prompt on copies and removals"
			echo -e " --interactive, -i	\t:\t Prompt on each copy and removal"
			echo "Zephyr Support System : zss"
			echo -e " dev  : dev class support \t\t[$ROOTBUILD/config/dev]" | tr -s "/"
			echo -e " prod : prod class support \t\t[$ROOTBUILD/config/prod]" | tr -s "/"
			echo "Commands:	cmds"
			echo -e  " add, a \t\t\t:\t New configset on configset_date basis(stamped today : $SYSDATE) "
			echo -e  " del, d \t\t\t:\t Delete given configset_date"
			echo -e  " pkg, p \t\t\t:\t Package and save given configset_date"
			echo -e " view, v \t\t\t:\t View files holds in given config_date"
			echo "$TAMPON"
}


#
# Func displaying in enhanced or simple mode configset files
#
# $1 : Zephyr Support System { dev | prod }
# $2 : date formatted like YYYY-MM-DD
#
function display
{

	[ "x$1" != "x" ] && ZSS="$1"
	[ "x$2" != "x" ] && DTE="$2"
	 

			LIST=`ls -1d $ROOTBUILD/config/$ZSS/*-$DTE* 2>/dev/null | sort -r | tr '\n' ' '`
	
				for i in $LIST
				do
					ISASCII=`file $i | cut -f2 -d":" | cut -f1-2 -d " " | tr -d " "`
					if [ "x$ISASCII" = "xASCII" ]; then
						aff $i
						echo $i | tr -s "/"
						ISASCII=""
					fi
				done
				if [ "x$LIST" = "x" ]; then
					echo "$ROOTBUILD/$ZSS/*.$DTE files absent, quitting." | tr -s "/"
					return 1
				fi
	return $?
}


#
# Check date validity
# $1 : date input
# 
#
function validdate 
{
	FFR="$1"

	if [ "x$FFR" = "x" ] ; then
		echo -e "Fatal : date is void !" ; return 99
	fi
		FFR2=`echo $FFR | tr "-" "\n" | wc -l | tr -d " "`
		FFR22=`echo $FFR | cut -f3 -d"-"`
	if [ "$FFR2" -ne 3 ] || [ "x$FFR22" = "x" ] ; then
		echo -e "Error : date is badly formated, remember that's YYYY-MM-DD (ie 1979-12-24) !" ; return 1
	fi 	
		FFR3=`echo $FFR | cut -f1-3 -d"-" | tr -d "*"`	
	if [ "$FFR3" != "$FFR" ] ; then
		echo -e "Error : date is invalid ! Don't use any metacharacters." ; return 1
	fi

	return 0
}


#
# Permits a Congifset late input. exports the configset
#

function lateinput
{
echo "Hey, I need a configset date ! $ROOTBUILD/config/$ZSS/ holds :"
ls -1d  $ROOTBUILD/config/$ZSS/*-*-*
NOGOOD=1
	while [ $NOGOOD -ne 0 ]
	do
		echo -e "Please, cut/paste or enter date of configset to view its files\n format is (YYYY-MM-DD) : "
		read CFG_SET
		validdate "$CFG_SET"
		NOGOOD="$?"
		[ $NOGOOD -eq 99 ] && return 1
	done
	export CFG_SET
return 0
}

	while [ $# -ne 0 ]
	do
		# While we have no ZSS and CFGSET file defined
		if [ "x$ZSS" = "x" ] ; then 
			case "x$1" in
				"x--simple-edit" | "x-s")
					echo -n "Toggling simple editing :"
					if [ "$NOAFF" = 0 ]; then 
						NOAFF=1; echo "ON"
					else
						NOAFF=0; echo "OFF"
					fi
					shift;;

				"x--buildroot-default" | "x-bd")
					echo -n "Patching in buildroot's config defaults configset to current one :"
                                        if [ "$BR_DEF" = 0 ]; then
                                                BR_DEF=1; echo "ON"
                                        else
                                                BR_DEF=0; echo "OFF"
                                        fi
					shift;;

				"x--onadd-view"| "x-av" )
                                        echo -n "Viewing configset after adding :"
                                        if [ "$VIEW_AFTER_ADD" = 0 ]; then
                                                VIEW_AFTER_ADD=1; echo "ON"
                                        else
                                                VIEW_AFTER_ADD=0; echo "OFF"
                                        fi

					shift;;

				"x--interactive" | "x-i")
					alias cp="cp -i"
					alias rm="rm -i"
					shift;;	

				"x--force" | "x-f")
					alias cp="cp -f"
                                        alias rm="rm -f"
					shift;;

				"x--help" | "x-h")
					usage
					shift; return 0;;
				
				"xdev")		
					ZSS="dev";shift;;
				"xprod")
					ZSS="prod";shift;;

				*)
					if [ "x$ZSS" != "x" ] && [ "x$ACTION" != "x" ]; then 
						echo "Configset date is : $1"
						validdate "$1"
						if [ $? -ne 0 ];then break; else CFGSET="$CFGSET $1";fi
						shift
					else 
					     if [ "x$ZSS" = "x" ]; then
						echo -e "No Zephyr Support System nammed after \"$1\" \n Or is it a configset file specification ?  First specify a ZSS !  "
						shift
						return 1
					     fi
					fi
				;;
			
			 
			esac
		else
		
			case "x$1" in

				"x--help" | "x-h")
                                       usage
                                       shift; return 0;;

				"xview" | "xv")
					ACTION=view
					shift
				;;
				
				"xadd" | "xa")
					ACTION=add
					shift
				;;
                                "xdel" | "xd")
                                        ACTION=del
                                        shift
                                ;;
			
				"xpkg" | "xp")
					ACTION=pkg
					shift
				;;	
				*)		DTE="$1"
						echo "Configset date is : $DTE"
                                                validdate "$DTE"
                                                if [ "$?" -eq 0 ]; then
							CFG_SET="$DTE"
						fi
						shift
				;;
			esac
		fi
	done

while [ "$ACTION" != "quit" ]; do
	case "$ACTION" in

		       "--help" | "-h")
				       usage
				       shift; return 0;;


			pkg)
                                        if [ "x$CFG_SET" = "x" ]; then
                                                        lateinput
                                                        [ $? -ne 0 ] && return 1

                                        fi

                                        CHEM=`echo $ROOTBUILD/config/$ZSS | tr -s "/"`
					ARCH="configset-$CFG_SET"
                                        echo "]Packaging from $CHEM configset $CFG_SET : archive is $CHEM/$ARCH.tgz"
                                        LIST=`find $CHEM -name "*-$CFG_SET*" | tr "\n" " "`
                                        for i in $LIST
                                        do
                                                FICH=`basename $i`
                                                echo -en "Packaging $ARCH.tar, with $FICH... \t\t"
                                                tar -rf "$CHEM/$ARCH.tar" "$CHEM/$FICH" 1>/dev/null 2>&1
                                                [ "$?" -eq 0 ] && echo "ok" || echo "ERROR"
                                        done
						echo -en "Gziping to $CHEM/$ARCH.tgz ... \t\t"
						gzip -c9 "$CHEM/$ARCH.tar" > $CHEM/$ARCH.tgz 
						if [ "$?" -eq 0 ]; then
							rm "$CHEM/$ARCH.tar" -f 1>/dev/null 2>&1
							echo "ok" 	
						else	
							echo "ERROR"
						fi
                                        break
                        ;;



			del)
                                        if [ "x$CFG_SET" = "x" ]; then
                                                        lateinput
                                                        [ $? -ne 0 ] && return 1

                                        fi
					CHEM=`echo $ROOTBUILD/config/$ZSS | tr -s "/"`
                                        echo "]Deleting in $CHEM configset $CFG_SET"
                                        LIST=`find $CHEM -name "*-$CFG_SET*" | tr "\n" " "`
                                        for i in $LIST
                                        do
						FICH=`basename $i`
                                                echo -en "Delete $CHEM/$FICH... \t\t"
                                                rm "$CHEM/$FICH" -r
                                                [ $? -eq 0 ] && echo "ok" || echo "ERROR"
                                        done
					break
			;;

			add)
					if [ "x$CFG_SET" = "x" ]; then
							lateinput
							[ $? -ne 0 ] && return 1

					fi
					CHEM=`echo $ROOTBUILD/config/$ZSS | tr -s "/"`
					echo "]Add a new configset in $CHEM dated $SYSDATE from template configset $CFG_SET" 
					LIST=`find $CHEM -name "*-$CFG_SET*" | tr "\n" " "`
					for i in $LIST
					do
						ORIG=`basename $i`
						DEST=`echo $ORIG | sed -e s/$CFG_SET/$SYSDATE/g`
						echo -en "Adding $CHEM/$DEST from $CHEM/$ORIG... \t\t"
						cp -a "$CHEM/$ORIG" "$CHEM/$DEST" 
						[ $? -eq 0 ] && echo "ok" || echo "ERROR"
					done
					if [ "$VIEW_AFTER_ADD" -eq 1 ]; then 
						ACTION=view	
						CFG_SET=$SYSDATE
						continue
					else
						ACTION=quit
					fi
			;;	

			view)	
					if [ "x$CFG_SET" = "x" ]; then
							lateinput
							[ $? -ne 0 ] && return 1
							
					fi
					echo "]Displaying $ZSS $CFG_SET" 
					display $ZSS $CFG_SET
					break					
			;;
			
			*)
					if [ "x$ACTION" = "x" ]; then
						echo -n "No action specified... "
						[ "x$DTE" != "x" ] && echo -n "Or is $DTE one ?!"
						echo " Wandering around..."
					fi
					break
			;;
	esac
done

unset SIMPLEEDIT CMD ARG NOAFF LIST COL LIG RET CFG_SET SYSDATE ACTION 2>/dev/null 


