# ----- Load modules --------------------------

module load netcdf/4.2.1-pgf90
module list 

# ---- set some directories and other stuff ---

set bdir = ${PWD}
set mode = $1

# ----- Set libraries and includes ------------

set libs   = "-L ${bdir}/lib"
set libs   = "${libs} -lcdfio" 
set libs   = "${libs} -lcdfplus" 
set libs   = "${libs} -lipo"
set libs   = "${libs} -lgm2em"

set ncdf_incs  = `nc-config --fflags`
set ncdf_libs  = `nc-config --flibs`

# ----- Make clean ----------------------------
clean:

if ( "${mode}" != "clean" ) goto compile

cd ${bdir}/lib
foreach lib (  libcdfio libcdfplus libipo libgm2em ) 
 \rm -f ${lib}.o
end

cd ${bdir}/diag/
foreach tool (  calc_qgpv check_boundcon difference hydrostatic qvec_analysis ) 
 \rm -f ${tool}.o
 \rm -f ${tool}
end

cd ${bdir}/post/
foreach tool ( add2p rotate_lalo )  
 \rm -f ${tool}.o
 \rm -f ${tool}
end

cd ${bdir}/prep/
foreach tool ( coastline cutnetcdf def_anomaly p2z ref_profile rotate_grid z2s ) 
 \rm -f ${tool}.o
 \rm -f ${tool}
end

cd ${bdir}/pvin/
foreach tool ( inv_cart prep_iteration pv_to_qgpv z2s ) 
 \rm -f ${tool}.o
 \rm -f ${tool}
end

cd ${bdir}/spec/
foreach tool ( modify_anomaly ) 
  \rm -f ${tool}.o
  \rm -f ${tool}
end

# ----- Compile -------------------------------
compile:

if ( "${mode}" != "compile" ) goto done

cd ${bdir}/lib

foreach lib (  libcdfio libcdfplus libipo libgm2em ) 

\rm -f ${lib}.o

echo "pgf90 -c ${lib}.f ${ncdf_incs}"  
      pgf90 -c ${lib}.f ${ncdf_incs}   

end

cd ${bdir}/diag/

foreach tool (  calc_qgpv check_boundcon difference hydrostatic qvec_analysis ) 

\rm -f ${tool}.o
\rm -f ${tool}

echo "pgf90 -c ${tool}.f ${ncdf_incs}"  
      pgf90 -c ${tool}.f ${ncdf_incs}   
echo "pgf90 -o ${tool} ${tool}.o ${libs} ${ncdf_libs}"        
      pgf90 -o ${tool} ${tool}.o ${libs} ${ncdf_libs}

if ( ! -f ${tool} ) then
  echo "ERROR: compilation of <tool> failed... exit"
  exit 1
endif

end

cd ${bdir}/post/

foreach tool ( add2p rotate_lalo )  

\rm -f ${tool}.o
\rm -f ${tool}

echo "pgf90 -c ${tool}.f ${ncdf_incs}"  
      pgf90 -c ${tool}.f ${ncdf_incs}   
echo "pgf90 -o ${tool} ${tool}.o ${libs} ${ncdf_libs}"        
      pgf90 -o ${tool} ${tool}.o ${libs} ${ncdf_libs}

if ( ! -f ${tool} ) then
  echo "ERROR: compilation of <tool> failed... exit"
  exit 1
endif

end

cd ${bdir}/prep/

foreach tool ( coastline cutnetcdf def_anomaly p2z ref_profile rotate_grid z2s ) 

\rm -f ${tool}.o
\rm -f ${tool}

echo "pgf90 -c ${tool}.f ${ncdf_incs}"  
      pgf90 -c ${tool}.f ${ncdf_incs}   
echo "pgf90 -o ${tool} ${tool}.o ${libs} ${ncdf_libs}"        
      pgf90 -o ${tool} ${tool}.o ${libs} ${ncdf_libs}

if ( ! -f ${tool} ) then
  echo "ERROR: compilation of <tool> failed... exit"
  exit 1
endif

end

cd ${bdir}/pvin/

foreach tool ( inv_cart prep_iteration pv_to_qgpv z2s ) 

\rm -f ${tool}.o
\rm -f ${tool}

echo "pgf90 -c ${tool}.f ${ncdf_incs}"  
      pgf90 -c ${tool}.f ${ncdf_incs}   
echo "pgf90 -o ${tool} ${tool}.o ${libs} ${ncdf_libs}"        
      pgf90 -o ${tool} ${tool}.o ${libs} ${ncdf_libs}

if ( ! -f ${tool} ) then
  echo "ERROR: compilation of <tool> failed... exit"
  exit 1
endif

end

cd ${bdir}/spec/

foreach tool ( modify_anomaly ) 

\rm -f ${tool}.o
\rm -f ${tool}

echo "pgf90 -c ${tool}.f ${ncdf_incs}"  
      pgf90 -c ${tool}.f ${ncdf_incs}   
echo "pgf90 -o ${tool} ${tool}.o ${libs} ${ncdf_libs}"        
      pgf90 -o ${tool} ${tool}.o ${libs} ${ncdf_libs}

if ( ! -f ${tool} ) then
  echo "ERROR: compilation of <tool> failed... exit"
  exit 1
endif

end

# ----- Done ----------------------------------
done:

exit 0
