#!/bin/sh

# Copyright (C) 2005  Joshua Hoblitt
#
# $Id: modules-delta,v 1.2 2005-09-20 21:43:56 jhoblitt Exp $

MODULE=psmodule
DOC=ModulesSDRS.tex
UTIL_MODULE=utils
ROOTDIR=$HOME/tmp/$0
WORKDIR=$ROOTDIR/work
UTILDIR=$ROOTDIR/$UTIL_MODULE
TEXBLOCK=$UTILDIR/texblock.pl
APIDELTA=$UTILDIR/apidelta.pl
CVS=/usr/bin/cvs
SSH=/usr/bin/ssh

CVS_RSH=$SSH
CVSROOT=cvs.pan-starrs.ifa.hawaii.edu:/cvsroot/pan-starrs
export CVS_RSH CVSROOT

PERL5LIB=$UTILDIR
export PERL5LIB

STARTDIR=`pwd`

usage()
{
    cat <<EOF
Usage: $0 [--color={none,auto,always}]
EOF
}

if test $# -gt 1; then
    usage
    exit 1
fi

# checkout pslib sources

CVS_ERROR=0

if [ ! -d "$ROOTDIR/$MODULE" ] ; then
    mkdir -p $ROOTDIR
    cd $ROOTDIR
    $CVS co $MODULE > /dev/null 2>&1 || CVS_ERROR=1
else
    cd "$ROOTDIR/$MODULE"
    $CVS up -dP > /dev/null 2>&1 || CVS_ERROR=1
fi

if [ $CVS_ERROR -eq 1 ] ; then
    echo "CVS error"
    exit 1;
fi

# checkout utils

CVS_ERROR=0

if [ ! -d "$ROOTDIR/$UTIL_MODULE" ] ; then
    mkdir -p $ROOTDIR
    cd $ROOTDIR
    $CVS co $UTIL_MODULE > /dev/null 2>&1 || CVS_ERROR=1
else
    cd "$ROOTDIR/$UTIL_MODULE"
    $CVS up -dP > /dev/null 2>&1 || CVS_ERROR=1
fi

if [ $CVS_ERROR -eq 1 ] ; then
    echo "CVS error from utils"
    exit 1;
fi

# compare sources

if [ ! -d "$WORKDIR" ] ; then
    mkdir -p $WORKDIR
fi

cd $STARTDIR

find $ROOTDIR/$MODULE/src -name "*.h" -exec cat {} \; > $WORKDIR/code.txt
$TEXBLOCK -b prototype -i $DOC > $WORKDIR/spec.txt
$TEXBLOCK -b datatype -i $DOC >> $WORKDIR/spec.txt
$APIDELTA $1 --spec $WORKDIR/spec.txt --code $WORKDIR/code.txt

# cleanup

#if [ -d "$WORKDIR" ] ; then
#    rm -rf $WORKDIR
#fi
