#!/bin/sh

die()
{
    echo $1
    exit 1
}


s_install()
{
    src_DIR=$1
    mode=$2
    dst_DIR=$RPM_BUILD_DIR/`echo $3 | sed 's/\///'`	

    [ -d $dst_DIR ] || mkdir -p $dst_DIR

    THISDIR=`pwd`
    cd $src_DIR
    for f in `ls -1`
    do
        if [ -f $f ]
	then
	   install -m $mode $f $dst_DIR/$f
	fi
    done
    cd $THISDIR
}


print_attr()
{
    dst_DIR=$1
    mode=$2
    t_dir=$RPM_BUILD_DIR/$dst_DIR

    THISDIR=`pwd`
    cd $t_dir
    for f in `ls -1`
    do
        [ -f $f ] && echo "%attr($mode, root, root) $dst_DIR/$f"
    done
    cd $THISDIR
}

make_targz()
{
    THISDIR=`pwd`
    src_dir=$1
    cd $RPM_BUILD_DIR/..
    tar cvf - $src_dir |\
	gzip > /usr/src/redhat/SOURCES/$src_dir.tar.gz
    cd $THISDIR
}

make_spec()
{
    name=$1
    cat <<-LEOF > $TOPDIR/dist/RPM/$name.spec

Summary: libwms - get and parse wms data
Name: $name
Version: $REV
Release: $REL
Copyright: LGPL
Group: Graphics
Source: $name-$REV.tar.gz
URL: http://www.flids.com
Packager: `awk -f - /etc/passwd <<-EOF
BEGIN { FS=":"}
{
    if( \\$1 == "$LOGNAME" )
    print \\$5
}
EOF`

%description


%prep

%setup

%build

%install

cd $RPM_BUILD_DIR 
tar cvf - . | tar xvfC - /

# ---------------------
# FILES Sections
%files

LEOF
}


make_wms()
{
    RPM_BUILD_DIR=/usr/src/redhat/BUILD/libwms-$REV
    rm -rf $RPM_BUILD_DIR
    mkdir $RPM_BUILD_DIR

    s_install $TOPDIR/lib 755 $INST_LIBS
    s_install $TOPDIR/bin 755 $INST_EXAMPLES

    make_targz -$REV
    make_spec libwms

    print_attr $INST_LIBS 755    >> $TOPDIR/dist/RPM/libwms.spec
    print_attr $INST_EXAMPLES 755   >> $TOPDIR/dist/RPM/libwms.spec

    rpmbuild -bb --clean $TOPDIR/dist/RPM/libwms.spec

    mv /usr/src/redhat/RPMS/i386/libwms-$REV-$REL.i386.rpm dist/RPM
}

make_wms_dev()
{
    RPM_BUILD_DIR=/usr/src/redhat/BUILD/libwms_dev-$REV
    rm -rf $RPM_BUILD_DIR
    mkdir $RPM_BUILD_DIR

    for d in `ls $TOPDIR/include | grep -v CVS`
    do
        [ -d $TOPDIR/include/$d ] && \
            s_install $TOPDIR/include/$d 644 $INST_INCLUDE/$d
    done


    for d in `ls -1 $INST_EXAMPLE_SRC`
    do
        if [ -d $INST_EXAMPLE_SRC/$d ] 
        then
           s_install $INST_EXAMPLE_SRC/$d 644 $INST_EXAMPLE_SRC/$d
        fi
        cd $THISDIR
    done

    s_install $INST_SRC/Make 644 $INST_SRC/Make


    make_targz libwms_dev-$REV
    make_spec libwms_dev

    for d in `ls -1 $RPM_BUILD_DIR/$INST_INCLUDE`
    do
        print_attr $INST_INCLUDE/$d 755  >> $TOPDIR/dist/RPM/libwms_dev.spec
    done

    for d in `ls -1 $RPM_BUILD_DIR/$INST_EXAMPLE_SRC`
    do
        print_attr $INST_EXAMPLE_SRC/$d 755  >> $TOPDIR/dist/RPM/libwms_dev.spec
    done

    print_attr $INST_SRC/Make 755 >>  $TOPDIR/dist/RPM/libwms_dev.spec

    rpm -ba --clean $TOPDIR/dist/RPM/libwms_dev.spec

    mv /usr/src/redhat/RPMS/i386/libwms_dev-$REV-$REL.i386.rpm dist/RPM
}

if [ "`whoami`" != "root" ] 
then
    echo
    echo  "    === makerpm : You must be root do make the RPM distribution."
    echo
    exit 1
fi

if [ $# -ne 9 ] 
then
    echo
    echo makerpm : Internal Error - Bad Usage
    echo 
    exit 1
fi

OS=$1
TOPDIR=$2
INST_LIBS=$3
INST_INCLUDE=$4
INST_EXAMPLES=$5
INST_SRC=$6
INST_EXAMPLE_SRC=$7
VERSION=$8
shift; shift;shift; shift;shift; shift;shift;shift;
INST_DOC=$1

REV=$VERSION
REL=1

make_wms

make_wms_dev



exit 0
