#!/bin/sh
#
#    newtree -- create a copy of the postgres master tree
#
# handle some command line arguments...
#
# - changed -d (debug) to -D so this script now supports
#   checking out a tree as of a given date.  For example,
#
# 	newtree -d28-April-1991,00:00-PST
#
#   will check out a tree consisting of all the revisions
#   of all the files valid at midnight of April 28, 1991.
#   -cim 5/2/91
#
# set -x

MASTER=/a/postgres/ctree/
if test `hostname` = "hermes.Berkeley.EDU"
then
PARALLELNEWTREE=t
fi

cd $MASTER

debug=0
quiet=0
BUILDOBJ=1
TREE=""
PORT=""
OD=""
dateopt=
name=$0
create=0
usage="Usage: $name [-q -c -noobj] [-p port] [-o obj_dir] full_path_to_tree"

while (test ! -z "$1")
do
    case $1 in
    -D)
        debug=1
        shift ;;
    -d*)
        dateopt=$1
        shift ;;
    -c)
        create=1
        shift ;;
    -p) 
        PORT=$2
        cd $MASTER/src/port
        if (test ! -d $PORT) 
        then
            echo "$name: port $PORT does not exist - exiting..." >&2
            exit 1
        fi
        if (test -z "$OD")
        then
            OD=obj.$port
        fi
        shift; shift ;;
    -q)
        quiet=1
        shift;;
    -noobj)
        BUILDOBJ=0
        shift ;;
    -o)
        OD=$2
        shift; shift ;;
    -parallel)
	PARALLELNEWTREE=t
	shift;;
    -*)
        echo "$usage" >&2
        exit 1 ;;
    /*)
        TREE=$1
        shift ;;
    *)
        echo "$usage" >&2
        exit 1 ;;
    esac
done
cocmd="co "$dateopt

if (test ! -z "$TREE")
then
    if (test $quiet -eq 0)
    then
        echo "using directory $TREE"
    fi
fi

if (test -z "$TREE")
then
    echo ""
    echo -n "Full pathname of directory to contain new tree "
    echo -n "(default /usr/postgres): "
    read TREE

    if (test -z "$TREE")
    then
        TREE="/usr/postgres"
    fi
fi

if (test ! -d $TREE -a $create -eq 0)
then
    echo ""
    echo -n "Directory $TREE does not exist; create (y/n)? "
    read yn
    if (test $yn != y)
    then
        echo "aborting newtree creation"
        exit
    fi
    create=1
fi

if (test $create -eq 1)
then
    TREEPATH=`echo $TREE | sed 's|/| |g' `
    cd /
    for i in $TREEPATH
    do
        if (test ! -d $i)
        then
            if (test $quiet -eq 0)
	    then
		echo ===== creating `pwd`/$i =====
	    fi
            mkdir $i
        fi
        cd $i
    done
fi

if (test $BUILDOBJ -eq 1) then
    if (test -z "$PORT")
    then
        echo ""
        cd $MASTER/src/port
        echo "System-specific ports available are: "
        echo "" ; echo "    " * ; echo ""
        echo -n "Please choose a port (default is dec): "
        read dir
        if (test ! -z "$dir")
        then
            if (test ! -d $dir )
            then
                echo "Port \"$d\" does not exist"
            fi
            PORT=$dir
        else
            PORT=dec
        fi
    fi

    if (test -z "$OD")
    then
        OD=obj.$PORT
        echo ""
        echo "Please enter the object directory name.  To accept the default"
        echo -n "of $OD, hit return: "
        read od
        if (test ! -z "$od")
        then
            OD=$od
        fi
    fi
fi

# generate the new tree

cd $MASTER/newconf

if (test ! -d $TREE/src )
then
    if (test $quiet -eq 0)
    then
        echo ""
        echo ===== creating new source tree =====
    fi
    mkdir $TREE/src
fi

#
# Make src dirs.
#
for i in `$cocmd -q -p $MASTER/newconf/dirs.mk`
do
    mkdir $TREE/src/$i
    if (test -d $MASTER/src/$i/RCS)
    then
        cd $TREE/src/$i
        if (test $quiet -eq 0)
        then
            echo ===== checking out files in $i =====
        fi
        if (test $debug -eq 0)
        then
            ln -s $MASTER/src/$i/RCS
	    if test $PARALLELNEWTREE
	    then
		$cocmd -q RCS/*,v &
	    else
                $cocmd -q RCS/*,v
	    fi
        fi
    fi
done

#
# Make "other" dirs
#

if (test $quiet -eq 0) 
then
    echo ===== checking out other directories =====
fi

for i in `$cocmd -q -p $MASTER/newconf/dirs_other.mk`
do
    mkdir $TREE/$i
    if (test  -d $MASTER/$i/RCS )
    then
        cd $TREE/$i
        if (test $quiet -eq 0) 
        then
            echo ===== checking out files in $i =====
        fi
        if (test $debug -eq 0)
        then
            ln -s $MASTER/$i/RCS
	    if test $PARALLELNEWTREE
	    then
		$cocmd -q RCS/*,v &
	    else
                $cocmd -q RCS/*,v
	    fi
        fi
    fi
done

if test $PARALLELNEWTREE
then
wait
fi

if (test $BUILDOBJ -eq 1)
then

    if (test $quiet -eq 0)
    then
        echo ===== building object tree =====
    fi
    cd $TREE/newconf
    if (test $debug -eq 1) 
    then
        echo "debug  - not running newport"
        exit
    fi
    if (test $quiet -eq 0)
    then
        csh ./newport $TREE $PORT $OD
    else
        csh ./newport -q $TREE $PORT $OD
    fi
fi
