#!/bin/csh -f

if ( ! -e configure.wps ) then
	echo "Do 'configure' first"
	exit ( 1 ) 
endif

if  ( ( ! $?NETCDF ) && ( -d netcdf_links ) ) then
	setenv NETCDF `pwd`/netcdf_links
	setenv temp_netcdf 1
else
	setenv temp_netcdf 0
endif

set DEV_TOP = `pwd`
set first_char = `grep ^WRF_DIR configure.wps | awk '{print $3}' | cut -c -1`

if ( "$first_char" == "/" ) then
	set WRF_DIR_PRE	=
else
	set WRF_DIR_PRE	=	${DEV_TOP}/
endif

if      ( ${#argv} == 0 ) then
	set names	=	( geogrid ungrib metgrid g1print g2print plotfmt rd_intermediate plotgrids mod_levs avg_tsfc )
	set NAMES	=	( GEOGRID UNGRIB METGRID GRIBUTIL GRIBUTIL UTIL     UTIL            UTIL      UTIL    UTIL   )
else if ( $1 == wps    ) then
	set names	=	( geogrid ungrib metgrid )
	set NAMES	=	( GEOGRID UNGRIB METGRID )
else if ( $1 == util    ) then
	set names	=	( g1print g2print plotfmt rd_intermediate plotgrids mod_levs avg_tsfc )
	set NAMES	=	( GRIBUTIL GRIBUTIL UTIL       UTIL          UTIL      UTIL    UTIL   )
else if ( $1 == geogrid ) then
	set names	=	( geogrid )
	set NAMES	=	( GEOGRID )
else if ( $1 == ungrib  ) then
	set names	=	( ungrib  )
	set NAMES	=	( UNGRIB  )
else if ( $1 == metgrid ) then
	set names	=	( metgrid )
	set NAMES	=	( METGRID )
else if ( $1 == g1print ) then
	set names	=	( g1print )
	set NAMES	=	( GRIBUTIL )
else if ( $1 == g2print ) then
	set names	=	( g2print )
	set NAMES	=	( GRIBUTIL )
else if ( $1 == plotfmt ) then
	set names	=	( plotfmt )
	set NAMES	=	( UTIL )
else if ( $1 == rd_intermediate ) then
	set names	=	( rd_intermediate )
	set NAMES	=	( UTIL )
else if ( $1 == plotgrids ) then
	set names	=	( plotgrids )
	set NAMES	=	( UTIL )
else if ( $1 == mod_levs ) then
	set names	=	( mod_levs )
	set NAMES	=	( UTIL )
else if ( $1 == avg_tsfc ) then
	set names	=	( avg_tsfc )
	set NAMES	=	( UTIL )
else
        echo "*****"
        echo " "
	echo "Unrecognized compile target $1."
        echo " "
        echo "Usage: compile [target]"
        echo "where target is one of"
        echo "   wps"
        echo "   util"
        echo "   geogrid"
        echo "   ungrib"
        echo "   metgrid"
        echo "   g1print"
        echo "   g2print"
        echo "   plotfmt"
        echo "   rd_intermediate"
        echo "   plotgrids"
        echo "   mod_levs"
        echo "   avg_tsfc"
        echo " "
        echo " or just run compile with no target to build everything."
        echo " "
        echo "*****"
        exit(1)
endif

echo " "
if ( ${#argv} == 0 ) then
	echo "**** Compiling WPS and all utilities ****"
else
	echo "**** Compiling $1 ****"
endif
echo " "

set count = 1
foreach f ( $names )
	if ("$NAMES[$count]" == "UTIL") then
		( cd util ; make -i -r WRF_DIR_PRE="${WRF_DIR_PRE}" DEV_TOP="${DEV_TOP}" TARGET="${f}.exe" CPP_TARGET="$NAMES[$count]" all )
	else if ("$NAMES[$count]" == "GRIBUTIL") then
		( cd ungrib ; make -i -r WRF_DIR_PRE="${WRF_DIR_PRE}" DEV_TOP="${DEV_TOP}" TARGET="${f}.exe" CPP_TARGET="$NAMES[$count]" all )
		if ( -e ungrib/src/${f}.exe ) then
			( cd util ; ln -sf ../ungrib/src/${f}.exe . )
		endif
	else
		( cd $f ; make -i -r WRF_DIR_PRE="${WRF_DIR_PRE}" DEV_TOP="${DEV_TOP}" TARGET="${f}.exe" CPP_TARGET="$NAMES[$count]" all )
		if ( -e ${f}/src/${f}.exe ) then
			ln -sf ${f}/src/${f}.exe .
		endif
	endif
	@ count ++
end

if ( $temp_netcdf == 1 ) then
	unsetenv NETCDF
endif

exit(0)
