#!/bin/sh
# +--------------------------------------------------------------------------+
# | Copyright 1996 - 2002 Rational Software Corporation. All Rights Reserved.|
# | This software contains proprietary and confidential information of       |
# | Rational and its suppliers. Use, disclosure or reproduction is           |
# | prohibited without the prior express written consent of Rational.        |
# +--------------------------------------------------------------------------+
# @(#)$Revision: 1.10 $  $Date: 02/20/2002 16:10:05 $
#
# When adding other command aliases to this script, add to this list:
alias_list="readfile writefile findfile listfile"
#**************************************************************************
#SOHT
#
#NAME
#     readfile  - Read product tar archive files.
#     writefile - Write product tar archive files.
#     listfile  - List the contents of a product tar archive file.
#     findfile  - Find the given files in a comp dir.
#
#SYNOPSIS
#     readfile  [<OPTIONS>] <N>...|all
#     writefile [<OPTIONS>] <N>...|all
#     listfile  [<OPTIONS>] <N>...
#     findfile  [<OPTIONS>] <file>...
#
#DESCRIPTION
#     The readfile script will read file<N> from the current directory.
#     If you specify 'all' will read file0..file<N> from the current directory.
#     The writefile script will create file<N> in the current directory
#       from the files in the comp<N> directory.
#     If you specify 'all' will write file0..file<N> in the current directory.
#       from the files in the comp0..comp<N> directories.
#     The listfile script will display the list of files in file<N>.
#     You can specify one or more <N> numbers.
#
#OPTIONS
#    all    Read or write all files.
#     -a    Archive share dir if in comp0.
#     -d    Delete comp<N> dir after write or file<N> after read.
#     -e    Extract the prod_desc file from comp0 or file0.
#     -f <X>
#           For readfile, only un'tar <X>.
#     -gt   Use gtar.  The default is to use the same tar command used by
#           makeprod.
#     -gz   Use gzip compression.
#     -h    Show this usage.
#     -i    Install command aliases.
#     -nc   No compression.  The default is to use the same compress command
#           used by makemedia, recorded in the prod_desc file.
#     -r    When running 'writefile all' only write files not written.
#     -s    Show command to do read or write but do not run it.
#     -st   Storage directory for files for writefile.
#     -v    Verbose--show files as they are being written or read.
#
#ASSUMPTIONS
#     The gzip and gtar commands may need to be on your search path.
#     The ARCH env var must be set the same as APEX_ARCH.
#
#ERROR CODES
#     1 = improper command usage
#     2 = Storage directory not found.
#     3 = The current directory is not writeable.
#
#EOHT
#**************************************************************************

FILE=$0
CMD=`basename $0`
DO=one
ARCHIVE=maybe
LIST=
RECOVER=no
INSTALL=no
RMF=':'
RMFR=':'
ECHORM=':'
RUN=
ST='..'
vop=
compress=yes
ARCH=${ARCH:-$APEX_ARCH}
VERBOSE=':'

#------------------------------------------------------------------------
Usage() {
#------------------------------------------------------------------------

    status=$1
    if [ "$status" = 0 ]
    then
        # Extract the text between #SOHT and #EOHT in this script
        sed -e '1,/^#SOHT/d' -e '/^#EOHT/,$d' -e 's/^#//' $FILE
    else
        echo "Run '$CMD -h' to get usage info."
    fi
    exit $status
}

#------------------------------------------------------------------------
find_table() {
#------------------------------------------------------------------------

    for table in $*
    do
        if grep "$file" $table >/dev/null
        then
            echo "$table:"
            grep "$file" $table
        fi
    done
}

#------------------------------------------------------------------------
unarchive_share() {
#------------------------------------------------------------------------

    if [ -d $1 ]
    then
        install_dir=$1
    else
        install_dir=$2
    fi

    if [ -f $install_dir/share.tar.Z ]
    then
        (
        echo "Unarchiving $install_dir/share.tar.Z"
        cd $install_dir
        $VERBOSE uncompress share.tar.Z
                 uncompress share.tar.Z
        $VERBOSE tar -x${vop}f share.tar
                 tar -x${vop}f share.tar
        $VERBOSE /bin/rm -f share.tar
                 /bin/rm -f share.tar
        $VERBOSE chmod -R g+w share
                 chmod -R g+w share
        touch archive_share
        )

    elif [ -f $install_dir/share.tar.gz ]
    then
        (
        echo "Unarchiving $install_dir/share.tar.gz"
        cd $install_dir
        $VERBOSE gunzip share.tar.gz
                 gunzip share.tar.gz
        $VERBOSE tar -x${vop}f share.tar
                 tar -x${vop}f share.tar
        $VERBOSE /bin/rm -f share.tar
                 /bin/rm -f share.tar
        $VERBOSE chmod -R g+w share
                 chmod -R g+w share
        touch gzip_share
        )

    elif [ -f $install_dir/share.tar ]
    then
        (
        echo "Unarchiving $install_dir/share.tar"
        cd $install_dir
        $VERBOSE tar -x${vop}f share.tar
                 tar -x${vop}f share.tar
        $VERBOSE /bin/rm -f share.tar
                 /bin/rm -f share.tar
        $VERBOSE chmod -R g+w share
                 chmod -R g+w share
        if [ "`uname -s`" = Linux ]
        then
            touch gzip_share
        else
            touch archive_share
        fi
        )
    fi
}

#------------------------------------------------------------------------
archive_share() {
#------------------------------------------------------------------------

    if [ -d $1 ]
    then
        install_dir=$1
    else
        install_dir=$2
    fi

    if [ -d $install_dir/share -a "$ARCHIVE" = yes ]
    then
        if [ "`uname -s`" = Linux ]
        then
            touch $install_dir/gzip_share
        else
            touch $install_dir/archive_share
        fi
    fi

    if [ -f $install_dir/archive_share ]
    then
        (
        echo "Archiving $install_dir/share"
        $VERBOSE cd $install_dir
                 cd $install_dir
        $VERBOSE tar -c${vop}f share.tar share
                 tar -c${vop}f share.tar share
        $VERBOSE compress share.tar
                 compress share.tar
        $VERBOSE '/bin/rm -fr share/* archive_share'
                  /bin/rm -fr share/* archive_share
        )

    elif [ -f $install_dir/gzip_share ]
    then
        (
        echo "Archiving $install_dir/share"
        $VERBOSE cd $install_dir
                 cd $install_dir
        $VERBOSE tar -c${vop}f share.tar share
                 tar -c${vop}f share.tar share
        $VERBOSE gzip share.tar
                 gzip share.tar
        $VERBOSE '/bin/rm -fr share/* gzip_share'
                  /bin/rm -fr share/* gzip_share
        )
    fi
}

#------------------------------------------------------------------------
set_tar() {
#------------------------------------------------------------------------

    test -n "$TAR" && return

    # Determine the tar command to use from the install (file0).
    # install directory in comp0 or file0.
    if [ -f file0 ]
    then
        if tar -tf file0 | grep /gtar >/dev/null
        then
            TAR=gtar
            return
        fi
    fi
    TAR=tar
}

#------------------------------------------------------------------------
extract_prod_desc() {
#------------------------------------------------------------------------

    prod_desc=prod_desc
    test -f $prod_desc && return
    if [ -d comp0 ]
    then
        for prod_desc in `find comp0 -name prod_desc -print`
        do
            /bin/cp -p $prod_desc .
            break
        done

    elif [ -f file0 ]
    then
        prod_desc=`tar -tf file0 | grep /prod_desc |tail -1`
        tar -xf file0 $prod_desc
        if [ -f $prod_desc ]
        then
            if /bin/cp -p $prod_desc prod_desc
            then
                /bin/rm -f $prod_desc
                pdir=`dirname $prod_desc`
                while rmdir $pdir 2>/dev/null
                do
                    pdir=`dirname $pdir`
                    test "$pdir" = '.' && break
                done
            fi
        else
            echo Failed to extract $prod_desc from file0.
            exit
        fi
    fi

    if [ -f prod_desc ]
    then
        prod_desc=prod_desc
        return 0
    else
        return 1
    fi
}

#------------------------------------------------------------------------
set_comp_uncomp() {
#------------------------------------------------------------------------

    test -n "$COMP"  -o  "$compress" = no && return

    extract_prod_desc

    # Determine the compression command to use from the
    # install directory in comp0 or file0.
    eval `grep compress= $prod_desc`
    if [ "$compress" = yes ]
    then
        COMP=compress
        UNCOMP=uncompress
    else
        # Assume gzip compression
        COMP=gzip
        UNCOMP=gunzip
    fi
}

#------------------------------------------------------------------------
install_aliases() {
#------------------------------------------------------------------------
# Install command aliases.

    DIR=`dirname $FILE`
    DIR=`(cd $DIR; pwd)`

    if [ ! -w $DIR ]
    then
        echo "Installation of command aliases cannot be done."
        echo "$DIR not writeable."
        exit 2
    fi

    cd $DIR
    n=0
    install_mesg="All aliases okay. Nothing to install."
    for cmnd in $alias_list
    do
        test "$cmnd" = "$CMD" && continue

        if [ -x $cmnd ]
        then
            /bin/ls -l $cmnd
        else
            n=`expr $n + 1`
            ln -s $CMD $cmnd
            /bin/ls -l $cmnd
            if [ $n -eq 1 ]
            then
                install_mesg="$n alias installed."
            else
                install_mesg="$n aliases installed."
            fi
        fi
    done
    echo "$CMD: $install_mesg"
}

#------------------------------------------------------------------------
# Parse command line args.
#------------------------------------------------------------------------
while [ $# -gt 0 ]
do
    case $1 in
    all )
        if [ -n "$N" ]
        then
            echo "$CMD: <N> already set to '$N'."
            Usage 1
        fi
        N=all
        ;;
    -a )
        ARCHIVE=yes
        ;;
    -d )
        RMF='/bin/rm -f'
        RMFR='/bin/rm -fr'
        ECHORM='echo'
        ;;
    -e )
        extract_prod_desc
        exit $?
        ;;
    -f )
        X=$2
        echo "Will un'tar $X."
        shift
        ;;
    -gt )
        TAR=gtar
        ;;
    -gz )
        COMP=gzip
        UNCOMP=gunzip
        ;;
    -h* )
        Usage 0
        ;;
    -i )
        install_aliases
        exit 0
        ;;
    -nc )
        compress=no
        COMP=
        UNCOMP=
        ;;
    -r )
        RECOVER=yes
        ;;
    -s )
        RUN=':'
        ;;
    -st )
        ST=$2
        if [ -z "$ST" ]
        then
            echo "$CMD: option '-st' requires an argument."
            Usage 1
        elif [ ! -d "$ST" ]
        then
            echo "$CMD: Storage directory, $ST, not found."
            Usage 2
        fi
        shift
        ;;
    -v )
        vop=v
        VERBOSE=echo
        ;;
    [0-9]* )
        if [ -n "$N" ]
        then
            LIST="$LIST $1"
            N=list
        else
            LIST=$1
            N=$1
        fi
        ;;
    * )
        if [ "$CMD" = findfile ]
        then
            N=$1
            file_list="$*"
            break
        fi
        ;;
    esac
    shift
done

if [ -z "$N" ]
then
    if [ "$CMD" = findfile ]
    then
        echo "$CMD: <file> not specified."
    else
        echo "$CMD: <N> not specified."
    fi
    Usage 1
fi

case $N in
list )
    ;;
[0-9]* )
    LIST=$N
    ;;
all )
    if [ $CMD = readfile ]
    then
        LIST=
        file0to9=`/bin/ls file? 2>/dev/null`
        file10andUp=`/bin/ls file?? 2>/dev/null`
        for file in $file0to9 $file10andUp
        do
            LIST="$LIST $file"
        done
        LIST=`echo "$LIST" | sed -e "s/file//g"`
    elif [ $CMD = writefile ]
    then
        LIST=
        comp0to9=`/bin/ls -d comp? 2>/dev/null | sed -e "s/comp//g"`
        comp10andUp=`/bin/ls -d comp?? 2>/dev/null | sed -e "s/comp//g"`
        for comp in $comp0to9 $comp10andUp
        do
            if [ "$RECOVER" = yes  -a  -f file$comp ]
            then
                echo "file$comp already written."
                continue
            fi
            LIST="$LIST $comp"
        done
    else
        echo "$CMD: 'all' not allowed for this command."
        Usage 1
    fi
    ;;
* )
    if [ "$CMD" != findfile ]
    then
        echo "$CMD: <N> must be a number."
        Usage 1
    fi
    ;;
esac

case `uname -s` in
HP-UX )
    x_opt="-x${vop}f"
    ;;
OSF1 )
    x_opt="-x${vop}Bf"
    ;;
* )
    x_opt="-x${vop}Bpf"
    ;;
esac

set_tar
set_comp_uncomp
/bin/rm -f CTinfo

case $CMD in
readfile|listfile )
    for N in $LIST
    do
        if [ ! -f file$N ]
        then
            echo file$N not found
            continue
        fi

        if [ $CMD = listfile ]
        then
            file=file$N
            tar_opt="-t${vop}f"
            RMF=':'
            UP=':'
            ECHORM=':'
        else
            if [ -d comp$N ]
            then
                echo cd comp$N
                cd comp$N
                echo Cleaning: *
                /bin/rm -fr *
            else
                echo mkdir comp$N
                mkdir comp$N
                echo cd comp$N
                cd comp$N
            fi
            UP='cd ..'
            file=../file$N
            tar_opt="$x_opt"
        fi

        if [ $N = 0 ]
        then
            echo "$TAR $tar_opt $file $X"
            $RUN $TAR $tar_opt $file $X
            status=$?
        elif [ -n "$UNCOMP" ]
        then
            echo "dd if=$file ibs=126b obs=2520b | $UNCOMP | $TAR $tar_opt - $X"
            $RUN dd if=$file ibs=126b obs=2520b | $UNCOMP | $TAR $tar_opt - $X
            status=$?
        else
            echo "dd if=$file ibs=126b obs=2520b | $TAR $tar_opt - $X"
            $RUN dd if=$file ibs=126b obs=2520b | $TAR $tar_opt - $X
            status=$?
        fi

        $UP

        if [ $status -eq 0 ]
        then
            $ECHORM $RMF file$N
            $RMF file$N
        fi

        test $N = 0 &&
            unarchive_share comp0/releases/*/install comp0/releases/*/*/install
    done
    ;;

writefile )
    for N in $LIST
    do
        if [ ! -d "comp$N" ]
        then
            echo "$CMD: Directory 'comp$N' not found."
            continue
        fi

        if [ -f file$N ]
        then
            echo /bin/rm -f file$N
            /bin/rm -f file$N
        fi

        echo cd comp$N
        cd comp$N
        dirs=`/bin/ls -d .[!.]* * 2>/dev/null`
        if [ -z "$dirs" ]
        then
            echo "$CMD: Directory 'comp$N' is empty."
            cd ..
            continue
        fi

        if [ $N = 0 ]
        then
            archive_share releases/*/install releases/*/*/install
            echo "$TAR -c${vop}f $ST/file$N $dirs"
            $RUN $TAR -c${vop}f $ST/file$N $dirs
            status=$?
        elif [ -n "$COMP" ]
        then
            echo "$TAR -c${vop}f -" $dirs "| $COMP -c |"
            echo "dd of=$ST/file$N ibs=2520b obs=126b"
            $RUN $TAR -c${vop}f - $dirs | $COMP -c | dd of=$ST/file$N ibs=2520b obs=126b
            status=$?
        else
            echo "$TAR -c${vop}f -" $dirs "|"
            echo "dd of=$ST/file$N ibs=2520b obs=126b"
            $RUN $TAR -c${vop}f - $dirs | dd of=$ST/file$N ibs=2520b obs=126b
            status=$?
        fi

        echo cd ..
        cd ..

        if [ "$status" -eq 0 ]
        then
            $ECHORM $RMFR comp$N
            $RMFR comp$N
        fi

        if [ "$ST" = '..' ]
        then
            echo chmod 444 file$N
            chmod 444 file$N
            /bin/ls -l file$N
        else
            echo chmod 444 $ST/file$N
            chmod 444 $ST/file$N
            /bin/ls -l $ST/file$N
        fi
    done
    ;;

findfile )
    for file in $file_list
    do
        echo "Searching for $file in comp dirs..."
        find comp* ! -type d -name $file -print 2>/dev/null
        echo "Searching in 'table' files..."
        find_table `find comp* -name table -print 2>/dev/null`
    done
    ;;
esac
