#!/bin/sh
#
# Usage: build-krb [path=...] [hostname=...] [host=...] [cc=...]
#
# Figures out path to source from argv[0]
# Figures out hostname from uname
# Figures out host from uname and config.guess
# cc gets defaulted
#
# 
umask 022
progname=$0
path_value="`echo ${progname} | sed 's=/[^/]*$=='`"
hostname_value="`uname -n`"
case "${hostname_value}" in 
	"") hostname_value="`hostname`" ;;
esac
host_value="`${path_value}/config.guess`"
cc_value=gcc
#
# find a better make, if we can
#
if [ -x /usr/progressive/bin/make ]; then
	M=/usr/progressive/bin/make
elif [ -x /usr/latest/bin/make ]; then
	M=/usr/latest/bin/make
else
M=make
fi
M_label=
install=no
clean=no
keepold=no

for arg in $*;
do
	case "$arg" in
	hostname=*)
		hostname_value="`echo ${arg} | sed 's/^[-a-z]*=//'`"
		;;

	host=*)
		host_value="`echo ${arg} | sed 's/^[-a-z]*=//'`"
		;;

	make=*)
		M="`echo ${arg} | sed 's/^[-a-z]*=//'`"
		M_label="-`echo ${M} | sed -e 's/ //g' -e 's=/=-=g'`"
		;;

	cc=*)
		cc_value="`echo ${arg} | sed 's/^[-a-z]*=//'`"
		cc_label="`echo ${cc_value} | sed -e 's/ //g' -e 's=^/==' -e 's=/=-=g'`"
		;;

	--native)
		cc_value=cc
		cc_label="native-cc"
		;;

	--progressive)
		PATH=/usr/progressive/bin:$PATH
		export PATH
		cc_value=gcc
		cc_label="progressive-gcc"
		;;

	--latest)
		PATH=/usr/latest/bin:$PATH
		export PATH
		cc_value=gcc
		cc_label="latest-gcc"
		;;

	--install)
		install=yes
		;;

	--vault)
		vault=yes
		;;

	--clean)
		clean=yes
		;;

	--keepold)
		keepold=yes
		;;

	--bin-ld)
		cc_value="${cc_value} -B/bin/"
		cc_label="${cc_label}-bin-ld"
		;;

	--no-const)
		cc_value="${cc_value} -Dconst="
		cc_label="${cc_label}-no-const"
		;;

	--varargs)
		cc_value="${cc_value} -DVARARGS"
		cc_label="${cc_label}-varargs"
		;;

	--path=*)
		PATH="`echo ${arg} | sed 's/^[-a-z]*=//'`":$PATH
		export PATH
		;;

	# Sometimes the args after cc='gcc -B/bin/' don't get kept in
	# the same argument anyway...sigh.
	-*)
		cc_value="${cc_value} `echo ${arg} | sed 's/^[-a-z]*=//'`"
		cc_label="`echo ${cc_value} | sed -e 's/ //g' -e 's=/=-=g'`"
		;;

	*)
echo 'Usage: build-krb [path=...] [hostname=...] [host=...] [cc=...] [make=...]'
echo '       build-krb [--latest | --progressive | --native]'
		exit 1;
	;;
	esac
done

build_dir="${host_value}-${cc_label}${M_label}"
log_file=${build_dir}-log

date
echo "${hostname_value} ${build_dir}"

test -d ${build_dir} || mkdir ${build_dir}
case "${path_value}" in
   /*) configpath=${path_value} ;;
   *)  configpath=../${path_value} ;;
esac

# Change output to go to log file, saving real stdout on descriptor 3
exec 3>&1 >${log_file} 2>&1

failed=no
failed_check=no

date
echo hostname=${hostname_value}
echo hosttype=${host_value}
echo MAKE=$M
echo CC=${cc_value}
echo PATH=$PATH
echo output of ${cc_value} -v:
${cc_value} -v 2>&1
echo =====

cd ${build_dir}

if [ x$keepold = xno ]; then
  echo rm -rf *
  rm -rf *
fi

CC=${cc_value}
export CC
echo ${configpath}/configure -v --host="${host_value}" --srcdir="${configpath}"
${configpath}/configure -v --host="${host_value}" --srcdir="${configpath}"
if [ $? != 0 ]; then
  echo '***' configure failed
  failed=yes
fi

if [ x$failed = xno ]; then
  echo $M MAKE=$M CC="${cc_value}" depend
  $M MAKE=$M CC="${cc_value}" depend
  if [ $? != 0 ]; then
    echo '***' make depend failed
    failed=yes
  fi
fi

if [ x$failed = xno ]; then
  echo $M MAKE=$M CC="${cc_value}" all
  $M MAKE=$M CC="${cc_value}" all
  if [ $? != 0 ]; then
    echo '***' make all failed
    failed=yes
  fi
fi

if [ x$failed = xno ]; then
  echo $M MAKE=$M CC="${cc_value}" all
  $M MAKE=$M CC="${cc_value}" check
  if [ $? != 0 ]; then
    echo '***' make check failed
    failed_check=yes
  fi
fi

if [ x$install = xyes ]; then
  if [ x$failed = xno ]; then
    rm -f ../${host_value}.tar.Z
    echo $M MAKE=$M CC="${cc_value}" DESTDIR=`pwd`/DESTDIR install
    $M MAKE=$M CC="${cc_value}" DESTDIR=`pwd`/DESTDIR install
    if [ $? != 0 ]; then
      echo '***' make install failed
      failed=yes
    else
      $M MAKE=$M CC="${cc_value}" DESTDIR=`pwd`/DESTDIR GZIPPROG=compress GZIPEXT=Z dist
      if [ $? != 0 ]; then
	echo '***' could not build tar file
      else
	mv DESTDIR/kerberos.tar.Z ../${host_value}.tar.Z
      fi
    fi
  fi
fi

if [ x$vault = xyes ]; then
  if [ x$failed = xno ]; then
    VAULT=${VAULT-/progressive/release-vault/cns-95q1}
    vdir=${VAULT}/cns/${host_value}/${host_value}
    rm -rf ${vdir}
    if [ -d ${VAULT}/cns ]; then true; else mkdir ${VAULT}/cns; fi
    if [ -d ${VAULT}/cns/${host_value} ]; then true; else mkdir ${VAULT}/cns/${host_value}; fi
    if [ -d ${vdir} ]; then true; else mkdir ${vdir}; fi
    echo $M MAKE=$M CC="${cc_value}" DESTDIR=${vdir} install
    $M MAKE=$M CC="${cc_value}" DESTDIR=${vdir} install
    if [ $? != 0 ]; then
      echo '***' make install to vault failed
      failed=yes
    else
      ${vdir}/usr/kerberos/install/fixprot ${vdir} tar
      if [ $? != 0 ]; then
	echo '***' fixprot failed
      fi
    fi
  fi
fi

if [ x$clean = xyes ]; then
  if [ x$failed = xno ]; then
    if [ x$failed_check = xno ]; then
      echo $M MAKE=$M CC="${cc_value}" clean
      $M MAKE=$M CC="${cc_value}" clean
      if [ $? != 0 ]; then
	echo '***' make clean failed
	failed=yes
      fi
    fi
  fi
fi

date

if [ x$failed = xno ]; then
  if [ x$failed_check = xno ]; then
    echo 1>&3 succeeded
  else
    echo 1>&3 '***' built but failed check
  fi
else
  echo 1>&3 '***' failed
  exit 1
fi
