:
# NAME:
#	install-mk - install mk files
#
# SYNOPSIS:
#	install-mk [options] [var=val] [dest]
#
# DESCRIPTION:
#	This tool installs mk files in a semi-intelligent manner into
#	"dest".
#
#	Options:
#
#	-n	just say what we want to do, but don't touch anything.
#
#	-f	use -f when copying sys,mk.
#
#	-v	be verbose
#
#	-q	be quiet
#
#	-m "mode"
#		Use "mode" for installed files (444).
#
#	-o "owner"
#		Use "owner" for installed files.
#
#	-g "group"
#		Use "group" for installed files.
#
#	var=val
#		Set "var" to "val".  See below.
#
#	All our *.mk files are copied to "dest" with appropriate
#	ownership and permissions.
#	
#	By default if a sys.mk can be found in a standard location
#	(that bmake will find) then no sys.mk will be put in "dest".
#
#	FORCE_SYS_MK:
#		Can be set to 'cp' 'ln' or 'Generic'.  The first two
#		are only meaningful if a standard sys.mk can be found
#		and affect how it gets to "dest".
#
#		If set to 'Generic' though we will force "dest"/sys.mk
#		to be a symlink to 'Generic.sys.mk' (see below).
#
#	FORCE_BSD_MK:
#		By default if bsd.*.mk exist in the standard location,
#		they are used there and not provided in "dest".
#		If set to 'cp' or 'ln' the standard bsd.*.mk are
#		replicated to "dest".  If set to any other non-empty
#		value, or if no bsd.*.mk exist in a standard location
#		the generic prog.mk et al are symlinked to bsd.*.mk
#
#	USE_SYS_MK:
#		Set to one of the available *.sys.mk (without
#		the .sys.mk bit) selects that for linking as
#		"dest"/sys.mk if it does not exist.
#
#	Generic.sys.mk:
#
#	This as the name suggests is a generic sys.mk, it includes
#	host-target.mk and then attempts to find a suitable *.sys.mk
#	to include.  If it cannot find one, it uses some modest
#	defaults.
#
# AUTHOR:
#       Simon J. Gerraty <sjg@crufty.net>

# RCSid:
#	$Id: install-mk,v 1.20 2005/04/05 22:09:39 sjg Exp $
#
#	@(#) Copyright (c) 1994 Simon J. Gerraty
#
#	This file is provided in the hope that it will
#	be of use.  There is absolutely NO WARRANTY.
#	Permission to copy, redistribute or otherwise
#	use this file is hereby granted provided that 
#	the above copyright notice and this notice are
#	left intact. 
#      
#	Please send copies of changes and bug-fixes to:
#	sjg@crufty.net
#

MK_VERSION=20050405
OWNER=
GROUP=
MODE=444
ECHO=:
SKIP=
cp_f=-f

while :
do
	case "$1" in
	*=*) eval "$1"; shift;;
	+f) cp_f=; shift;;
	-f) cp_f=-f; shift;;
	-m) MODE=$2; shift 2;;
	-o) OWNER=$2; shift 2;;
	-g) GROUP=$2; shift 2;;
	-v) ECHO=echo; shift;;
	-q) ECHO=:; shift;;
	-n) ECHO=echo SKIP=:; shift;;
	--) shift; break;;
	*) break;;
	esac
done

case $# in
0)	echo "$0 [options] <destination> [<os>]"
	echo "eg."
	echo "$0 -o bin -g bin -m 444 /usr/local/share/mk Solaris"
	exit 1
	;;
esac
dest=$1
os=${2:-`uname`}
osrel=${3:-`uname -r`}

Do() {
	$ECHO "$@"
	$SKIP "$@"
}

[ -d $dest ] || Do mkdir -p $dest
[ -d $dest ] || Do mkdir $dest || exit 1
dest=`cd $dest && /bin/pwd`

cd `dirname $0`
mksrc=`/bin/pwd`
if [ $mksrc = $dest ]; then
	SKIP_MKFILES=:
else
	mk_files=`grep '^[a-z].*\.mk' FILES`
	sys_mk_files=`grep '^[A-Z].*\.sys\.mk' FILES`
	SKIP_MKFILES=
fi
$SKIP_MKFILES Do cp $cp_f $mk_files $sys_mk_files $dest
# if this is a BSD system we don't want to touch sys.mk
# setting FORCE_SYS_MK allows us to force install a sys.mk
SYS_MK_DIR=${SYS_MK_DIR:-/usr/share/mk}
SYS_MK=${SYS_MK:-$SYS_MK_DIR/sys.mk}
case "$FORCE_SYS_MK" in
Generic) USE_SYS_MK=$FORCE_SYS_MK;;
esac
# if USE_SYS_MK is Generic, then we will link that to sys.mk
use_sys=$USE_SYS_MK

if [ -s $SYS_MK ]; then
        # A BSD system, we should be using $SYS_MK and friends
        # but perhaps we want to capture a copy...
	case "$FORCE_SYS_MK" in
	cp) Do cp $cp_f $SYS_MK $dest/sys.mk;;
	ln) Do ln -s $SYS_MK $dest/sys.mk;;
	esac
	
	for f in $SYS_MK_DIR/bsd.*.mk
	do
		case "$FORCE_BSD_MK" in
		cp) Do cp $f $dest;;
		ln) Do ln -s $f $dest;;
		esac
	done
	case "$FORCE_BSD_MK" in
	cp)
		[ "$GROUP" ] && Do chgrp $GROUP $dest/bsd.*.mk
		[ "$OWNER" ] && Do chown $OWNER $dest/bsd.*.mk
		;;
	esac
else
	# This might be SunOS.5.7 which will end up picking
	# SunOS.5.sys.mk
	use_sys=${USE_SYS_MK:-$os.$osrel}
	while :
	do
		if [ -s $use_sys.sys.mk ]; then
                        break
		fi
		case "$use_sys" in
		*.*)	use_sys=`expr $use_sys : '\(.*\)\.[^\.]*$'`;;
		*)	echo "Using Generic sys.mk you may need to edit it"
			use_sys=Generic
			;;
		esac
	done
	if [ ! -s $use_sys.sys.mk ]; then
		echo "Pick, or create a suitable sys.mk to install in $dest. We have:"
		ls -1 *.sys.mk
		exit 1
	fi
fi
case "$FORCE_SYS_MK" in
Generic) Do rm -f $dest/sys.mk;;
esac
if [ ! -s $dest/sys.mk -a -s $use_sys.sys.mk ]; then
        Do ln -s $use_sys.sys.mk $dest/sys.mk
fi
$SKIP cd $dest
$SKIP_MKFILES Do chmod $MODE $mk_files
[ "$GROUP" ] && $SKIP_MKFILES Do chgrp $GROUP $mk_files $sys_mk_files
[ "$OWNER" ] && $SKIP_MKFILES Do chown $OWNER $mk_files $sys_mk_files
# if this is a BSD system the bsd.*.mk should exist and be used.
if [ ! -f $FORCE_BSD_MK$SYS_MK_DIR/bsd.prog.mk ]; then
	for f in dep doc init lib man nls obj own prog subdir
	do
		b=bsd.$f.mk
		[ -s $b ] || Do ln -s $f.mk $b
	done
fi
exit 0
