#!/bin/csh -f
# This postinstall script echoes the values of the available
# arguments and environmental variables.
#

echo "Start postupgrade script"
echo ""

set InstDest = $argv[2]


echo "Removing symlinks in /usr/local from any previous installations"

foreach file ( asciitf2vtf ncdf2vdf raw2vdf vapor-setup.csh vapor-setup.sh vaporgui vaporversion vdf2raw vdfbkup.pl vdfcreate vdfls wrf2vdf wrfvdfcreate vdfedit flashamr2vdf tiff2geotiff getWMSImage.sh)

	if (-e  /usr/local/bin/$file && -l /usr/local/bin/$file) then
		/bin/rm /usr/local/bin/$file
	endif
end


#
#
#
#

foreach fullpath ($InstDest/VAPOR.app/Contents/MacOS/*.dylib $InstDest/VAPOR.app/Contents/MacOS/*.so $InstDest/VAPOR.app/Contents/MacOS/*.dlm)

	set file = `basename $fullpath`
	if (-e  /usr/local/lib/$file && -l /usr/local/lib/$file) then
		/bin/rm /usr/local/lib/$file
	endif

end

foreach fullpath ($InstDest/VAPOR.app/Contents/SharedSupport/man/man1/*)

	set file = `basename $fullpath`
	if (-e  /usr/local/share/man/man1/$file && -l /usr/local/share/man/man1/$file) then
		/bin/rm /usr/local/share/man/man1/$file
	endif

end

if (-e  /usr/local/share/vapor-1.5.2 && -d /usr/local/share/vapor-1.5.2) then
	/bin/rm -fr /usr/local/share/vapor-1.5.2
endif




set vapor_root = $InstDest/VAPOR.app
set bindir = $vapor_root/Contents/MacOS
set lib_search_dirs = $bindir
set mandir = $vapor_root/Contents/SharedSupport/man

set sedcmd = sed
if (-e /bin/sed) set sedcmd = /bin/sed
if (-e /usr/bin/sed) set sedcmd = /usr/bin/sed


#
# Edit the user environment setup scripts
#
set old0 = 'set[    ][  ]*root[     ][  ]*=.*$'
set new0 = "set root = $vapor_root"
set old1 = 'set[    ][  ]*lib_search_dirs[  ][  ]*=.*$'
set new1 = "set lib_search_dirs = $lib_search_dirs"
set old2 = 'set[    ][  ]*bindir[   ][  ]*=.*$'
set new2 = "set bindir = $bindir"
set old3 = 'set[    ][  ]*mandir[   ][  ]*=.*$'
set new3 = "set mandir = $mandir"
$sedcmd -e "s#$old0#$new0#" -e "s#$old1#$new1#"  -e "s#$old2#$new2#"  -e "s#$old3#$new3#" < $bindir/vapor-setup.csh >! $bindir/vapor-setup.tmp
/bin/mv $bindir/vapor-setup.tmp $bindir/vapor-setup.csh


set old0 = 'root=.*$'
set new0 = "root=$vapor_root"
set old1 = 'lib_search_dirs=.*$'
set new1 = "lib_search_dirs=$lib_search_dirs"
set old2 = 'bindir=.*$'
set new2 = "bindir=$bindir"
set old3 = 'mandir=.*$'
set new3 = "mandir=$mandir"
$sedcmd -e "s#$old0#$new0#" -e "s#$old1#$new1#" -e "s#$old2#$new2#" -e "s#$old3#$new3#" < $bindir/vapor-setup.sh >! $bindir/vapor-setup.tmp
/bin/mv $bindir/vapor-setup.tmp $bindir/vapor-setup.sh

echo "Finish postupgrade script"
exit 0
