#!/bin/ksh
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
#

# Use distributed make (dmake) by default.
make=${MAKE:-dmake}

CLOSED_IS_PRESENT=no
export CLOSED_IS_PRESENT

# Do this if you want to use dbx or gdb
# export SOURCEDEBUG=yes

[ -n "$SRC" ] || {
  echo "SRC not set.  Run 'ws' or 'bldenv' first."
  exit 1
}

cpu=`uname -p`
case $cpu in
i386)
	x=intel
	mdb_arch="ia32 amd64"
	arch64=amd64
	;;
sparc)
	x=sparc
	mdb_arch=v9
	arch64=sparcv9
	;;
*)  echo "Huh?" ; exit 1;;
esac

################################################################

build_tools() {
  test -f $SRC/tools/proto/root_i386-nd/opt/onbld/bin/genoffsets ||
    (cd $SRC/tools && $make install)
  (cd $SRC/common/mapfiles; $make install)
}

clobber_tools() {
  (cd $SRC/tools && $make clobber)
  (cd $SRC/common/mapfiles; $make clobber)
}

################################################################

do_hdrs() {

targ=$1
if [ "$targ" = clobber ]
then
  (cd $SRC/uts && $make -k clobber_h)
  (cd $SRC/head && $make clobber)
fi

if [ "$targ" = install ]
then
  targ=install_h

  # Just the parts of "make sgs" we need, and
  # skip them if they appear to be done.
  # ... stuff under $SRC
  test -f $SRC/uts/common/sys/priv_names.h ||
    (cd $SRC/uts && $make -k all_h)

  test -f $SRC/head/rpcsvc/nispasswd.h ||
    (cd $SRC/head && $make -k install_h)

  # ... stuff under $ROOT (proto area)
  test -d $ROOT/usr/include/sys ||
    (cd $SRC && $make rootdirs)
  test -f $ROOT/usr/include/sys/types.h ||
    (cd $SRC/uts && $make -k install_h)
  test -f $ROOT/usr/include/rpcsvc/daemon_utils.h ||
    (cd $SRC/head && $make install_h)

  # system and smbsrv headers (we need to build libsmb)
  (cd $SRC/uts/common/sys && $make -k install_h)
  (cd $SRC/uts/common/smb && $make -k install_h)
  (cd $SRC/uts/common/smbsrv && $make -k install_h)

fi

# Need some library headers too...
for lib in \
  libads \
  libbsm \
  libcmdutils \
  libcryptoutil \
  libdevid \
  libgss \
  libkrb5 \
  libldap5 \
  libidmap \
  libsmbfs \
  libsqlite \
  libuutil
do
  (cd $SRC/lib/$lib && $make $targ)
done
}

################################################################

do_kern() {
  case $1 in
  lint) targ=modlintlib ;;
  *) targ=$1 ;;
  esac
  ( unset SOURCEDEBUG ;
  (cd $SRC/uts/$x/idmap && $make $targ) )
}

################################################################

do_libs() {

for lib in \
  libavl \
  libcmdutils \
  libldap5 \
  libadutils \
  libuutil \
  libidmap \
  libads \
  libsmbfs \
  libsqlite \
  nsswitch/ad
do
  (cd $SRC/lib/$lib && $make $1)
done

# need libsmb for cmd/idmap/idmapd (and libsmbfs for libsmb)
(cd $SRC/lib/smbsrv/libsmb && $make $1)

}

################################################################

do_cmds() {

(cd $SRC/cmd/idmap && $make $1)

}


################################################################
# This builds $SRC/TAGS (and cscope.files) in a helpful order.

do_tags() {
	(cd $SRC ;
	find uts/common/sys -name '*.[ch]' -print |sort
	find uts/common/net -name '*.[ch]' -print |sort
	find uts/common/netinet -name '*.[ch]' -print |sort
	find uts/common/rpcsvc -name '*.[ch]' -print |sort
	find uts/common/idmap -name '*.[ch]' -print |sort
	find head -name '*.h' -print |sort
	find lib/libidmap -name '*.[ch]' -print |sort
	find lib/libadutils -name '*.[ch]' -print |sort
	find lib/libldap5 -name '*.[ch]' -print |sort
	find cmd/idmap -name '*.[ch]' -print |sort
	) > $SRC/cscope.files

	(cd $SRC ;
	exctags -e --langmap=c:+.ndl -h ndl -L - < cscope.files
	cscope -b )
}

################################################################
# This creates a tarfile one can use to update a test machine.

do_tar() {
	git_rev=`git rev-parse --short=8 HEAD`
	files="
lib/svc/manifest/system/idmap.xml
usr/lib/idmapd
usr/lib/libads.so.1
usr/lib/$arch64/libads.so.1
usr/lib/libadutils.so.1
usr/lib/$arch64/libadutils.so.1
usr/lib/libidmap.so.1
usr/lib/$arch64/libidmap.so.1
usr/lib/libldap.so.5
usr/lib/$arch64/libldap.so.5
usr/lib/nss_ad.so.1
usr/lib/$arch64/nss_ad.so.1
usr/bin/test-getdc
usr/sbin/idmap
usr/sbin/nltest
"

	(cd $ROOT && tar cfj ../../idmap-${git_rev}.tar.bz2 $files)
}

################################################################

if [ "$1" = "" ]; then
  set '?' # force usage
fi

set -x

for arg
do
  case "$arg" in
  build|install)
    arg=install
    build_tools
    set -e
    do_hdrs $arg
    do_kern $arg
    do_libs $arg
    do_cmds $arg
    ;;
  lint)
    do_kern $arg
    do_libs $arg
    do_cmds $arg
    ;;
  clean)
    do_cmds $arg
    do_libs $arg
    do_kern $arg
    ;;
  clobber)
    do_cmds $arg
    do_libs $arg
    do_kern $arg
    do_hdrs $arg
    clobber_tools
    ;;
  tags)
    do_tags
    ;;
  tar)
    do_tar
    ;;
  *)
    echo "Usage: $0 {build|lint|clean|clobber|tags|tar}";
    exit 1;
    ;;
  esac
done