#!/bin/bash -e

# Script to build and install Coda client and/or server from source.
# Davor Ocelic, docelic@mail.inet.hr, Feb 2007.
#
# Coda, an Advanced Netowrked Filesystem, http://www.coda.cs.cmu.edu/
# Coda Wiki, http://coda.wikidev.net/
#
# Usage:
# sh ./coda_cvs.sh [nolwp] [norpc] [norvm] [nocoda] [server]
#
# So common invocation is (to build and install client):
#   sh ./coda_cvs.sh
#
# And to install server part after client is compiled and installed:
#   cd coda-cvs/coda && make server-install
#
# Or to build server part directly with the script:
#   sh ./coda_cvs.sh server
#

mkdir -p coda-cvs
cd coda-cvs/

PREFIX=/usr/local/coda

export CVSROOT=:pserver:anonymous@coda.cs.cmu.edu:/coda-src       

if echo "$*" | grep -v noup >/dev/null; then
        echo "UPDATING CVS MODULES"
        if [ -d lwp  ]; then lwp_action="up -d";  else lwp_action=co; fi
        if [ -d rpc2 ]; then rpc2_action="up -d"; else rpc2_action=co; fi
        if [ -d rvm  ]; then rvm_action="up -d";  else rvm_action=co; fi
        if [ -d coda ]; then coda_action="up -d"; else coda_action=co; fi
        cvs -z3 $lwp_action  -P lwp
        cvs -z3 $rpc2_action -P rpc2
        cvs -z3 $rvm_action  -P rvm
        cvs -z3 $coda_action -P coda
fi

if echo "$*" | grep -v nolwp >/dev/null; then
        echo "BUILDING LWP"
        cd lwp; sh bootstrap.sh; ./configure --prefix=$PREFIX \
                   && make && make install; cd ..
fi

if echo "$*" | grep -v rpc >/dev/null; then
        echo "BUILDING RPC2"
        cd rpc2; sh bootstrap.sh; ./configure --prefix=$PREFIX \
           --with-lwp-includes=$PREFIX/include \
           --with-lwp-library=$PREFIX/lib && make && make install; cd ..
fi

if echo "$*" | grep -v norvm >/dev/null; then
        echo "BUILDING RVM"
        cd rvm; sh bootstrap.sh; ./configure --prefix=$PREFIX \
           --with-lwp-includes=$PREFIX/include \
           --with-lwp-library=$PREFIX/lib && make && make install; cd ..
fi

if echo "$*" | grep -v nocoda >/dev/null; then
        type=client

        if echo "$*" | grep server >/dev/null; then
                type=server
        fi

        echo "BUILDING CODA (Will install files for client/server: $type)"

        cd coda; sh bootstrap.sh; ./configure --prefix=$PREFIX \
           --with-lwp=$PREFIX \
           --with-rpc2=$PREFIX \
           --with-rvm=$PREFIX && make && make ${type}-install; cd ..
fi

echo "ALL DONE. YOU ARE WONDERFUL!"

