#!/bin/sh
#
#    doctree -- create a copy of the postgres master documentation tree
#
# handle some command line arguments...

MASTER=/a/postgres/ctree/doc

cd $MASTER

debug=0
quiet=0
TREE=""
name=$0
create=0
usage="Usage: $name [-d] [-q] [-c] full_path_to_tree"

while (test ! -z "$1")
do
    case $1 in
    -d)
        debug=1
        shift ;;
    -c)
        create=1
        shift ;;
    -q)
        quiet=1
        shift;;
    -*)
        echo "$usage" >&2
        exit 1 ;;
    /*)
        TREE=$1
        shift ;;
    *)
        echo "$usage" >&2
        exit 1 ;;
    esac
done

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 doc tree "
    echo -n "(default /usr/postgres/doc): "
    read TREE

    if (test -z "$TREE")
    then
        TREE="/usr/postgres/doc"
    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 doctree creation"
        exit
    fi
    create=1
fi

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

# generate the new tree

cd $TREE
if (test -d $MASTER/RCS)
then
    if (test $quiet -eq 0)
    then
	echo ===== checking out files in $MASTER =====
    fi
    if (test $debug -eq 0)
    then
	ln -s $MASTER/RCS
	co -q RCS/*,v
    fi
fi

for i in `co -q -p $MASTER/dirs.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
            co -q RCS/*,v
        fi
    fi
done

echo ===== documentation tree completed =====
