#! /bin/csh -f
#
# Do a specific make.
#

if ($#argv < 3) then
    echo "usage: domake <target> <X version> <name> ..."
#	E.g., "domake install X11 libInterViews libgraphic libtext"
endif
set target = $1
set xversion = $2
if (! -r cpu) then
    echo "unknown CPU type--make cpu"
    exit 1
endif
set machine = `cat cpu`
echo "CPU type is $machine"
if ($xversion == -) then
    set dir = $machine
else
    set dir = $machine.$xversion
endif
if (! -r ../bin/$dir) then
    echo "mkdir ../bin/$dir"
    mkdir ../bin/$dir; chmod 775 ../bin/$dir
endif
if (! -r ../lib/$dir) then
    echo "mkdir ../lib/$dir"
    mkdir ../lib/$dir; chmod 775 ../lib/$dir
endif
# skip to $3
shift
shift
#
# need to get genmake and mkdepend from ../bin/all and/or ../bin/$machine
#
set full = `/bin/pwd`
set path = ($full:h/bin/$machine $full:h/bin/all $path)

foreach name ($*)
    pushd ./$name > /dev/null
    echo ""
    echo "making $name"
    if ($target != clean) then
	if (! -d $dir) then
	    echo "mkdir $name/$dir"
	    mkdir $dir
	    if ($status != 0) then
		echo "mkdir failed"
		exit 1
	    endif
	    chmod 775 $dir
	endif
	if (! -d $machine) then
	    echo "mkdir $name/$machine"
	    mkdir $machine
	    chmod 775 $machine
	endif
	if (! -r $dir/Makefile) then
	    if ($xversion == -) then
		echo "genmake $machine"
		genmake $machine
	    else
		echo "genmake $machine $xversion"
		genmake $machine $xversion
	    endif
	endif
    endif
    if (-d $dir && -r $dir/Makefile) then
	cd $dir
	if ($target == -) then
	    echo "cd $name/$dir; make"
	    make
	else
	    echo "cd $name/$dir; make $target"
	    make $target
	    if ($target == clean) then
		echo "rm -f Makefile"
		rm -f Makefile
		cd ..
		echo "rmdir $dir"
		rmdir $dir
		if (-d $machine) then
		    echo "rmdir $machine"
		    rmdir $machine
		endif
	    endif
	endif
    endif
    popd > /dev/null
end
