#!/bin/csh -f
#
#      $Id: ymkmf,v 1.11 2005-02-16 20:48:08 grubin Exp $
#
#########################################################################
#                                                                       #
#			   Copyright (C)  1993                                      #
#	     University Corporation for Atmospheric Research                #
#			   All Rights Reserved                                      #
#                                                                       #
#########################################################################
#
#	File:		ymkmf
#
#	Author:		Jeff W. Boote
#			National Center for Atmospheric Research
#			PO 3000, Boulder, Colorado
#
#   Date:		Tue Nov 2 09:22:31 MST 1993
#
#   Description:    This is a wrapper function for ymake so that
#                   it is easier to create a Makefile in any sub-directory
#                   of the distribution. This script parses the cwd
#                   and searches up the tree until it finds the name
#                   "ncarg-'version' or "$topdirname".  It considers that to be
#                   the top of your current distribution.  If the script
#                   is passed "$topdirname" it just passes TOPDIR and
#                   CURDIR to ymake, and uses ymake from the installed
#                   area.  If it is called without any args, then it
#                   assumes ymake is in the "config" directory that is
#                   a direct sub-dir of "ncarg-'version'" where 'version'
#                   is the current release of NCAR Graphics.
#
#	Usage:		ymkmf [topdirname]
#
#	Environment:	csh script
#
#	Files:		ymake
#
#
#	Options:	topdirname

onintr	cleanup

set	topdir	= .
set	thisdir = `pwd`

set	defines

while ($#argv)

	switch ("$argv[1]")

	case	-D*
	case	-noproject
		set defines = ($defines "$argv[1]")

		breaksw

	default:
		# make sure "topname" is last arg
		if ($#argv != 1) then
			echo "$0 : Usage `basename $0` [-noproject] [-D ...] topname"
			exit 1
		endif

		set	topname = $argv[1]

		breaksw
	endsw

	shift
end

if (! $?topname) then
#   assume default installation: (for example: .../ncarg-4.4.1)
    if (-e version) then
        set vers = `cat version`
        set topname = ncarg-$vers
    else
#   installation in a different location; find it
        set findvers  = `find . -name version -print`
        if (! $?findvers) then
            set topname = `dirname $findvers`
        else
            set topname = .
        endif

        cd $topname
    endif
	set	def_topname
endif

if ("$topname" == ".") then
	set	topdir = .
	set	curdir = .
	goto shortcut
endif

echo $thisdir | fgrep $topname >& /dev/null
if ($status != 0) then
	if ($?def_topname) then
		set topdir = .
		set curdir = .
		goto shortcut
	endif
	echo "$0 : Unable to find TopDir $topname in $thisdir"
	exit 1
endif

echo $topname | fgrep / >& /dev/null
if ($status != 1) then
	echo "$0 : Bad TopDir name $topname"
	exit 1
endif

set	base = `basename $thisdir`
set	ndir = `dirname $thisdir`

while ("$base" != "$topname")
	if (("$base" == "/") || ("$base" == "")) then
		if ($?def_topname) then
			set topdir = .
			set curdir = .
			goto shortcut
		endif
		echo "$0 : Bad TopDir name $topname"
		exit 1
	endif
	if ($?curdir) then
		set curdir = "$base"/"$curdir"
	else
		set curdir = "$base"
	endif

	set topdir = ../"$topdir"

	set base = `basename $ndir`
	set ndir = `dirname ${ndir}`
end

if ($?curdir) then
	set curdir = ./"$curdir"
else
	set curdir = .
endif

shortcut:

set defines = ($defines -Curdir "$curdir" -Topdir "$topdir")

if ( -e "$topdir"/config/ymake) then
	@ useinstalled = 0
else
	@ useinstalled = 1
endif

if ($useinstalled == 1) then
	exec ymake $defines
else
	if ( ! -x "$topdir"/config/ymake-filter) then
		echo "Making ymake from Makefile.ini in "$topdir"/config first"
		(cd "$topdir"/config; make -f Makefile.ini clean all)
		echo ; echo "Continuing in: `pwd`"
	endif
	exec "$topdir"/config/ymake -config "$topdir"/config $defines
endif

cleanup:
echo ""
echo "Terminating..."
echo ""
exit 0
