#!/bin/csh
#
# INSTALL -- Install IRAF on a UNIX/IRAF host.  May also be run after the
# initial installation as a consistency check to verify that all the necessary
# links and file permissions are still in place (e.g., after updating UNIX
# itself).
#
# Usage:	install [-n] [-r rootdir] [-i imdir] [-b localbindir] \
#		    [-R oldroot] [-I oldimdir] [-u username (e.g., 'iraf')]
#		    [-m mach (e.g., 'sparc', 'mc68020') ]
#
# Example:
#		% su
#		% cd $hlib
#		% install -n
#		% install
#
# If run with no arguments, INSTALL will make an informed guess and prompt
# with this value; type <return> to accept the value, or enter a new value.
#
# Use "install -n" to do a dry run to see what the would be done, without
# actually modifying the host system and IRAF configuration files.  To do the
# actual installation one must be superuser, but anyone can run "install -n"
# to see what it would do.
# ----------------------------------------------------------------------------

unset	noclobber
unalias cd cp cmp echo ln mv rm sed set grep ls chmod chown pwd touch
set	path	= (/sbin /usr/sbin /bin /usr/bin /usr/ucb /etc /usr/etc)

# set echo

set W		= '\([ 	"]\)'		# match a blank, tab, or "
set TEMP	= "/tmp/I_temp"
set exec	= yes
set user	= iraf
set LS		= /bin/ls
alias PUT	"mv -f \!*; chown $user \!$ "

# Determine platform architecture.
if (-f /etc/redhat-release) then
    set mach = redhat
else if (-f /etc/SuSE-release) then
    set mach = suse
else
    set mach = `uname -s | tr '[A-Z]' '[a-z]'`
endif

if ($?iraf == 1) then
    if (! -d $iraf) then
	setenv iraf ""
    endif
else
    setenv iraf	""
endif

set imdir	= ""
set lbin	= ""
set o_iraf 	= ""
set o_imdir 	= ""

# Process any command line arguments.
while ("$1" != "")
    switch ("$1")
    case -n:				# no execute
	alias PUT "diff \!$ \!^; rm -f $TEMP"
	set exec = no
	breaksw
    case -b:				# set local bin directory (unix)
	if ("$2" != "") then
	    shift
	else
	    echo "missing argument to '-b <localbindir>' switch"
	    exit 1
	endif
	set lbin = "$1"
	breaksw
    case -i:				# set imdir directory
	if ("$2" != "") then
	    shift
	else
	    echo "missing argument to '-i <imdir>' switch"
	    exit 1
	endif
	set imdir = "$1"
	breaksw
    case -m:				# set machine type
	if ("$2" != "") then
	    shift
	else
	    echo "missing argument to '-m <mach>' switch"
	    exit 1
	endif
	set mach = "$1"
	breaksw
    case -r:				# set root directory
	if ("$2" != "") then
	    shift
	else
	    echo "missing argument to '-r <irafdir>' switch"
	    exit 1
	endif
	setenv iraf "$1"
	breaksw
    case -I:				# set old imdir directory
	if ("$2" != "") then
	    shift
	else
	    echo "missing argument to '-I <o_imdir>' switch"
	    exit 1
	endif
	set o_imdir = "$1"
	breaksw
    case -R:				# set old root directory
	if ("$2" != "") then
	    shift
	else
	    echo "missing argument to '-R <o_iraf>' switch"
	    exit 1
	endif
	set o_iraf = "$1"
	breaksw
    case -u:				# set user name for iraf, e.g, 'iraf'
	if ("$2" != "") then
	    shift
	else
	    echo "missing argument to '-u <irafowner>' switch"
	    exit 1
	endif
	set user = "$1"
	breaksw
    default:
	echo "install: unknown argument $1"
	breaksw
    endsw

    if ("$2" == "") then
	break
    else
	shift
    endif
end

# Set $iraf, the new root directory for iraf.  The system must already have
# been read in at this directory (e.g., /usr/iraf), but we assume that no
# files have yet been modified.

if ("$iraf" == "") then
    # Make a guess at what the new root directory is.
    set d_iraf = ""
    if (-d /iraf/iraf) then
	set d_iraf = /iraf/iraf
    else if (-d /iraf) then
	set d_iraf = /iraf
    else if (-d /usr/iraf) then
	set d_iraf = /usr/iraf
    else
	# Search for a directory /u*/iraf.
	foreach i (/u*)
	    if (-d $i/iraf) then
		set d_iraf = "$i/iraf"
		break
	    endif
	end
	# Search for a directory /*/iraf.
	if ("$d_iraf" == "") then
	    foreach i (/*)
		if (-d $i/iraf) then
		    set d_iraf = "$i/iraf"
		    break
		endif
	    end
	endif
    endif

    if ("$d_iraf" == "") then
	set d_iraf = /usr/iraf
    endif
else
    set d_iraf = $iraf
endif

# If the given directory doesn't exist, compute the root directory relative
# to $iraf/unix/hlib (our current directory, presumably).

if (! -d $d_iraf) then
    set d_iraf = `(cd ../..;cwd)`
endif

set d_iraf = `echo $iraf | sed -e 's+/\(["]*\)$+\1+'`
echo -n "new iraf root directory ($d_iraf): "
setenv iraf "$<"
if ("$iraf" == "") then
    setenv iraf "$d_iraf"
endif

# Get the values of o_iraf and o_imdir from the current mkiraf.csh file, if
# not already otherwise defined.

cd $iraf/unix/hlib
set WS = '[ 	]'
if ("$o_iraf" == "") then
    set o_iraf =\
    `grep "^set$WS*iraf" mkiraf.csh | sed -e "s+^.*=$WS*++" | sed -e 's+"++g'`
endif
if ("$o_imdir" == "") then
    set o_imdir =\
    `grep "^set$WS*imdir" mkiraf.csh | sed -e "s+^.*=$WS*++" | sed -e 's+"++g'`
endif

# Strip any trailing / in the pathname to be matched, so that the trailing /,
# if present, will be LEFT in the occurrence of the path in the file.

set o_iraf  = `echo $o_iraf  | sed -e 's+/\(["]*\)$+\1+'`
set o_imdir = `echo $o_imdir | sed -e 's+/\(["]*\)$+\1+'`

# Set $imdir, the default user image storage root directory.  Each user imdir
# will be a subdirectory of this directory by default, when MKIRAF is run.
# Since bulk image data can consume hundreds of megabytes of disk space, IRAF
# likes to keep such data on a public scratch device, which is probably not
# backed up, which has a short file expiration interval, and possibly which
# has been configured (newfs/mkfs) with a large block size for fast seq. i/o.

if ("$imdir" == "") then
    if (-d $o_imdir) then
	set d_imdir = $o_imdir
    else if (-d /tmp2) then
	set d_imdir = /tmp2/iraf
    else if (-d /tmp3) then
	set d_imdir = /tmp3/iraf
    else if (-d /usr/tmp) then
	set d_imdir = /usr/tmp
    else
	set d_imdir = /tmp
    endif

    echo -n "default root image storage directory ($d_imdir): "
    set imdir = "$<"
    if ("$imdir" == "") then
	set imdir = "$d_imdir"
    endif
endif

# Get UNIX directory where HSI commands (links) are to be installed, if not
# set on command line.  IRAF will only install a very few new commands in this
# directory.  Ideally it should be a directory on the standard user $path,
# so that users do not have to customize their . files just to run IRAF.

if ("$lbin" == "") then
    # Look around and come up with a likely candidate directory.
    if (-d /usr/local/bin) then
	set d_lbin = /usr/local/bin
    else if (-d /local/bin) then
	set d_lbin = /local/bin
    else
	set d_lbin = /usr/bin
    endif

    echo -n "local unix commands directory ($d_lbin): "
    set lbin = "$<"
    if ("$lbin" == "") then
	set lbin = "$d_lbin"
    endif
endif

# Verify the machine type.
echo "install iraf for machine type $mach"
# Document the old root pathnames.
echo "old iraf root = $o_iraf, old imdir = $o_imdir"

# Cannot have iraf and imdir the same.
if ($iraf == $imdir) then
    echo "imdir cannot be the same as the iraf root, try again"
    exit 1
endif

# The following is partially system dependent.
# ------------------------------------------
# Set the BINDIRS pathnames - directories where the HSI executables go.
set host	= "$iraf/unix"
set hbin	= "$iraf/unix/bin.$mach"	# "mach" is SUN specific!!
set hlib	= "$iraf/unix/hlib"
set fbin	= "$iraf/bin"

# Replace any // by /.
set host	= `echo $host | sed -e "s+//+/+g"`
set hbin	= `echo $hbin | sed -e "s+//+/+g"`
set fbin	= `echo $fbin | sed -e "s+//+/+g"`
set hlib	= `echo $hlib | sed -e "s+//+/+g"`

# Strip any trailing /.
set host	= `echo $host | sed -e 's+/\(["]*\)$+\1+'`
set hbin	= `echo $hbin | sed -e 's+/\(["]*\)$+\1+'`
set fbin	= `echo $fbin | sed -e 's+/\(["]*\)$+\1+'`
set hlib	= `echo $hlib | sed -e 's+/\(["]*\)$+\1+'`
set BINDIRS	= "$hbin $hlib $fbin"

# The following file lists are partially system dependent.
set PATHFILES	= "mkiraf.csh libc/iraf.h cl.csh"
set MODEFILES	= "mkiraf.csh mkmlist.csh generic.e mkpkg.e rmbin.e rmfiles.e rpp.e rtar.e wtar.e xc.e xpp.e xyacc.e sgidispatch.e $hbin/sgi2*.e cl.csh mkfloat.csh fc.csh $host/reboot"
set LINKFILES	= "cl.e generic.e mkiraf.csh mkmlist.csh mkpkg.e rmbin.e rmfiles.e rtar.e sgidispatch.e wtar.e rpp.e xpp.e xyacc.e xc.e"
# ------------------------------------------

# Begin installation.
# ------------------

echo "installing iraf at $iraf, imdir=$imdir, lbindir=$lbin"
echo -n "proceed with installation? (yes): "; set temp = $<
if (! ($temp == "" || $temp == "y" || $temp == "yes")) then
    echo "installation terminated"
    exit 1
else
    echo ""
endif

# Create the root imdir as a public scratch directory, if not already created.
if (! -d $imdir) then
    echo "creating root imdir at $imdir"
    if ($exec == yes) then
	mkdir $imdir; chmod 777 $imdir
    endif
else
    if ($exec == yes) then
	chmod 777 $imdir
    endif
    echo "default root imdir is ok"
endif

# Allow deletion of files in /tmp - needed for multiuser tape allocation.
if ($exec == yes) then
    echo "set delete permission on /tmp"
    chmod -t /tmp
endif

# Initialize permissions of tape devices.
if ($exec == yes) then
    echo "set mode 0666 on magtape devices to permit tape allocation"
    chmod 666 /dev/*st[0-4]*
endif

# Set owner=root for the device allocation task, alloc.e.

foreach i ($BINDIRS)
    if (-e $i/alloc.e) then
	if ("`$LS -l $i/alloc.e | grep 'rwsr-.*root'`" != "") then
	    echo "device allocation task $i/alloc.e is ok"
	else
	    echo "chown $i/alloc.e to root, set uid modebit"
	    if ($exec == yes) then
		chown 0 $i/alloc.e
		chmod u+s $i/alloc.e
	    endif
	endif
	break
    else
	echo "cannot find alloc.e"
    endif
end

# Edit the $iraf pathname in the .login file for user 'iraf'.
cd $iraf/local

if (-e .login) then
    rm -f $TEMP >& /dev/null
    sed -e "s+$W$o_iraf+\1$iraf+" .login > $TEMP
    cmp -s .login $TEMP
    if ($status) then
	echo "updating $iraf/local/.login"
	PUT $TEMP .login
    else
	echo "iraf .login file is ok"
	rm -f $TEMP
    endif
else
    echo "cannot find the iraf .login file"
endif

# Edit the $iraf and $imdir pathnames in the mkiraf.csh and libc/iraf.h files.
cd $iraf/unix/hlib

foreach i ($PATHFILES)
    if (-e $i) then
	rm -f $TEMP >& /dev/null
	sed -e "s+$W$o_iraf+\1$iraf+" $i |\
	    sed -e "s+$W$o_imdir+\1$imdir+" > $TEMP
	cmp -s $i $TEMP
	if ($status) then
	    echo "updating $i"
	    PUT $TEMP $i
	    if ($exec == yes) then
		chmod 755 $i
	    endif
	else
	    echo "$i is ok"
	    rm -f $TEMP
	endif
    else
	echo "cannot find $i"
    endif
end

# Set default file permissions for the executable files in the BINDIRS,
# in case the file mode has somehow been changed, e.g., in a file restore
# or copy.

echo "--------------- Check File Permissions ----------------"

foreach i ($MODEFILES)
    set file = $i
    if (! -e $file) then
	foreach j ($BINDIRS)
	    if (-e $j/$i) then
		set file = $j/$i
		break
	    endif
	end
    endif
	
    if (-e $file) then
	if ("`$LS -l $file | grep '^.rwxr.xr.x'`" != "") then
	    echo "file $file exists with the correct permissions"
	else
	    echo "set mode for $file to 0755"
	    if ($exec == yes) then
		chmod 755 $file
	    endif
	endif
    else
	echo "cannot find $file"
    endif
end

# Link $hlib/libc/iraf.h to <iraf.h>.  This is needed not only to compile C
# source files in iraf, but also to define $iraf, $host, etc. for iraf tasks.

echo "---------------- Check Symbolic Links -----------------"
set file1 = /usr/include/iraf.h
set file2 = $iraf/unix/hlib/libc/iraf.h

if (-e $file1) then
    if ("`$LS -l $file1 | grep $file2`" != "") then
	echo "symbolic link $file1 is ok"
    else
	echo "link $file1 exists but is invalid; unlink $file1"
	if ($exec == yes) then
	    rm -f $file1
	endif
	echo "make symbolic link $file1 -> $file2"
	if ($exec == yes) then
	    rm -f $file1
	    ln -s $file2 $file1
	endif
    endif
else
    echo "make symbolic link $file1 -> $file2"
    if ($exec == yes) then
	rm -f $file1
	ln -s $file2 $file1
    endif
endif

# Establish the remaining symbolic links to HSI tasks.
echo "directory $lbin" 
cd $lbin

foreach i ($LINKFILES)
    # Locate the file to be linked to.
    set file1 = $i:r
    foreach j ($BINDIRS)
	set file2 = $j/$file1.csh
	if (-e $file2) then
	    break
	endif
	set file2 = $j/$i
	if (-e $file2) then
	    break
	endif
    end

    # Verify or set the link.
    if ("`$LS $file1`" == "$file1") then
	if ("`$LS -l $file1 | grep $file2`" != "") then
	    echo "symbolic link $file1 is ok"
	else
	    echo "link $file1 exists but is invalid; unlink $file1"
	    if ($exec == yes) then
		rm -f $file1
	    endif
	    echo "make symbolic link $file1 -> $file2"
	    if ($exec == yes) then
		rm -f $file1
		ln -s $file2 $file1
	    endif
	endif
    else
	echo "make symbolic link $file1 -> $file2"
	if ($exec == yes) then
	    rm -f $file1
	    ln -s $file2 $file1
	endif
    endif
end

# Mark the system update time.
echo 'touch hlib$utime'
if ($exec == yes) then
    touch $hlib/utime
endif

#exit 0					# COMMENT OUT FOR SUN/IRAF

# SUN/IRAF specific stuff.
# ------------------------------

if ($mach == linux || $mach == redhat || || $mach == suse || \
	$mach == freebsd || $mach == sunos) then
    set pciraf = yes
else
    set pciraf = no
endif

# If PC-IRAF only do X.
if ($pciraf == yes) then
    goto xconfig
endif

# Install custom suntools (GTERM and IMTOOL).

if ("`grep Release.4 /etc/motd`" != "" || `mach` == "i386") then
    set SUNOS4 = yes
else
    set SUNOS4 = no
endif

echo ""
echo "------------ Custom Suntools Installation -------------"

if ($SUNOS4 == "yes") then
    # SunOS 4.0 and later versions.
    # Install GTERM and IMTOOL executables in /usr/bin.
    # ----------------------------------

    # Get the path to the user bin directory.
    set usrbin = /usr/bin
    if ($mach != `mach`) then
	echo -n "/usr/bin directory for machine type $mach ($usrbin): "
	set usrbin = "$<"
	if ("$usrbin" == "") then
	    set usrbin = "$usrbin"
	endif
    endif

    # Install the executables.
    cd $usrbin
    foreach i (gterm imtool)
	set file2 = $hbin/$i.e
	# Check if installed executable, if any, is up to date.
	if (-e $i) then
	    rm -f $TEMP.[12] >& /dev/null
	    size $i > $TEMP.1;  size $file2 > $TEMP.2
	    cmp -s $TEMP.1 $TEMP.2
	    if ($status || "`find $file2 -newer $i -print`" != "") then
		set uptodate = no
	    else
		set uptodate = yes
	    endif
	    rm $TEMP.[12]
	else
	    set uptodate = no
	endif

	# Update file if necessary.
	if ($uptodate == no) then
	    echo "copy $file2 to $usrbin/$i"
	    if (-e $file2) then
		if ($exec == yes) then
		    if (-e $i) then
			rm -f $i
		    endif
		    cp $file2 $i
		endif
	    else
		echo "file $file2 not found"
	    endif
	else
	    echo "installed version of $i is up to date"
	endif
    end

    # Add entries for GTERM and IMTOOL to rootmenu file, if necessary.
    set file = /usr/lib/rootmenu
    if ("`grep gterm $file`" != "" &&\
	"`grep imtool $file`" != "") then
	echo "standard rootmenu file is ok (has gterm, imtool entries)"
    else
	echo -n "add entries for gterm and imtool to rootmenu file? (yes): "
	set temp = "$<"

	if ("$temp" == "" || "$temp" == "y" || "$temp" == "yes") then
	    (rm -f $TEMP;  rm -f $TEMP.s) >& /dev/null
	    if ("`grep gterm $file`" == "") then
		echo '7a\' > $TEMP.s
		echo '"Gterm"			gterm' >> $TEMP.s
		sed -f $TEMP.s < $file > $TEMP
		PUT $TEMP $file; rm $TEMP.s
	    endif
	    if ("`grep imtool $file`" == "") then
		echo '8a\' > $TEMP.s
		echo '"ImTool"		imtool' >> $TEMP.s
		sed -f $TEMP.s < $file > $TEMP
		PUT $TEMP $file; rm $TEMP.s
	    endif
	endif
    endif

else
    # Install the custom Suntools - SunOS versions prior to 4.0.
    # -------------------------------
    echo -n "install custom suntools (GTERM, IMTOOL)? (yes): "; set temp = "$<"
    if (! ("$temp" == "" || "$temp" == "y" || "$temp" == "yes")) then
	echo "suntools installation skipped"
	exit 0
    endif

    # The custom suntools executable is upwards compatible with standard
    # suntools.  If the answer to the query below is yes, the new executable
    # replaces /usr/bin/suntools, which is renamed /usr/bin/suntools.BAK.
    # If the answer is no, GTERM and IMTOOL will be linked to the uninstalled
    # IRAF version of suntools, $iraf/local/suntools, which will work, but
    # runtime memory requirements will be 800 Kb or so greater than otherwise.
    # The main reason one would not want to use the IRAF /usr/bin/suntools is
    # if a custom version of /usr/bin/suntools containing tools not in the
    # standard SUN version of suntools has already been installed.

    echo -n "install custom version of /usr/bin/suntools executable? (yes): "
    set temp = "$<"

    if ("$temp" == "" || "$temp" == "y" || "$temp" == "yes") then
	# Install custom version of /usr/bin/suntools, and make links for gterm
	# and imtool in /usr/bin, like the other suntools.

	set usrbin = /usr/bin
	set file2 = $hbin/suntools.e

	if ($mach != `mach`) then
	    echo -n "/usr/bin directory for machine type $mach ($usrbin): "
	    set usrbin = "$<"
	    if ("$usrbin" == "") then
		set usrbin = "$usrbin"
	    endif
	endif

	set suntools = $usrbin/suntools

	# Check if installed executable is up to date.
	rm -f $TEMP.[12] >& /dev/null
	size $suntools > $TEMP.1;  size $file2 > $TEMP.2
	cmp -s $TEMP.1 $TEMP.2
	if ($status || "`find $file2 -newer $suntools -print`" != "") then
	    set uptodate = no
	else
	    set uptodate = yes
	endif

	rm $TEMP.[12]

	# Update file if necessary.
	if ($uptodate == no) then
	    if (! -e $suntools.BAK) then
		echo "rename standard suntools ($suntools) to $suntools.BAK"
		if ($exec == yes) then
		    cp $suntools $suntools.BAK
		endif
	    endif

	    echo "copy $file2 to $suntools"
	    if (-e $file2) then
		if ($exec == yes) then
		    cp $file2 temp
		    mv -f temp $suntools
		endif
	    else
		echo "file $file2 not found"
	    endif
	else
	    echo "installed version of $suntools is up to date"
	endif

	# Add entries for GTERM and IMTOOL to rootmenu file, if necessary.
	set file = /usr/lib/rootmenu
	if ("`grep gterm $file`" != "" &&\
	    "`grep imtool $file`" != "") then
	    echo "standard rootmenu file is ok (has gterm, imtool entries)"
	else
	    echo -n "add entries for gterm and imtool to rootmenu file? (yes): "
	    set temp = "$<"

	    if ("$temp" == "" || "$temp" == "y" || "$temp" == "yes") then
		(rm -f $TEMP;  rm -f $TEMP.s) >& /dev/null
		if ("`grep gterm $file`" == "") then
		    echo '7a\' > $TEMP.s
		    echo '"Gterm"			gterm' >> $TEMP.s
		    sed -f $TEMP.s < $file > $TEMP
		    PUT $TEMP $file; rm $TEMP.s
		endif
		if ("`grep imtool $file`" == "") then
		    echo '8a\' > $TEMP.s
		    echo '"ImTool"		imtool' >> $TEMP.s
		    sed -f $TEMP.s < $file > $TEMP
		    PUT $TEMP $file; rm $TEMP.s
		endif
	    endif
	endif

	# Make sure GTERM and IMTOOL links are in place.
	echo "directory $usrbin"
	cd $usrbin

	# Note that the following is resolved at runtime, hence the /usr/bin
	# is correct regardless of the physical directory.
	set exe = /usr/bin/suntools

	foreach i (gterm imtool)
	    if (-e $lbin/$i) then
		echo "remove old symbolic link $lbin/$i"
		if ($exec == yes) then
		    /bin/rm -f $lbin/$i
		endif
	    endif
	    if (-e $i) then
		if ("`$LS -l $i | grep $exe`" != "") then
		    echo "symbolic link $i is ok"
		else
		    echo "link $i exists but is invalid; unlink it"
		    if ($exec == yes) then
			/bin/rm -f $i
		    endif
		    echo "make symbolic link '$i' pointing to $exe"
		    if ($exec == yes) then
			/bin/ln -s $exe $i
		    endif
		endif
	    else
		echo "make symbolic link '$i' pointing to $exe"
		if ($exec == yes) then
		    ln -s $exe $i
		endif
	    endif
	end

    else
	# Do not install custom suntools and rootmenu files; leave these in
	# $hbin, but add links to $lbin for gterm, and imtool, pointing to
	# local iraf version of suntools.

	set file = $hbin/suntools.e
	if (! -e $file) then
	    echo "custom suntools executable $file is missing!!"
	endif

	echo "directory $lbin"
	cd $lbin
	foreach i (gterm imtool)
	    if (-e /usr/bin/$i) then
		echo "remove old symbolic link /usr/bin/$i"
		if ($exec == yes) then
		    rm -f /usr/bin/$i
		endif
	    endif
	    if (-e $i) then
		if ("`$LS -l $i | grep iraf`" != "") then
		    echo "symbolic link $i is ok"
		else
		    echo "link $i exists but is invalid; unlink it"
		    if ($exec == yes) then
			rm -f $i
		    endif
		    echo "make symbolic link '$i' -> $file"
		    if ($exec == yes) then
			ln -s $file $i
		    endif
		endif
	    else
		echo "make symbolic link '$i' -> $file"
		if ($exec == yes) then
		    ln -s $file $i
		endif
	    endif
	end
    endif
endif

# Common code for GTERM/IMTOOL installation.
# -------------------------------------------

xconfig:

# Make sure special device entries for the IMTOOL display server exist
# in /dev.

foreach i (/dev/imt1i /dev/imt1o)
    if (-e $i) then
	echo "imtool device entry $i is ok"
    else
	echo "make device entry $i for imtool"
	if ($exec == yes) then
	    if ($mach == linux || $mach == redhat || $mach == suse) then
		mknod $i p
	    else if ($mach == freebsd) then
		mkfifo $i
	    else
		mknod $i p
	    endif
	    chmod 777 $i
	endif
    endif
end

# The old /dev/imt1 entry is now just a link to /dev/imt1o.
set file = /dev/imt1
if (-e $file) then
    if ("`$LS -l $file | grep imt1o`" != "") then
	echo "imtool device entry $file is ok"
    else
	echo "make device entry $file for imtool"
	if ($exec == yes) then
	    rm -f $file
	    ln -s /dev/imt1o $file
	endif
    endif
else
    echo "make device entry $file for imtool"
    if ($exec == yes) then
	ln -s /dev/imt1o $file
    endif
endif

# Install the default IMTOOLRC frame buffer configuration file.  The path
# /usr/local/lib path hardwired in to imtool and cannot easily be changed, but
# if installation of the default imtoolrc in this directory is not possible,
# the file can be installed in each imtool user's login directory as .imtoolrc,
# or the environment variable IMTOOLRC can be defined in each imtool user's
# .login or .cshrc to define the path to the file.

# Verify imtoolrc link.
cd /usr/local/lib
set file1 = imtoolrc
set file2 = $iraf/dev/imtoolrc

# Verify or set the link.
if ("`$LS $file1`" == "$file1") then
    if ("`$LS -l $file1 | grep $file2`" != "") then
	echo "symbolic link $file1 is ok"
    else
	echo "link $file1 exists but is invalid; unlink $file1"
	if ($exec == yes) then
	    rm -f $file1
	endif
	echo "make symbolic link $file1 -> $file2"
	if ($exec == yes) then
	    rm -f $file1
	    ln -s $file2 $file1
	endif
    endif
else
    echo "make symbolic link $file1 -> $file2"
    if ($exec == yes) then
	rm -f $file1
	ln -s $file2 $file1
    endif
endif

# Make sure there are entries in the the termcap and graphcap files for xgterm
# and imtool.

set temp = `grep -l xgterm $iraf/dev/termcap | grep -v "^#"`
if ("$temp" == "") then
    echo "WARNING: no xgterm entry in termcap file"
else
    echo "termcap file is ok (contains xgterm entry)"
endif

set gcok = yes
set temp = `grep -l xgterm $iraf/dev/graphcap | grep -v "^#"`
if ("$temp" == "") then
    echo "WARNING: no xgterm entry in graphcap file"
    set gcok = no
endif
set temp = `grep -l imtool $iraf/dev/graphcap | grep -v "^#"`
if ("$temp" == "") then
    echo "WARNING: no imtool entry in graphcap file"
    set gcok = no
endif
if ($gcok == yes) then
    echo "graphcap file is ok (contains xgterm, imtool entries)"
endif

# Install or update the XGTERM and XIMTOOL manual pages.
if ($pciraf == no && -e /usr/man/mann) then
    cd /usr/man/mann
    set src = $iraf/unix/x11

    foreach i (xgterm ximtool)
	set file1 = $i.n
	set file2 = $src/$i.man

	# Install or update the manpage source file.
	if (-e $file1) then
	    cmp -s $file1 $file2
	    if ($status) then
		echo "update $i manual page"
		if ($exec == yes) then
		    cp $file2 $file1
		endif
	    else
		echo "$i manual page is up to date"
	    endif
	else
	    echo "install $i manual page in /usr/man/mann"
	    if ($exec == yes) then
		cp $file2 $file1
	    endif
	endif

	# Rerun catman if necessary.
	if (-e /usr/man/catl) then
	    set file2 = /usr/man/catl/$i.n
	    if (! -e $file2 || "`find $file1 -newer $file2 -print`" != "") then
		echo "rerun catman on section n"
		if ($exec == yes) then
		    /usr/etc/catman n
		endif
	    endif
	endif
    end
endif
