#!/bin/sh
### ------------------------------------------------------------------
### | Copyright Unpublished, Atria Software, Inc.  All Rights        |
### | Reserved.  This software contains proprietary and confidential |
### | information of Atria and its suppliers.  Use, disclosure or    |
### | reproduction is prohibited without the prior express written   |
### | consent of Atria.                                              |
### ------------------------------------------------------------------
###
#
# Extract versions from a ClearCase delta container file.
#

ATRIA_DIR=${ATRIAHOME:-/usr/atria}
PROGN=`basename $0`
ECHO=/bin/echo
UNAME=/bin/uname
SLEEP=/bin/sleep
SED=/bin/sed
OS_TYPE=`$UNAME -s`
OS_VER=`$UNAME -r`
VERBOSE="FALSE"
if [ "$1" = "-v" ] ; then
    VERBOSE="TRUE"
fi

if [ "$CLEARCASE_ROOT" = "" ] ; then
    $ECHO "Warning: You have no view set so pathnames won't be printed."
else
    if $ATRIA_DIR/bin/cleartool describe .@@ >/dev/null 2>/dev/null ; then
	echo >/dev/null
    else
       $ECHO "Warning: You are not in a VOB so pathnames won't be printed."
    fi
fi

sgi_echo_n() { $ECHO "$*\c" ; }
hp_echo_n() { $ECHO "$*\c" ; }
osf1_echo_n() { $ECHO "$*\c" ; }
sun4_echo_n() { $ECHO -n "$*" ; }
aix_echo_n() { $ECHO -n "$*" ; }
unix_sv_echo_n() { $ECHO -n "$*" ; }

case "$OS_TYPE" in

'IRIX*')
	ECHO_N=sgi_echo_n
	;;

'SunOS')
	ECHO_N=sun4_echo_n
	;;

'HP-UX')
	ECHO_N=hp_echo_n
	;;

'OSF1')
	ECHO_N=osf1_echo_n
	;;

'AIX')
	ECHO_N=aix_echo_n
	;;
'UNIX_SV')
	ECHO_N=unix_sv_echo_n
	;;

*)
	$ECHO "*** $PROGN *** Unknown OS type: $OS_TYPE"
	exit -1
	;;
esac

SINCE_TIME=""

$ECHO_N "Where is the VOB storage directory? "
read VOB_STORAGE_ROOT_PN
if [ ! -r $VOB_STORAGE_ROOT_PN/s ] ; then
    $ECHO "$VOB_STORAGE_ROOT_PN doesn't look like a vob storage root!"
    exit 1
fi

$ECHO_N "Recover versions since when (8-hex-digits or <CR> for LATEST)? "
read SINCE_TIME

if [ "$SINCE_TIME" = "" ] ; then
    VERBOSE="TRUE"
fi

$ECHO_N "Recover only versions before when (8-hex-digits or <CR> all versions)? "
read BEFORE_TIME

$ECHO_N "Where do you want the results (<CR> for "/usr/tmp")? "
read TO_DIR_PN

if [ "$TO_DIR_PN" = "" ] ; then
    TO_DIR_PN="/usr/tmp"
fi

/bin/find $VOB_STORAGE_ROOT_PN/s -type f -print | while read CNTR_PN ; do

    ELEM_OID=`/bin/grep '^\^E [0-9a-f:.]*$' $CNTR_PN | $SED -e 's/\^E //'`
    if [ "$ELEM_OID" = "" ] ; then
	continue
    fi

    if [ "$VERBOSE" = "TRUE" ] ; then
	ELEM_PN="`$ATRIA_DIR/bin/cleartool dump -oid $ELEM_OID | grep '\@\@'`"
	ELEM_LEAF="`basename $ELEM_PN @@`"
    else
	ELEM_PN="$ELEM_OID"
	ELEM_LEAF="$ELEM_OID"
    fi

    if [ "$ELEM_PN" = "" ] ; then
	# Couldn't find element -- may be new
	ELEM_PN="$ELEM_OID"
	ELEM_LEAF="$ELEM_OID"
    fi

    if [ "$VERBOSE" = "TRUE" ] ; then
	$ECHO "Considering $ELEM_PN ..."
    else
	$ECHO_N "."
    fi

    /bin/grep '^\^V [0-9a-f:.]*' $CNTR_PN | \
	while read CTRL VER_OID BRANCH GEN DTM ; do
	    # Do since a date?
	    if [ "$SINCE_TIME" != "" ] ; then
  	        # Do those later than given date
	        if [ "`($ECHO $SINCE_TIME; $ECHO $DTM) | sort | head -1`" = "$DTM" ] ; then
		    break # all done, get out of loop
	        fi
	    fi

	    # Do before a date?
	    if [ "$BEFORE_TIME" != "" ] ; then
  	        # Do those before given date
	        if [ "`($ECHO $BEFORE_TIME; $ECHO $DTM) | sort | head -1`" = "$BEFORE_TIME" ] ; then
		    continue # all done, get out of loop
	        fi
	    fi

	    if [ "$BRANCH" = "0" ] ; then
		BRANCH="main"
	    fi

	    # DO IT!
	    # Tell user what's happening
	    if [ "$VERBOSE" = "FALSE" ] ; then
		# stop printing ....... and get the element pathname
		$ECHO
		ELEM_PN="`$ATRIA_DIR/bin/cleartool dump -oid $ELEM_OID | grep '\@\@'`"
		ELEM_LEAF="`basename $ELEM_PN @@`"
		if [ "$ELEM_PN" = "" ] ; then
		    # Couldn't find element -- may be new
		    ELEM_PN="$ELEM_OID"
		    ELEM_LEAF="$ELEM_OID"
		fi
	    fi
	    $ECHO "  Fetching $ELEM_PN to $TO_DIR_PN/$ELEM_LEAF.$BRANCH.$GEN"
	    $ECHO "    [from delta container $CNTR_PN]"

	    # FETCH VERSION
	    # Command line for constructing (fetching) a version
	    # <manager pathname>     source_container_pname
	    #  	    	    	 copy_into_pname
	    #  	    	    	 ver_oid
	    #
	    $ATRIA_DIR/lib/mgrs/text_file_delta/construct_version \
		$CNTR_PN "$TO_DIR_PN/$ELEM_LEAF.$BRANCH.$GEN" $VER_OID

	    # Only do latest?
	    if [ "$SINCE_TIME" = "" ] ; then
	        break
	    fi
        done # versions
    
done # find
