#!/bin/csh -f
#
# INSTALL -- Install the X11IRAF files and binaries.
#
#
#============================================================================

unset   noclobber
onintr  x11iraf_cleanup_
unalias cd cp cmp echo ln mv rm sed set grep ls chmod chown pwd touch sort

set     path  = (/sbin /usr/sbin /bin /usr/bin /usr/ucb /etc /usr/etc $path /usr /local/bin /opt/local/bin /local/bin /home/local/bin )


#============================================================================
# Global Variables.
#============================================================================

set hilite              = 1


#============================================================================
# Utility aliases.
#============================================================================
alias PUT      "mv -f \!*; chown $user \!$ "           # [MACHDEP]
alias BOLD_ON  "(if ($hilite) tput bold)"
alias BOLD_OFF "(if ($hilite) tput sgr0)"
alias SO_ON    "(if ($hilite) tput smso)"
alias SO_OFF   "(if ($hilite) tput rmso)"

alias DO_OK    "(echo -n '[ '; BOLD_ON; echo -n ' OK '; BOLD_OFF; echo ' ]')"
alias DO_WARN  "(echo -n '[ '; BOLD_ON; echo -n 'WARN'; BOLD_OFF; echo ' ]')"
alias DO_FAIL  "(echo -n '[ ';   SO_ON; echo -n 'FAIL';   SO_OFF; echo ' ]')"

alias MSG      "(echo -n '   ';BOLD_ON;echo -n '***  ';BOLD_OFF; echo \!*)"
alias MSGN     "(echo -n '   ';BOLD_ON;echo -n '***  ';BOLD_OFF; echo -n \!*)"
alias MSGB     "(echo -n '   ';BOLD_ON;echo -n '***  ';echo \!*; BOLD_OFF)"
alias MSGBN    "(echo -n '   ';BOLD_ON;echo -n '***  ';echo -n \!*;BOLD_OFF)"
alias ERRMSG   "(echo -n '   ';BOLD_ON;echo -n 'ERROR: ';BOLD_OFF; echo \!*)"
alias WARNING  "(echo -n '   ';BOLD_ON;echo -n 'WARNING: ';BOLD_OFF; echo \!*)"
alias NEWLINE  "(echo '')"

alias PROMPT   "(BOLD_ON; echo -n \!*; BOLD_OFF; echo -n ' (yes): ')"
alias PROMPT_N "(BOLD_ON; echo -n \!*; BOLD_OFF; echo -n ' (no): ')"


#============================================================================
# Get the current platform architecture.
#============================================================================

set UNAME=""
if (-e /usr/bin/uname) then
    set uname_cmd = /usr/bin/uname
    set UNAME=`/usr/bin/uname | tr '[A-Z]' '[a-z]'`
else if (-e /bin/uname) then
    set uname_cmd = /bin/uname
    set UNAME=`/bin/uname | tr '[A-Z]' '[a-z]'`
else
    set UNAME = "INDEF"
endif


switch ($UNAME) 
    case sunos:
        if (`$uname_cmd -m | cut -c2-` == "86pc") then
            set arch = "sunos"
        else
            setenv OSVERSION `uname -r | cut -c1`
            if ($OSVERSION == 5) then
                set arch = "ssun"
            else
                set arch = "sparc"
            endif
        endif
	breaksw
    case linux:
        if (`$uname_cmd -m` == "ppc") then
            if (-f /etc/redhat-release) then
	        set arch = "linuxppc"
	    else
                set arch = "mklinux"
	    endif
	else
            if (-f /etc/redhat-release) then
                set arch = "redhat"
            else if (-f /etc/SuSE-release) then
                set arch = "suse"
            else
                set arch = "linux"
            endif
        endif
	breaksw
    case darwin: 
	set arch = "macosx"
	breaksw
    case freebsd: 
	set arch = "freebsd"
	breaksw
    case hp-ux:   
	set arch = "hp700"
	breaksw
    case irix:    
	set arch = "irix"
	breaksw
    case irix64:  
	set arch = "irix"
	breaksw
    case aix:  	  
	set arch = "rs6000"
	breaksw
    case osf1:    
	set arch = "alpha"
	breaksw
    case ultrix:    
	set arch = "ultrix"
	breaksw
    default:	  
        echo "ERROR: No 'uname' command found to determine architecture."
bad_arch:
	NEWLINE
	echo -n "Enter architecture name: "
	set arch = "$<"
	if (! -e bin.$arch) then
	    echo -n "Invalid architecture, try again..."
	    goto bad_arch
	endif
	breaksw
endsw



#############################################################################
# Process command line options.
#############################################################################
set  exec	= yes

while ("$1" != "")
    switch ("$1")
    case -n:                            # no execute
        set exec = no
        breaksw
    case -hl:                           # disable highlighting
        set hilite = 0
        alias BOLD_ON   "(if ($hilite) tput bold)"
        alias BOLD_OFF  "(if ($hilite) tput sgr0)"
        alias SO_ON     "(if ($hilite) tput smso)"
        alias SO_OFF    "(if ($hilite) tput rmso)"
        breaksw
    case +hl:                           # enable highlighting
        set hilite = 1
        alias BOLD_ON   "(if ($hilite) tput bold)"
        alias BOLD_OFF  "(if ($hilite) tput sgr0)"
        alias SO_ON     "(if ($hilite) tput smso)"
        alias SO_OFF    "(if ($hilite) tput rmso)"
        breaksw
    case -h:                            # print help summary
        goto Usage
    default:
        echo "install: unknown argument $1"
        breaksw
    endsw

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

    
#############################################################################
# Check that we're running as 'root'
#############################################################################
if (-e /usr/bin/whoami) then
    set WHOAMI = `/usr/bin/whoami`
else if (-e /usr/ucb/whoami) then
    set WHOAMI = `/usr/ucb/whoami`
else if (-e /bin/whoami) then
    set WHOAMI = `/bin/whoami`
endif

if ($WHOAMI != "root" && $exec == "yes") then
  clear
  NEWLINE ; NEWLINE
  BOLD_ON
  echo "======================================================================"
  echo -n "WARNING"
  BOLD_OFF
  echo ": This script must be run as root for changes to take effect."
  echo "         If you decide to proceed,  the 'no-op' flag will be enabled"
  echo "         by default.   No changes will be made to the system  files,"
  echo "         however you will be able to see what the script does."
  BOLD_ON
  echo "======================================================================"
  BOLD_OFF
  NEWLINE
no_op_proc_:

  PROMPT "Proceed with a no-op installation anyway? "
  set ans = "$<"
  if ("$ans" != "") then
        if ($ans == 'n' || $ans == 'N' || $ans == 'no' || $ans == 'NO') then
	    exit 0
      endif
  endif
  NEWLINE
        
  set exec = no
endif

#############################################################################
# Print the banner message.
#############################################################################
clear
NEWLINE
BOLD_ON
echo "			X11IRAF V1.3 Installation"
echo "			========================="
BOLD_OFF
NEWLINE



#############################################################################
# Get the X11IRAF distribution file directory, assume it's the local
# directory but prompt if we don't find the files we expect.
#############################################################################

set xroot = "./"

do_root:
if (!(-e "$xroot/bin.$arch" && \
      -e "$xroot/lib.$arch" && \
      -e "$xroot/app-defaults")) then
        echo -n "Directory containing the X11IRAF distribution files ($xroot): "
        set xroot = "$<"
        if ("$xroot" != "") then
            if (! (-d "$xroot")) then
                echo "Directory $xroot not found."
                NEWLINE
                goto do_root
            endif
        else if (! (-e "$xroot/bin.$arch" && \
	    -e "$xroot/lib.$arch" && \
	    -e "$xroot/app-defaults")) then
                echo "    *** This directory doesn't appear to contains all the"
                echo "    *** X11IRAF distribution files for this platform."
                NEWLINE
		set xroot = "./"
            	goto do_root
        endif
endif


#############################################################################
# Install the binaries.
#############################################################################
set xgt = `which xgterm |& grep -i "^\/"`
if ($status == 0) then
    set d_xbin = $xgt:h
else
    set d_xbin = /usr/local/bin
endif

set BINFILES = \
	"ism_wcspix.e obmsh resize vximtool xgterm ximtool ximtool-alt xtapemon"


NEWLINE ; BOLD_ON
echo "Binary Installation:"
echo "--------------------"
NEWLINE ; BOLD_OFF


echo "   The local commands directory is the place where the commands"
echo "   such as 'xgterm' and 'ximtool' will be installed.  This should"
echo "   be a directory in a common search path such as /usr/local/bin."
if ("$WHOAMI" != "root") then
    echo "   Since you are not running as the root user the files may be"
    echo "   installed in a personal directory such as $HOME/bin"
endif

do_bin:
    NEWLINE
    echo -n "   Local commands directory ($d_xbin): "
    set xbin = "$<"
    if ("$xbin" == "") then
	set xbin = $d_xbin
    endif

# Check whether we're going to replace the existing binary or change
# the directory.  Use only xgterm as the test.
set xgt = `which xgterm |& grep -i "^\/"`
if ($status == 0) then
    if ("$xgt" != $xbin/xgterm) then
        NEWLINE
        echo "   *** Some X11IRAF commands are already installed at a"
        echo "   *** different location, e.g. $xgt"
        NEWLINE
db_pr:
        echo -n "   *** Would you like to delete these first (yes/no)? "
        set c = "$<"
    	if ("$c" != "") then
            if ($c == 'y' || $c == 'Y' || $c == 'yes' || $c == 'YES') then
	        NEWLINE
		set xgt_root = $xgt:h
		echo -n "   Removing old binaries from $xgt_root ...	"
		if ($exec == "yes") then
	    	    foreach i ($BINFILES)
		        /bin/rm -f $xgt_root/$i
		    end
		endif
		echo -n "	"
		DO_OK
            else
		NEWLINE ; NEWLINE
            endif
    	else
	    goto db_pr
    	endif
    endif
endif

# Create the bin directory if needed.
if (! -e $xbin && "$exec" == "yes") then
    mkdir $xbin
    if ($status != 0) then
        goto do_bin
    endif
endif

# Copy the bin files to the new directory.
NEWLINE
set errstat = 0
if (-e bin.$arch) then
    chdir bin.$arch
    echo -n "   Installing new binaries in $xbin ..."
    if ("$exec" == "yes") then
        foreach i (*)
	    cp $i $xbin >& /dev/null
	    if ($status == 1) then
		DO_FAIL
		ERRMSG "Cannot copy '$i' to $xbin"
		set errstat = 1
		break
	    endif
	end
    endif
    if ($errstat == 0) then
	DO_OK
    endif
    chdir ..
else
    echo "   *** No bin.$arch directory found, skipping installation."
endif


#############################################################################
# Install the man pages.
#############################################################################
if (-e /usr/local/man/man1) then
    set d_xman = /usr/local/man/man1
else if (-e /opt/local/man/man1) then
    set d_xman = /opt/local/man/man1
else if (-e /usr/man/man1) then
    set d_xman = /usr/man/man1
else if (-e /share/man/man1) then
    set d_xman = /share/man/man1
else if (-e /usr/share/man/man1) then
    set d_xman = /usr/share/man/man1
else 
    set d_xman = "none found"
endif

NEWLINE ; NEWLINE
BOLD_ON
echo "Manual Page Installation:"
echo "-------------------------"
BOLD_OFF
NEWLINE

if ("$WHOAMI" != "root") then
    echo "   Since you are not running as the root user you cannot install"
    echo "   the manual pages for the X11IRAF tasks.  These can still be"
    echo "   read by either formatting the pages yourself with a command"
    echo "   such as "
    echo "		% nroff -man -tbl xgterm.1 "
    NEWLINE
    echo "   or you can define the MANPATH environment variable to search"
    echo "   this directory.  For example,"
    NEWLINE
    echo '	setenv MANPATH $MANPATH'":$cwd/man "
else
    echo "   Man pages are available for each of the X11IRAF tasks, some"
    echo "   tasks will also provide online documentation.  Manual pages"
    echo "   may be installed either in the main system directory or in "
    echo "   a local directory as long as that is accessible through a"
    echo "   MANPATH environment variable."
endif

do_man:
    NEWLINE
    echo -n "   Local man page directory ($d_xman): "
    set xman = "$<"
    if ("$xman" == "") then
	set xman = $d_xman
    endif

# Create the man directory if needed.
if (! -e $xman && "$exec" == "yes") then
    mkdir $xman
    if ($status != 0) then
        goto do_man
    endif
endif

# Copy the man pages to the new directory.
NEWLINE
set errstat = 0
if (-e man) then
    chdir man
    echo  -n "   Installing man pages at $xman ..."
    if ("$exec" == "yes") then
        foreach i (*)
	    cp $i $xman >& /dev/null
	    if ($status == 1) then
		DO_FAIL
		ERRMSG "Cannot copy '$i' to $xman"
		set errstat = 1
		break
	    endif
	end
    endif
    if ($errstat == 0) then
	DO_OK
    endif
    chdir ..
else
    echo "   *** No 'man' directory found, skipping installation."
endif


#############################################################################
# Install the app-defaults files.
#############################################################################
if (-d /usr/lib/X11/app-defaults) then
    set d_xapp = /usr/lib/X11/app-defaults
else if (-d /etc/X11/app-defaults) then
    set d_xapp = /usr/lib/app-defaults
else if (-d /usr/X11R6/lib/app-defaults) then
    set d_xapp = /usr/X11R6/lib/app-defaults
else if (-d /usr/X11R6/etc/app-defaults) then
    set d_xapp = /usr/X11R6/etc/app-defaults
else if (-d /usr/openwin/lib/app-defaults) then
    set d_xapp = /usr/openwin/lib/app-defaults
else if (-d /usr/lib/app-defaults) then
    set d_xapp = /usr/lib/app-defaults
else 
    set d_xapp = "none found"
endif

NEWLINE ; NEWLINE
BOLD_ON
echo "Application Default Resource Installation:"
echo "------------------------------------------"
BOLD_OFF
NEWLINE

echo "   The app-defaults directory contains the default X resources for"
echo "   the XGterm task.  This is not required for the program to work"
echo "   but provides for more readable menu entries, fonts, etc."
echo "   The directory is normally part of the X11 tree but may be any "
echo "   directory specified in an XFILESEARCHPATH environment variable."

do_app:
    NEWLINE
    echo -n "   System app-defaults directory ($d_xapp): "
    set xapp = "$<"
    if ("$xapp" == "") then
	set xapp = $d_xapp
    endif

# Create the app-defaults directory if needed.
if (! -e $xapp && "$exec" == "yes") then
    mkdir $xapp
    if ($status != 0) then
        goto do_app
    endif
endif

# Copy the app-defaults files to the new directory.
NEWLINE
set errstat = 0
if (-e app-defaults) then
    chdir app-defaults
    echo -n "   Installing app-defaults in $xapp ..."
    if ("$exec" == "yes") then
        foreach i (*)
	    cp $i $xapp >& /dev/null
	    if ($status == 1) then
		DO_FAIL
		ERRMSG "Cannot copy '$i' to $xapp"
		set errstat = 1
		break
	    endif
	end
    endif
    if ($errstat == 0) then
	DO_OK
    endif
    chdir ..
else
     echo "   *** No 'app-defaults' directory found, skipping installation."
endif



#############################################################################
# Install the (optional) CDL library.
#############################################################################
set d_xlib = /usr/local/lib
set d_xinc = /usr/local/include

NEWLINE ; NEWLINE
BOLD_ON
echo "Client Display Library (CDL) Installation:"
echo "------------------------------------------"
BOLD_OFF
NEWLINE

echo "   The CDL is an interface for C/Fortran programs to display images"
echo "   to XImtool and compatible display servers.  This library is not"
echo "   required in the runtime system for XImtool but can be useful for"
echo "   developing local applications needing a display capability."
echo "   The local lib directory is the place where the CDL library file "
echo "   will be installed, the local include directory is where the needed"
echo "   nclude files for the library will be installed."

do_lib:
    NEWLINE
    echo -n "   Would you like to install this library? (yes): "
    set ans = "$<"
    set ans = `echo $ans | tr A-Z a-z`
    if ("$ans" != "n" && "$ans" != "no") then
        NEWLINE
        echo -n "   Local lib directory ($d_xlib): "
        set xlib = "$<"
        if ("$xlib" == "") then
	    set xlib = $d_xlib
        endif

        # Create the local lib directory if needed.
        if (! -e $xlib && "$exec" == "yes") then
	    mkdir $xlib
	    if ($status != 0) then
    	        goto do_lib
	    endif
        endif

        # Copy the library files to the new directory.
        NEWLINE
	set errstat = 0
        if (-e lib.$arch) then
            chdir lib.$arch
	    echo -n "   Installing CDL libraries in $xlib ..."
	    if ("$exec" == "yes") then
                foreach i (libcdl.a)
	            cp $i $xlib >& /dev/null
	    	    if ($status == 1) then
			DO_FAIL
			ERRMSG "Cannot copy '$i' to $xlib"
			set errstat = 1
			break
		    endif
	        end
            endif
	    if ($errstat == 0) then
	        DO_OK
    	    endif
            chdir ..
        else
	    echo "   *** No lib.$arch directory found, skipping installation."
        endif


	# Install the include files.
    do_inc:
        NEWLINE
	echo -n "   Local include directory ($d_xinc): "
	set xinc = "$<"
	if ("$xinc" == "") then
	    set xinc = $d_xinc
	endif

        # Create the local lib directory if needed.
        if (! -e $xinc && "$exec" == "yes") then
	    mkdir $xinc
	    if ($status != 0) then
    	        goto do_inc
	    endif
        endif

        # Copy the include files to the new directory.
        NEWLINE
	set errstat = 0
        if (-e include) then
            chdir include
	    echo -n "   Installing include files in $xinc ..."
	    if ("$exec" == "yes") then
                foreach i (cdl*)
	            cp $i $xinc >& /dev/null
	    	    if ($status == 1) then
			DO_FAIL
			ERRMSG "Cannot copy '$i' to $xlib"
			set errstat = 1
			break
		    endif
	        end
            endif
	    if ($errstat == 0) then
		DO_OK
	    endif
            chdir ..
        else
	    echo "   *** No 'include' directory found, skipping installation."
        endif

    else
	echo "    Skipping CDL library installation."
    endif


NEWLINE
echo "Installation complete."
NEWLINE

x11iraf_cleanup_:

# Normal exit.
exit 0

Usage:
    echo "Usage:    install [-n] [-h] [-hl]"
    echo ""
    echo "    where -n      # no execute"
    echo "          -h      # print this help summary"
    echo "          +hl     # enable highlighted text"
    echo "          -hl     # disable highlighted text"
    exit 0


syntax highlighted by Code2HTML, v. 0.9.1