#!/bin/sh
# build-dce
#
# Author: Marc Unangst
#
# $Id: build-dce,v 1.1.1.1 2005/03/21 08:52:03 agc Exp $
#
# build-dce: unpack, patch, and compile the DCE sources.  This is
# necessary because TOG's "free" source license for DCE 1.2.x does
# not allow redistribution of modified source trees or binaries
# without a fee.
#
#
# Copyright (c) of Carnegie Mellon University, 1998,1999.
#
# Permission to reproduce, use, and prepare derivative works of
# this software for internal use is granted provided the copyright
# and "No Warranty" statements are included with all reproductions
# and derivative works. This software may also be redistributed
# without charge provided that the copyright and "No Warranty"
# statements are included in all redistributions.
#
# NO WARRANTY. THIS SOFTWARE IS FURNISHED ON AN "AS IS" BASIS.
# CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER
# EXPRESSED OR IMPLIED AS TO THE MATTER INCLUDING, BUT NOT LIMITED
# TO: WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY
# OF RESULTS OR RESULTS OBTAINED FROM USE OF THIS SOFTWARE. CARNEGIE
# MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT
# TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.
#

# program version
version="1.0"
# directories to install ODE & DCE binaries into
# [note that if you change these, there are many places in the DCE source
# tree that must be changed as well]
ode_target=/opt/ode
dce_target=/opt/dce

if [ $# -lt 1 ]; then
	echo "usage: build-dce distdir [masterdir]"
	exit 1
fi

startdir=`pwd`

if [ $# -lt 2 ]; then
	masterdir=`pwd`
else
	cd $2
	masterdir=`pwd`
	cd $startdir
fi

cd $1
distdir=`pwd`
backing=$masterdir/backing
build=$masterdir/build

diff_files="dce.diffs.006.gz ode.diffs.003.gz pdl.diffs.001.gz"
ode_files="ode/ode.tar.gz"
dce_files="src/export.tar.gz src/install.tar.gz src/logs.tar.gz \
	src/obj.tar.gz src/rc_files.tar.gz src/tools.tar.gz"
dce_src_files="src/src/SVR4.tar.gz src/src/admin.tar.gz \
	src/src/config.tar.gz src/src/dce.tar.gz src/src/directory.tar.gz \
	src/src/examples.tar.gz src/src/file.tar.gz src/src/lbe.tar.gz \
	src/src/libdce.tar.gz src/src/libdcedfs.tar.gz \
	src/src/mit_krb5.tar.gz src/src/rc_files.tar.gz src/src/rpc.tar.gz \
	src/src/security.tar.gz src/src/threads.tar.gz src/src/time.tar.gz \
	src/src/tools.tar.gz"
crypto_files="src.crypto/src.tar.gz"

echo "PDL Linux FreeDCE build script v$version"

echo "Checking to make sure you have all the parts..."
cd $distdir
for file in $diff_files $ode_files $dce_src_files $crypto_files; do
	if [ ! -f $file ]; then
		echo "Missing: $file"
		missing=true
	fi
done
if [ -n "$missing" ]; then
	echo "You appear to be missing one or more files.  Check your download."
	exit 1
fi

mkdir -p $backing/ode/src
mkdir -p $backing/dce/src

echo "Checking disk space..."
freespace=`df $backing | sed 1d | awk '{print $4}'`
if [ $freespace -lt 480000 ]; then
	freemb=`expr $freespace / 1024`
	echo "You will need approximately 480MB of free space on the partition that holds"
	echo "$backing in order to build DCE.  You appear to only"
	echo "have ${freemb}MB available.  You need to free up some space, or"
	echo "specify a different location for masterdir."
	exit 1
fi

echo "Unpacking DCE from $distdir into $backing."

echo "unpacking ODE sources:"
cd $backing
echo "	$distdir/ode/ode.tar.gz"
tar -xzf $distdir/ode/ode.tar.gz
echo "done."

echo "unpacking DCE sources:"
cd dce/src
for file in $distdir/src/src/*.tar.gz; do
	echo "	$file"
	tar -xzf $file
done
echo "done."

# Note that the diffs must be applied in this order (unpack virgin DCE,
# apply BU diffs, unpack crypto, apply PDL diffs).
echo "Applying BU ODE diffs"
cd $backing
gunzip -c $distdir/ode.diffs.003.gz | patch -p0

echo "Applying BU DCE diffs"
gunzip -c $distdir/dce.diffs.006.gz | patch -p0

echo "Unpacking crypto source"
cd $backing/dce
tar -xzUf $distdir/src.crypto/src.tar.gz

cd $masterdir
echo "Applying PDL DCE diffs"
gunzip -c $distdir/pdl.diffs.001.gz | patch -p0

echo "Building ODE"
# setup env. variables
export DEFTOOLBASE=$build/dce/tools/x86_linux/bin/
export NLSPATH=""
export context=x86_linux
mkdir -p $build

# setup sandboxrc
cd $backing/ode
mv .sandboxrc .sandboxrc.orig
sed 's,^base.*,base * '$backing',' .sandboxrc.orig >.sandboxrc

# do bootstrap ODE tools build
cd $backing/ode/src
sh -x ode/setup/setup.sh x86_linux

# using those tools, build the rest of ODE
export PATH=$backing/ode/tools/x86_linux/bin:$PATH
build -rc $backing/ode/.sandboxrc

echo "Installing ODE"

# install ODE (need to do as root)
mkdir -p $ode_target
sh -x ode/setup/install.sh x86_linux $ode_target

echo "Building DCE"

# make sandboxrc for DCE build; note that this 
if [ -f $HOME/.sandboxrc ]; then
    ext=$$
    echo "*** Warning: you already have a ~/.sandboxrc file!"
    echo "    I need to make my own, so I'll rename yours to ~/.sandboxrc.$ext"
    mv -f $HOME/.sandboxrc $HOME/.sandboxrc.$ext
fi
export PATH=$ode_target/bin:$PATH
mksb -back $backing/dce -dir $build -m x86_linux -tools b -obj b / -src b / dce

# compile DCE
cd $build/dce/src
build setup_all
build export_all
export LD_LIBRARY_PATH=$build/dce/export/x86_linux/usr/lib:$LD_LIBRARY_PATH
build comp_all

echo "Installing DCE"

# install DCE (need to do as root)
mkdir -p /opt/dce
build -rc $HOME/.sandboxrc TOSTAGE=$dce_target install_all

echo "Done."
