#!/bin/sh
# 
# @OSF_COPYRIGHT@
# 
#
# HISTORY
# $Log: mig.sh,v $
# Revision 1.3.19.3  1995/08/21  20:58:53  devrcs
# 	Add -split option.
# 	[1994/02/14  16:37:19  dnoveck]
#
# Revision 1.3.19.2  1994/09/23  06:00:07  ezf
# 	change marker to not FREE
# 	[1994/09/23  05:25:54  ezf]
# 
# Revision 1.3.19.1  1994/09/14  22:50:52  dwm
# 	mk6 CR541 - add xX flags to accepted args
# 	[1994/09/14  22:50:36  dwm]
# 
# Revision 1.3.15.1  1994/05/03  21:33:04  condict
# 	# Ported the -maxonstack feature from NMK17.1.
# 	[1994/05/03  21:00:20  condict]
# 
# Revision 1.3.11.1  1994/02/08  11:03:58  bernadat
# 	Added support for maxonstack option.
# 	From Joseph Barrera (jsb) at Carnegie-Mellon University.
# 	[93/07/09            bernadat]
# 	[94/02/04            bernadat]
# 
# Revision 1.3.4.3  1993/07/09  15:58:47  jeffc
# 	CR9241: use unique names for temp files.
# 	[93/07/01            barbou]
# 
# Revision 1.3.4.2  1993/06/09  02:59:44  gm
# 	Generate by default code with prototypes and ANSI C
# 	(without __STDC__ conditionals). Establish the -K option to
# 	generate code in the old way (default K&R, optionally ANSI).  CR #9060.
# 	[1993/05/14  12:33:55  travos]
# 
# 	Changed mig to use cc -E instead of cpp.
# 	[1993/05/18  15:35:12  gm]
# 
# 	Updated for new mk5.1 source organization.
# 	[1993/05/04  04:32:17  gm]
# 
# Revision 1.3  1993/04/19  17:31:45  devrcs
# 	Merge untyped ipc:
# 	Add untyped migcom flags.				[travos@osf.org]
# 	Drive both typed and untyped migcom.			[gm@osf.org]
# 	[1993/03/25  08:53:27  rod]
# 
# Revision 1.2  1992/05/12  14:36:22  devrcs
# 	Created for OSF/1 MK
# 	[1992/05/04  06:05:03  condict]
# 
# Revision 2.8  92/01/23  15:21:28  rpd
# 	Changed to generate a dependency on migcom itself.
# 	[92/01/19            rpd]
# 
# Revision 2.7  92/01/14  16:46:20  rpd
# 	Removed -theader switched.
# 	Fixed dependency file generation to remove /dev/null.
# 	[92/01/10            rpd]
# 
# Revision 2.6  91/07/31  18:09:45  dbg
# 	Allow both -header and -sheader switches.
# 	Fix copyright.
# 	[91/07/30  17:14:28  dbg]
# 
# Revision 2.5  91/06/25  10:31:42  rpd
# 	Added -sheader.
# 	[91/05/23            rpd]
# 
# Revision 2.4  91/02/05  17:55:08  mrt
# 	Changed to new Mach copyright
# 	[91/02/01  17:54:47  mrt]
# 
# Revision 2.3  90/06/19  23:01:10  rpd
# 	The -i option takes an argument now.
# 	[90/06/03            rpd]
# 
# Revision 2.2  90/06/02  15:05:05  rpd
# 	For BobLand: changed /usr/cs/bin/wh to wh.
# 	[90/06/02            rpd]
# 
# 	Created for new IPC.
# 	[90/03/26  21:12:04  rpd]
# 
# $EndLog$
#	Created.
#
#
# Mach Operating System
# Copyright (c) 1991,1990 Carnegie Mellon University
# All Rights Reserved.
# 
# Permission to use, copy, modify and distribute this software and its
# documentation is hereby granted, provided that both the copyright
# notice and this permission notice appear in all copies of the
# software, derivative works or modified versions, and any portions
# thereof, and that both notices appear in supporting documentation.
# 
# CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
# CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
# ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
# 
# Carnegie Mellon requests users of this software to return to
# 
#  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
#  School of Computer Science
#  Carnegie Mellon University
#  Pittsburgh PA 15213-3890
# 
# any improvements or extensions that they make and grant Carnegie Mellon
# the rights to redistribute these changes.
#

C=${MIGCC-cc}
M=${MIGCOM-/usr/ccs/lib/migcom}

cppflags=
migflags=
files=

# If an argument to this shell script contains whitespace,
# then we will screw up.  migcom will see it as multiple arguments.
#
# As a special hack, if -i is specified first we don't pass -user to migcom.
# We do use the -user argument for the dependencies.
# In this case, the -user argument can have whitespace.

until [ $# -eq 0 ]
do
    case "$1" in
	-[dtqkKQvVtTrRsSlLxX] ) migflags="$migflags $1"; shift;;
	-i	) sawI=1; migflags="$migflags $1 $2"; shift; shift;;
	-user   ) user="$2"; if [ ! "${sawI-}" ]; then migflags="$migflags $1 $2"; fi; shift; shift;;
	-server ) server="$2"; migflags="$migflags $1 $2"; shift; shift;;
	-header ) header="$2"; migflags="$migflags $1 $2"; shift; shift;;
	-sheader ) sheader="$2"; migflags="$migflags $1 $2"; shift; shift;;
	-iheader ) iheader="$2"; migflags="$migflags $1 $2"; shift; shift;;
	-dheader ) dheader="$2"; migflags="$migflags $1 $2"; shift; shift;;
	-maxonstack ) migflags="$migflags $1 $2"; shift; shift;;
	-split ) migflags="$migflags $1"; shift;;
	-MD ) sawMD=1; cppflags="$cppflags $1"; shift;;
	-cpp) shift; shift;;
	-cc) C=$2; shift; shift;;
	-migcom) M=$2; shift; shift;;
	-* ) cppflags="$cppflags $1"; shift;;
	* ) files="$files $1"; shift;;
    esac
done

for file in $files
do
    base="`basename "$file" .defs`"
    temp="$base".$$
    rm -f "$temp".c "$temp".d
    (echo '#line 1 '\""$file"\"; cat "$file") > "$temp".c
    $C -E $cppflags "$temp".c | $M $migflags || exit
    if [ "$sawMD" -a -f "$temp".d ]
    then
	deps=
	s=
	rheader="${header-${base}.h}"
	if [ "$rheader" != /dev/null ]; then
		deps="${deps}${s}${rheader}"; s=" "
	fi
	ruser="${user-${base}User.c}"
	if [ "$ruser" != /dev/null ]; then
		deps="${deps}${s}${ruser}"; s=" "
	fi
	rserver="${server-${base}Server.c}"
	if [ "$rserver" != /dev/null ]; then
		deps="${deps}${s}${rserver}"; s=" "
	fi
	rsheader="${sheader-/dev/null}"
	if [ "$rsheader" != /dev/null ]; then
		deps="${deps}${s}${rsheader}"; s=" "
	fi
	riheader="${iheader-/dev/null}"
	if [ "$riheader" != /dev/null ]; then
		deps="${deps}${s}${riheader}"; s=" "
	fi
	rdheader="${dheader-/dev/null}"
	if [ "$rdheader" != /dev/null ]; then
		deps="${deps}${s}${rdheader}"; s=" "
	fi
	for target in ${deps}
	do
		sed -e 's;^'"${temp}"'.o[ 	]*:;'"${target}"':;' \
		    -e 's;: '"${temp}"'.c;: '"$file"';' \
		< "${temp}".d > "${target}".d
	done
	rm -f "$temp".d
    fi
    rm -f "$temp".c
done

exit 0
