#!/bin/sh
# 
#  INSTALL.SH -- Install IRAF on the local machine, either as a system-wide
#  facility, and/or for personal use.  Root privileges are only required for
#  a system installation. 
# 
#   Usage:
#   		% install [opts]
# 
#   Where [opts] include:
# 
#     -h, --help                  # print help
#     --version                   # print script version
# 
#     -n, --noop                  # no execute
#     -s, --system                # do a system install (needs root)
#     -C, --csh              	  # install C-shell scripts
# 
#     -t, --term <term>           # set default terminal type
#     -b, --bindir <dir>          # set local bin directory
#     -c, --cache <dir>           # set cache directory
#     -i, --imdir <dir>           # set image directory
#     -m, --mach <arch>           # set architecture
# 
#     -C, --oldcache <dir>        # set old cache directory
#     -I, --oldimdir <dir>        # set old image directory
#     -R, --oldroot <dir>         # set old iraf root directory
# 
# 


# Initialize script variables.

export iraf=$PWD
. "$iraf/unix/hlib/util.sh"
mach=$("${iraf}/unix/hlib/irafarch.sh")	# current platform architecture
hmach=$("${iraf}/unix/hlib/irafarch.sh" -hsi)	# current HSI architecture
if [ -z "$IRAFARCH" ]; then
    IRAFARCH=$mach
fi

VERSION="v2.16"                         # current IRAF version

defterm="xgterm"			# default terminal type
do_system=0				# system or personal install?
do_csh=0                                # Install csh scripts?
err_seen=0				# have errors been seen?
err_count=0				# error count
exec="yes"				# take action?
old_links=0				# use old link method?
extn="sh"				# shell script extension

LS="/bin/ls"				# directory listing command
TEMP="/tmp/iraf_install.$$"		# temp output file


imdir=""				# image directory
cache=""				# cache directory
myterm=""			        # terminal type

p_imdir=""				# <param> image directory
p_cache=""				# <param> cache directory
p_lbin=""				# commands directory
o_iraf=""				# old iraf root directory
o_imdir=""				# old image directory
o_cache=""				# old cache directory



#  INSTALL_HELP -- Print script help.

install_help() {
    ECHO ""
    ECHO "  Usage:"
    ECHO "  		% install [opts]"
    ECHO ""
    ECHO "  Where [opts] include:"
    ECHO ""
    ECHO "    -h, --help                # print help"
    ECHO "    --version                 # print script version"
    ECHO ""
    ECHO "    -n, --noop                # no execute"
    ECHO "    -s, --system              # do a system install (needs root)"
    ECHO "    -C, --csh                 # install C-shell scripts"
    ECHO ""
    ECHO "    -t, --term <term>         # set default terminal type"
    ECHO "    -b, --bindir <dir>        # set local bin directory"
    ECHO "    -c, --cache <dir>         # set cache directory"
    ECHO "    -i, --imdir <dir>         # set image directory"
    ECHO "    -m, --mach <arch>         # set architecture"
    ECHO ""
    ECHO "    -C, --oldcache <dir>      # set old cache directory"
    ECHO "    -I, --oldimdir <dir>      # set old image directory"
    ECHO "    -R, --oldroot <dir>       # set old iraf root directory"
    ECHO ""
    ECHO ""
}


#=============================================================================
#  Process command line arguments.  The defaults are set to empty strings
#  in order to allow them to be defined first on the command-line.  In 
#  addition, if the 'sysinstall" option is used it will change how we 
#  look for installation directories.
#=============================================================================

while [ -n "$1" ] ; do
    case "$1" in
    "-h"|"-help"|"--help")                   	# print script help
        install_help
	exit 0
        ;;
    "-version"|"--version")                   	# print script version
        echo "$VERSION"
	exit 0
        ;;


    "-n"|"-noop"|"--noop")                   	# no execute
        exec=no
        ;;
    "-s"|"-system"|"--system")                 	# do a system-wide install?
        do_system=1
        ;;
    "-C"|"-csh"|"--csh")                 	# Install csh scripts?
        do_csh=1
        ;;
    "-t"|"-term"|"--term")                 	# set default terminal type.
	myterm=$2 ; 	shift
        ;;
    "-b"|"-bindir"|"--bindir")                 	# set local bin directory
	p_lbin=$2 ; 	shift
        ;;
    "-c"|"-cache"|"--cache")               	# set cache directory
	p_cache=$2 ; 	shift
        ;;
    "-i"|"-imdir"|"--imdir")                  	# set image directory
	p_imdir=$2 ; 	shift
        ;;
    "-m"|"-mach"|"--mach")                   	# set architecture
	mach=$2 ; 	shift
        ;;
    "-C"|"-oldcache"|"--oldcache")              # set old cache directory
	o_cache=$2 ; shift
        ;;
    "-I"|"-oldimdir"|"--oldimdir")              # set old image directory
	o_imdir=$2 ; 	shift
        ;;
    "-R"|"-oldroot"|"--oldroot")                # set old iraf root directory
	o_iraf=$2 ; 	shift
        ;;
    *)
        MSG "Unknown option '$1'"
	exit 1
        ;;
    esac

    if [ "$2" = "" ]; then
        break
    else
        shift
    fi
done



#=============================================================================
#  Get the values of o_iraf and o_imdir from the current mkiraf.*sh file, if
#  not already otherwise defined.  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.
#=============================================================================

mkiraf=${iraf}/unix/hlib/mkiraf.sh

if [ "$o_iraf" = "" ]; then
   o_iraf=$(grep "^iraf=" "$mkiraf" | sed -e "s+iraf=++" -e 's+"++g')
fi
o_iraf=$(echo "$o_iraf"  | sed -e 's+/\(["]*\)$+\1+')

if [ "$o_imdir" = "" ]; then
   o_imdir=$(grep "^imdir=" "$mkiraf" | sed -e "s+imdir=++" -e 's+"++g')
fi
o_imdir=$(echo "$o_imdir" | sed -e 's+/\(["]*\)$+\1+')

if [ "$o_cache" = "" ]; then
   o_cache=$(grep "^cachedir=" "$mkiraf" | sed -e "s+cachedir=++" -e 's+"++g')
fi
o_cache=$(echo "$o_cache" | sed -e 's+/\(["]*\)$+\1+')



#=============================================================================
#  User Check
#
#  If we're doing a private install, initialize the $HOME/.iraf directory.
#=============================================================================

# Get the current user name.
WHOAMI=$(whoami)

if [ "$WHOAMI" = "root" ]; then
  if [ "$do_system" = 0 ]; then
    do_proceed="yes"
    while [ "$do_proceed" = "yes" ]; do
	BOLD_ON
	ECHO "You are running as the root user: "
	PROMPT "Install IRAF for all users on this machine?"
	read ans

	do_proceed="no"
	if [ -n "$ans" ]; then
  	  if [ "$ans" = "" ] || [ "$ans" = "y" ] || [ "$ans" = "yes" ]; then
    	    ECHO "Proceeding with a system-wide install on this machine ..."
	    do_system=1
    	    do_proceed="no"
  	  elif [ "$ans" = "quit" ] || [ "$ans" = "q" ]; then
    	    exit 0
  	  elif [ "$ans" = "no" ] || [ "$ans" = "n" ]; then
    	    ECHO "Proceeding with a personal install for 'root' ...."
	    do_system=0
    	    do_proceed="no"
  	  elif [ "$ans" = "help" ] || [ "$ans" = "h" ] || [ "$ans" = "?" ]; then
    	    NEWLINE
	    MSG "If you proceed, IRAF will be installed on this machine for all"
	    MSG "users.  This means that files and links will be put in the "
	    MSG "system directories such as /usr/include and a local bin dir,"
	    MSG "doing so requires root permission.  If you say 'no', then IRAF"
	    MSG "will be installed as a personal system for the user."
	    NEWLINE
            MSG  "Type <cr> to continue, or 'q' to quit to exit."
    	    NEWLINE
    	    do_proceed="yes"
  	  else 
    	    ECHO "Huh?"
    	    do_proceed="yes"
  	  fi
	else
    	    ECHO "Proceeding with a system-wide install on this machine ..."
	    do_system=1
    	    do_proceed="no"
	fi
    done
  fi
fi

if [ "$do_system" = 0 ]; then
    if [ ! -e "$HOME/.iraf" ]; then
        NEWLINE
        ECHO "Initializing $HOME/.iraf directory ....."
        NEWLINE

        mkdir "$HOME/.iraf"
        (cd "$HOME/.iraf" && mkdir imdir cache bin)
    fi
fi



#=============================================================================

clear
NEWLINE
BOLD_ON; 
ECHO "		===================================="
ECHO "		       IRAF $VERSION Installation"
ECHO "		===================================="
BOLD_OFF
NEWLINE

ECHO "       Welcome to the IRAF installation script.   This script will first"
ECHO "  prompt you for several needed path names.   Once  the  installation is"
ECHO "  complete, you will be allowed to do some minimal system configuration."

# Print a quick usage summary.
NEWLINE
ECHO -n "  For each prompt: hit "
BOLD_ON ; ECHO -n "<CR>"; BOLD_OFF;
ECHO -n " to accept the default value, "
BOLD_ON ; ECHO -n "'q'" ; BOLD_OFF
ECHO ' to quit,'

ECHO -n "  or "
BOLD_ON ; ECHO -n "'help'"; BOLD_OFF
ECHO -n " or ";
BOLD_ON ; ECHO -n "'?'"; BOLD_OFF
ECHO -n " to print an explanation of the prompt."
NEWLINE
NEWLINE



#=============================================================================
# Prompt the user for needed paths.
#=============================================================================

NEWLINE
BOLD_ON
ECHO "========================================================================"
ECHO "=====================  Query for System Settings  ======================"
ECHO "========================================================================"
BOLD_OFF
NEWLINE



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

# Use the current directory.
iraf=$(pwd)


#=============================================================================
# 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

  imdir_prompt="yes"

  if [ "$p_imdir" ]; then
      imdir=$p_imdir
      imdir_prompt="no"
  elif [ "$do_system" = 0 ]; then
      d_imdir="$HOME/.iraf/imdir"
  elif [ -d "$o_imdir" ]; then
      d_imdir=$o_imdir
  elif [ -d "/iraf" ]; then
      d_imdir=/iraf/imdirs
  elif [ -d "/home/iraf" ]; then
      d_imdir=/home/iraf/imdirs
  elif [ -d "/usr/local/iraf" ]; then
      d_imdir=/usr/local/iraf/imdirs
  else
      d_imdir="/tmp"
  fi

  while [ "$imdir_prompt" = "yes" ]; do

    imdir_prompt="no"
    BOLD_ON ; ECHO -n "Default root image storage directory " ; BOLD_OFF
    ECHO -n "($d_imdir): "
    read imdir

    if [ "$imdir" = "" ]; then
        imdir="$d_imdir"
        imdir_prompt="no"
    elif [ "$imdir" = "quit" ] || [ "$imdir" = "q" ]; then
        exit 0
    elif [ "$imdir" = "help" ] || [ "$imdir" = "h" ] || [ "$imdir" = "?" ]; then
        NEWLINE
            MSG "The root imdir directory is the default image storage dir"
            MSG 'for OIF images (i.e. the ".imh" format) used by all users on'
            MSG "this system.   Individual user dirs will be created as needed."
            MSG "It should be some large data disk on the machine which has a"
            MSG "regular backup, scratch or tmp disks should be avoided or data"
            MSG "may be lost."
            MSG ""
            MSG 'The "HDR$" syntax should not be used at this stage, please'
            MSG 'edit the hlib$mkiraf.sh script after installation if you wish'
            MSG "to make this the default."
        export imdir=$d_imdir
        imdir_prompt="yes"
    fi

    # Cannot have iraf and imdir the same.
    if [ "$imdir" = "$iraf" ]; then
        NEWLINE
            MSG  "The definition of imdir cannot be the same as the iraf"
            MSG  "root, please choose a different directory.  Ideally this"
            MSG  "should be some large data area on your system or a user"
            MSG  "data area such as /home, /users, /u1, etc."
        NEWLINE
        NEWLINE
        imdir_prompt="yes"
    fi
  done
fi


#=============================================================================
# Set $cache, the default user file cache root directory.
#=============================================================================

if [ "$cache" = "" ]; then

  cache_prompt="yes"

  if [ "$p_cache" ]; then
      cache=$p_cache
      cache_prompt="no"
  elif [ "$do_system" = 0 ]; then
      d_cache="$HOME/.iraf/cache"
  elif [ -d "/iraf" ]; then
      d_cache="/iraf/cache"
  elif [ -d "/home/iraf" ]; then
      d_cache="/home/iraf/cache"
  elif [ -d "/usr/local/iraf" ]; then
      d_cache="/usr/local/iraf/cache"
  else
      d_cache="/tmp"
  fi

  while [ "$cache_prompt" = "yes" ]; do

    cache_prompt="no"
    BOLD_ON ; ECHO -n "Default root cache directory " ; BOLD_OFF
    ECHO -n "($d_cache): "
    read cache
    if [ "$cache" = "" ]; then
        cache="$d_cache"
        cache_prompt="no"
    elif [ "$cache" = "quit" ] || [ "$cache" = "q" ]; then
        exit 0
    elif [ "$cache" = "help" ] || [ "$cache" = "h" ] || [ "$cache" = "?" ]; then
        NEWLINE
            MSG "The root cache directory is the default storage directory for"
            MSG "URL-referenced files. Individual user dirs will be created as"
            MSG "needed. It should be some large data disk on the machine "
            MSG "which has a regular backup, scratch or tmp disks should be"
            MSG "avoided or data may be lost."
            MSG ""
        NEWLINE
        export cache=$d_cache
        cache_prompt="yes"
    fi

    # Cannot have iraf and cache the same.
    if [ "$cache" = "$iraf" ]; then
        NEWLINE
            MSG  "The definition of cache cannot be the same as the iraf"
            MSG  "root, please choose a different directory.  Ideally this"
            MSG  "should be some large data area on your system or a user"
            MSG  "data area such as /home, /users, /u1, etc."
        NEWLINE
        NEWLINE
        cache_prompt="yes"
    fi
  done
fi



#=============================================================================
# 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

  lbin_prompt="yes"

  # Look around and come up with a likely candidate directory.
  if [ "$p_lbin" ]; then
      lbin=$p_lbin
      lbin_prompt="no"
  elif [ "$do_system" = 0 ]; then
      d_lbin="$HOME/.iraf/bin/"
  elif [ -d "/usr/local/bin" ]; then
      d_lbin="/usr/local/bin"
  elif [ -d "/opt/local/bin" ]; then
      d_lbin="/opt/local/bin"
  elif [ -d "/local/bin" ]; then
      d_lbin="/local/bin"
  else
      d_lbin="/usr/bin"
  fi

  while [ "$lbin_prompt" = "yes" ]; do

    lbin_prompt="no"
    BOLD_ON ; ECHO -n "Local unix commands directory " ; BOLD_OFF
    ECHO -n "($d_lbin): "
    read lbin
    if [ "$lbin" = "" ]; then
        lbin="$d_lbin"
        lbin_prompt="no"
    elif [ "$lbin" = "quit" ] || [ "$lbin" = "q" ]; then
        exit 0
    elif [ "$lbin" = "help" ] || [ "$lbin" = "h" ] || [ "$lbin" = "?" ]; then
        NEWLINE
            MSG "The local bin directory is the system directory into which the"
            MSG "iraf commands (e.g. cl, mkiraf, mkpkg, etc) will be installed"
            MSG "as symlinks to files in the iraf tree. This should be a common"
            MSG "dir such as /usr/local/bin which will likely be found in every"
            MSG "user's path."
        NEWLINE
        export lbin=$d_lbin
        lbin_prompt="yes"
    fi

    # Create the local bin directory if it doesn't exist?
    if [ ! -e $lbin ]; then
        if [ "$do_system" = 0 ]; then
	    ans="yes"
        else
            BOLD_ON ; 
	    ECHO -n "    Sorry, but $lbin does not exist, create it? "
	    BOLD_OFF
            read ans
        fi
        if [ "$ans" = "" ] || [ "$ans" = "y" ] || [ "$ans" = "yes" ]; then
            ECHO "  Creating directory $lbin..."
            if [ "$exec" = "yes" ]; then
                mkdir $lbin
                if [ ! -e $lbin ]; then
                    ERRMSG  "Cannot create $lbin, please retry..."
                    export lbin=$d_lbin
                    lbin_prompt="yes"
                fi
            fi
        else
            lbin_prompt="yes"
        fi
        NEWLINE
    fi
  done
fi
bindir=$lbin



#  Prompt for the terminal to use in a private installation.

if [ "$do_system" = 0 ]; then
    if [ -z "$myterm" ] ; then
	BOLD_ON
	ECHO    "Terminal types: xgterm, xtermjh, xterm, etc."
	ECHO -n 'Enter default terminal type ('
	BOLD_OFF
	ECHO -n "$defterm"
	BOLD_ON
	ECHO -n '): '
	BOLD_OFF

	read myterm
	if [ -z "$myterm" ]; then
            myterm=$defterm
	fi
    fi
fi



#===============================================================================
# Determine iraf root directory
#===============================================================================


#===============================================================================
# Determine the old 'iraf', 'imdir' and 'cachedir' for editing.
#===============================================================================


#===============================================================================
# Verify the machine type.
#===============================================================================

if [ -e /usr/bin/uname ]; then
    uname_cmd="/usr/bin/uname"
elif [ -e /bin/uname ]; then
    uname_cmd="/bin/uname"
else
    WARNING  "No 'uname' command found to determine architecture."
    exit 1
fi

export  UNAME=$($uname_cmd    | tr '[:upper:]' '[:lower:]')
if [ "$UNAME" = "sunos" ]; then
    export  UNAME_M=$($uname_cmd -m | cut -c2- | tr '[:upper:]' '[:lower:]')
else
    export  UNAME_M=$($uname_cmd -m | tr '[:upper:] ' '[:lower:]_')
fi
export  OSVERSION=$($uname_cmd -r | cut -c1)





# ============================================
# The following is partially system dependent.
# ============================================

# Set the BINDIRS pathnames - directories where the HSI executables go.
host="$iraf/unix"
hbin="$iraf/unix/bin.$hmach"
hlib="$iraf/unix/hlib"
fbin="$iraf/bin"

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

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

BINDIRS="$hbin $hlib $fbin $host"


# The following file lists are partially system dependent.
PATHFILES="mkiraf.${extn} cl.${extn} ecl.${extn} setup.sh setup.csh"
MODEFILES="cl.${extn} fc.${extn} mkiraf.${extn} mkfloat.${extn} mkmlist.${extn} $host/reboot 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 irafarch.${extn}"
LINKFILES="ecl.${extn} cl.${extn} mkiraf.${extn} mkmlist.${extn} generic.e mkpkg.e rmbin.e rmfiles.e rtar.e sgidispatch.e wtar.e rpp.e xpp.e xyacc.e xc.e"
LTARGETS="${hlib}/ecl.${extn} ${hlib}/cl.${extn} ${hlib}/mkiraf.${extn} ${hlib}/mkmlist.${extn} ${hbin}/generic.e ${hbin}/mkpkg.e ${hbin}/rmbin.e ${hbin}/rmfiles.e ${hbin}/rtar.e ${hbin}/sgidispatch.e ${hbin}/wtar.e ${hbin}/rpp.e ${hbin}/xpp.e ${hbin}/xyacc.e ${hbin}/xc.e"
CMDLINKS="ecl cl mkiraf mkmlist generic mkpkg rmbin rmfiles rtar sgidispatch wtar rpp xpp xyacc xc irafarch"






#===============================================================================
# Echo parameters, get go-ahead.
#===============================================================================

NEWLINE
BOLD_ON
ECHO "========================================================================"
ECHO "=====================  Verifying System Settings  ======================"
ECHO "========================================================================"
BOLD_OFF




NEWLINE
BOLD_ON; ECHO -n "Hostname      = "; \
    BOLD_OFF; ECHO "$(hostname)" | awk '{printf("%-20.20s  ", $1)}'
BOLD_ON; ECHO -n "OS version    = "; \
    BOLD_OFF; ECHO "$($uname_cmd) $($uname_cmd -r)"
BOLD_ON; ECHO -n "Architecture  = "; \
    BOLD_OFF; ECHO "$mach" | awk '{printf("%-20s  ", $1)}'
BOLD_ON; ECHO -n "HSI arch      = "; \
    BOLD_OFF; ECHO "$hmach" | awk '{printf("%-20s\n", $1)}'
BOLD_ON; ECHO -n "Old iraf root = "; \
    BOLD_OFF; ECHO "$o_iraf" | awk '{printf("%-20s  ", $1)}'
BOLD_ON; ECHO -n "New iraf root = "; \
    BOLD_OFF; ECHO "$iraf" | awk '{printf("%-20s\n", $1)}'
BOLD_ON; ECHO -n "Old imdir     = "; \
    BOLD_OFF; ECHO "$o_imdir" | awk '{printf("%-20s  ", $1)}'
BOLD_ON; ECHO -n "New imdir     = "; \
    BOLD_OFF; ECHO "$imdir" | awk '{printf("%-s\n", $1)}'
BOLD_ON; ECHO -n "Old cache     = "; \
    BOLD_OFF; ECHO "$o_cache" | awk '{printf("%-20s  ", $1)}'
BOLD_ON; ECHO -n "New cache     = "; \
    BOLD_OFF; ECHO "$cache" | awk '{printf("%-s\n", $1)}'
NEWLINE

BOLD_ON; ECHO -n "Local bin dir = "; \
    BOLD_OFF; ECHO "$bindir" | awk '{printf("%-20s\n", $1)}'
NEWLINE ; NEWLINE




#=============================================================================
# Prompt for the go-ahead ...
#=============================================================================

do_proceed="yes"
while [ "$do_proceed" = "yes" ]; do
  PROMPT "Proceed with installation? "
  read ans

  do_proceed="no"
  if [ "$ans" = "" ] || [ "$ans" = "y" ] || [ "$ans" = "yes" ]; then
    NEWLINE
    do_proceed="no"
  elif [ "$ans" = "quit" ] || [ "$ans" = "q" ]; then
    exit 0
  elif [ "$ans" = "no" ] || [ "$ans" = "n" ]; then
    exit 0
  elif [ "$ans" = "help" ] || [ "$ans" = "h" ] || [ "$ans" = "?" ]; then
    NEWLINE
        MSG  "If you proceed, the system will be installed on this machine."
        MSG  "This means that command links will be placed in the local bin"
        MSG  "directory, needed system files will be created, and the iraf"
        MSG  "root path will be edited into key files.  Stopping at this stage"
        MSG  "will have no side effects on your system or the iraf files."
        MSG  "Type <cr> to continue, or 'q' to quit to exit the installation."
    NEWLINE
    do_proceed="yes"
  else 
    ECHO "Huh?"
    do_proceed="yes"
  fi
done



# Constraints:
#
#	- validate iraf root directory
#	- imdir cannot be under iraf root directory
#	- cache cannot be under iraf root directory
#	- check for write permission on iraf directory
#
#	- if [ sysinstall ]; then
#	    - check we're running with root/sudo permissions
#	    - check file permissions to $iraf, $iraf/unix, $iraf/unix/hlib
#	  fi
#
#	- check that bin directory exists
#	- delete existing command directory


#  Do the actual installation.  This involves:
#       
#       1) Editing the $iraf path into system files
#       2) Creating the <iraf.h> link
#       3) Creating the system command links
#       4) Creating the image directory (imdir)
#       5) Creating the cache directory (cache)
#       6) Tape setup (modes on alloc.e and /dev tape devices)
#       7) Graphics/Display file installation/setup


#=============================================================================
# See whether there is an existing commands dir we need to delete.
#=============================================================================

NEWLINE
BOLD_ON
ECHO "========================================================================"
ECHO "=========================  Begin Installation  ========================="
ECHO "========================================================================"
BOLD_OFF

NEWLINE
NEWLINE
ECHO -n "Checking for existing commands directory...                    "
cl_found=0
clpath=""
paths=$(echo "$PATH" | sed -e "s/:/ /g")
for d in ${paths}; do
    if [ -e "$d/cl" ]; then
        cl_found=1
        clpath="$d/cl"
        break
    fi
done
if [ "$do_system" = 0 ]; then
  cl_found=0
fi
if [ "$cl_found" = 1 ]; then
  o_lbin=${clpath%/*}

  if [ "$o_lbin" != "$lbin" ]; then
      DO_WARN
      NEWLINE
          MSG  "IRAF commands were found in the directory:"
          MSG  ""
          MSG  "      $o_lbin"
          MSG  ""
          MSG  "    These commands may conflict with the commands now being"
          MSG  "installed in:  '$lbin'"
          MSG  ""

      del_cmd_="yes"
      while [ "$del_cmd_" = "yes" ]; do
        PROMPT "Do you want to delete commands in the old directory? "
        read ans

	del_cmd_="no"
        if [ -n "$ans" ]; then
	  if [ "$ans" = "y" ] || [ "$ans" = "yes" ]; then
            NEWLINE
            for i in ${CMDLINKS}; do            # remove the iraf commands
                file=$o_lbin/$i
                if [ -e "$file" ]; then
                    MSG  "Deleting old command $file ..."
                    if [ "$exec" = "yes" ]; then
                        RM "$file"	>> /dev/null 2>&1
                    fi
                fi
            done
	    del_cmd_="no"
          elif [ "$ans" = "quit" ] || [ "$ans" = "q" ]; then
            exit 1
          elif [ "$ans" = "no" ] || [ "$ans" = "n" ]; then
	    del_cmd_="no"
          elif [ "$ans" = "help" ] || [ "$ans" = "h" ] || [ "$ans" = "?" ]; then
            NEWLINE
                MSG "Multiple commands such as 'cl' or 'mkiraf' on a machine"
                MSG "may cause errors (such as 'command not found' due to an"
                MSG "invalid link), or confusions as to which version of iraf"
                MSG "is being run if the old link is still valid.  This is"
                MSG "because the command being used depends on the order in"
                MSG 'which the directories occur in the users "$path" environ-'
                MSG "ment variable (which may vary by user)."
                MSG ""
                MSG "It is recommended there be only one iraf command directory"
                MSG "on a given system, other methods can be used to start a"
                MSG "different IRAF installation.  This script will not auto-"
                MSG "matically remove those links, and will only correct the"
                MSG "path is the local bin directory is the same as before."
                MSG ""
                MSG "Type 'q' to quit and rerun the install script to specify"
                MSG "a different local bin directory, 'yes' to remove the old"
                MSG "links, and 'no' to leave the old commands around."
                MSG ""
            NEWLINE
	    del_cmd_="yes"
          else
            NEWLINE
            for i in ${CMDLINKS}; do            # remove the iraf commands
                file=$o_lbin/$i
                if [ -e "$file" ]; then
                    MSG  "Deleting old command $file ..."
                    if [ "$exec" = "yes" ]; then
                        RM "$file"	>> /dev/null 2>&1
                    fi
                fi
            done
	    del_cmd_="no"
          fi
        fi
        NEWLINE
      done
    else
        DO_OK
    fi

else
    DO_OK
fi


#  Step 1)  Edit the $iraf path into the system files

NEWLINE
BOLD_ON
ECHO "                             Editing Paths"
ECHO "                             -------------"
BOLD_OFF

# Edit the $iraf pathname in the .login file for user 'iraf'.
if [ "$do_system" = 0 ]; then
  ECHO -n "Editing the user login/rc files ...                            "
  pfiles="$HOME/.bashrc $HOME/.profile $HOME/.bash_profile $HOME/.bash_login $HOME/.cshrc $HOME/.login"
fi
for file in ${pfiles}; do
    if [ -e "$file" ]; then
	if [ "$exec" = "yes" ]; then
            sed -i.bak -e "s+$o_iraf+$iraf+" "$file"
	    RM "${file}.bak"
	fi
    fi
done
DO_OK

# Edit the $iraf and $imdir paths in mkiraf.*sh, *cl.*sh, and libc/iraf.h
# files.

ECHO -n "Editing iraf/imdir/cache paths into system files ...           "

err_seen=0
for i in ${PATHFILES}; do
    if [ -e "$iraf/unix/hlib/$i" ]; then
	sed -i.bak \
	    -e "s|$o_iraf|$iraf|"   \
	    -e "s|$o_cache|$cache|" \
	    -e "s|$o_imdir|$imdir|" \
	    "$iraf/unix/hlib/$i"
	    RM "${iraf}/unix/hlib/${i}.bak"
    else
        if [ "$err_seen" = 0 ]; then
            DO_FAIL
            err_seen=1
            err_count=$(( err_count+1 ))
        fi
        MSG  "File $i not found."
    fi
done
if [ "$err_seen" = 0 ]; then
    DO_OK
fi



    # Create the root imdir as a public scratch directory, if not already
    # created.
    err_seen=0
    if [ -d "$imdir" ]; then
        if [ -x $"imdir" ]; then
            ECHO -n 'Checking imdir permissions ...'
            ECHO -n '                                 '
        else
            ECHO -n 'Setting mode for $imdir to 0777'
	    ECHO -n '                                '
            if [ "$exec" = "yes" ]; then
                chmod 777 "$imdir"
            fi
        fi
    else
	if [ "$do_system" = 1 ]; then
          ECHO -n "Creating root imdir at $imdir ...                    "
	else
          ECHO -n 'Creating root imdir at $HOME/.iraf/imdir ...		'
          ECHO -n '       '
	fi
        if [ "$exec" = "yes" ]; then
            mkdir $imdir; chmod 777 $imdir
        fi
    fi
    if [ "$err_seen" = 0 ]; then
        DO_OK
    fi


    # Create the root cache as a public scratch directory, if not already
    # created.
    err_seen=0
    if [ -d "$cache" ]; then
        if [ -x "$cache" ]; then
            ECHO -n 'Checking cache permissions ...'
            ECHO -n '                                 '
        else
            ECHO -n 'Setting mode for $cache to 0777                  '
            if [ "$exec" = "yes" ]; then
                chmod 777 $cache
            fi
        fi
    else
	if [ "$do_system" = 1 ]; then
          ECHO -n "Creating root cache at $cache ...                    "
	else
          ECHO -n 'Creating root cache at $HOME/.iraf/cach  ...		'
          ECHO -n '       '
	fi
        if [ "$exec" = "yes" ]; then
            mkdir $cache; chmod 777 $cache
        fi
    fi
    if [ "$err_seen" = 0 ]; then
        DO_OK
    fi



NEWLINE
BOLD_ON
ECHO "                         Creating File Links"
ECHO "                         -------------------"
BOLD_OFF


if [ "$do_system" = 1 ]; then
    # 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.

    # Verify we have a /usr/include directory (some MacOSX systems won't)
    ECHO -n "Checking /usr/include directory ...                            "
    if [ ! -e /usr/include ]; then
        if [ $exec = yes ]; then
            mkdir /usr/include
            if [ -d /usr/include ]; then
                DO_OK
            else
                DO_FAIL
                err_count=$(( err_count+1 ))
            fi
        else
            DO_OK
        fi
    else
        DO_OK
    fi

    ECHO -n "Creating <iraf.h> symlink ...                                  "
    file1="/usr/include/iraf.h"
    file2="$iraf/unix/hlib/libc/iraf.h"

    err_seen=0
    if [ -e "$file1" ]; then
        if [ "$($LS -l $file1 | grep $file2)" = "" ]; then
            if [ "$exec" = "yes" ]; then
                RM "$file1"
                ln -s "$file2" "$file1"
            fi
        fi
    else
        if [ "$exec" = "yes" ]; then
            RM "$file1"
            ln -s "$file2" "$file1"
        fi
    fi
    if [ "$exec" = "no" ]; then
        DO_OK
    elif [ "$err_seen" = 0 ] && [ -e "$file1" ]; then
        DO_OK
    else
        DO_FAIL
	err_count=$(( err_count+1 ))
    fi


else			# else of 'do_system'

    ECHO -n "Creating <iraf.h> symlink ...                                  "
    file1="$HOME/.iraf/iraf.h"
    file2="$iraf/unix/hlib/libc/iraf.h"

    err_seen=0
    if [ -e "$file1" ]; then
        if [ "$($LS -l $file1 | grep $file2)" = "" ]; then
            if [ "$exec" = "yes" ]; then
                RM "$file1"
                ln -s "$file2" "$file1"
            fi
        fi
    else
        if [ "$exec" = "yes" ]; then
            RM "$file1"
            ln -s "$file2" "$file1"
        fi
    fi
    if [ "$exec" = "no" ]; then
        DO_OK
    elif [ "$err_seen" = 0 ] && [ -e "$file1" ]; then
        DO_OK
    else
        DO_FAIL
	err_count=$(( err_count+1 ))
    fi


    ECHO -n 'Marking system architecture ...                                '
    if [ "$exec" = "yes" ]; then
	if [ -e "$HOME/.iraf/arch" ]; then
            RM "$HOME/.iraf/arch"
	fi
        echo "$IRAFARCH" >  "$HOME/.iraf/arch"
    fi
    DO_OK

fi			# end of 'do_system'


# Establish the remaining symbolic links to HSI tasks.
ECHO -n "Creating iraf command links in local bin dir ...               "

err_seen=0

if [ $old_links = 1 ]; then
  for i in ${LINKFILES}; do
    # Locate the file to be linked to.
    file1=${i%.*}
    for j in ${BINDIRS}; do
        file2="$j/$file1.${extn}"
        if [ -e "$file2" ]; then
            break
        fi
        file2="$j/$i"
        if [ -e "$file2" ]; then
            break
        fi
    done

    # Verify or set the link.
    if [ -e "$lbin/$file1" ]; then
        if [ "$($LS -l $lbin/$file1 | grep $file2)" = "" ]; then
            if [ "$exec" = "yes" ]; then
                RM "$lbin/$file1"
                ln -s "$file2" "$lbin/$file1"
            fi
        fi
    else
        if [ "$exec" = "yes" ]; then
            RM "$lbin/$file1"
            ln -s "$file2" "$lbin/$file1"
        fi
    fi

    if [ "$exec" = "yes" ]; then
        if [ ! -e "$lbin/$file1" ]; then
            if [ "$err_seen" = 0 ]; then
                DO_FAIL
            fi
            MSG  "Could not make link $file1 -> $file2"
            err_seen=1
	    err_count=$(( err_count+1 ))
        fi
    fi
  done

else

  # Create the command links, regardless of whether the target binaries exist.
  # This allows us to create links on a source-only system.
  for file2 in ${LTARGETS}; do
    file1=$(basename "$file2" | cut -d. -f1)

    # Verify or set the link.
    if [ -e "$lbin/$file1" ]; then
        if [ "$($LS -l $lbin/$file1 | grep $file2)" = "" ]; then
            if [ "$exec" = "yes" ]; then
                RM "$lbin/$file1"
                ln -s "$file2" "$lbin/$file1"
            fi
        fi
    else
        if [ "$exec" = "yes" ]; then
            RM "$lbin/$file1"
            ln -s "$file2" "$lbin/$file1"
        fi
    fi
  done

fi


if [ "$err_seen" = 0 ]; then
    DO_OK
fi

DO_OK



#=============================================================================
# Common code for XGTERM/XIMTOOL installation.
#=============================================================================

NEWLINE
BOLD_ON
ECHO "                    Creating Graphics Device Files"
ECHO "                    ------------------------------"
BOLD_OFF


#=============================================================================
# Install the default IMTOOLRC frame buffer configuration file.  The path
# /usr/local/lib path hardwired in to ximtool 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.
if [ "$do_system" = 1 ]; then
    ECHO -n "Checking /usr/local/lib directory ...                          "
    if [ ! -e "/usr/local/lib" ]; then
        if [ "$exec" = "yes" ]; then
            if [ ! -e "/usr/local" ]; then
                mkdir /usr/local
            fi
            mkdir /usr/local/lib
            if [ -d "/usr/local/lib" ]; then
                DO_OK
            else
                DO_FAIL
    	        err_count=$(( err_count+1 ))
            fi
        else
            DO_OK
        fi
    else
        DO_OK
    fi
fi


# Verify or set the IMTOOLRC link.
if [ "$do_system" = 1 ]; then
    file1=/usr/local/lib/imtoolrc
    ECHO -n "Creating /usr/local/lib/imtoolrc link ...                      "
else
    file1=$HOME/.imtoolrc
    ECHO -n 'Creating $HOME/.imtoolrc link ...                              '
fi
/bin/rm -f "$file1"
file2=$iraf/dev/imtoolrc


err_seen=0
if [ -e "$file1" ]; then
    if [ "$($LS $file1)" = "$file1" ]; then
        if [ "$($LS -l $file1 | grep $file2)" = "" ]; then
            if [ "$exec" = "yes" ]; then
                RM "$file1"
                ln -s "$file2" "$file1"
            fi
        fi
    fi
else
    if [ "$exec" = "yes" ]; then
	if [ -e "$file1" ]; then
	    RM "$file1"
	fi
        ln -s "$file2" "$file1"
    fi
fi
if [ "$exec" = "no" ]; then
    DO_OK
elif [ "$err_seen" = 0 ] && [ -e "$file1" ]; then
    DO_OK
else
    DO_FAIL
    err_count=$(( err_count+1 ))
fi

DO_OK


#=============================================================================
# Initialize the login.cl/uparm for a personal installation
#=============================================================================

if [ "$do_system" = 0 ]; then
  NEWLINE
  BOLD_ON
  ECHO "                      Initializing Login Files"
  ECHO "                      ------------------------"
  BOLD_OFF

  cur=$PWD
  if [ ! -e "$HOME/.iraf" ]; then
      mkdir "$HOME/.iraf"
  fi
  cd "$HOME/.iraf"
  ECHO -n "Creating global login.cl and uparm directory ....              "
  "$iraf/unix/hlib/mkiraf.sh" --default --init --term="$myterm" > /dev/null 2>&1
  if [ -e "$HOME/.iraf/login.cl" ]; then
      DO_OK
  else
      DO_FAIL
  fi

  cd "$cur"

  # Add the setup to the user's login files.

  if [ "$do_csh" = 1 ] && [ "$exec" = "yes" ]; then
      cfiles="$HOME/.cshrc $HOME/.tcshrc $HOME/.login"
      for file in ${cfiles}; do
	  if ! grep -qs iraf/setup.csh "$file"; then
	      if [ -e "$file" ] ; then
		  cp "$file" "${file}.bak"
	      else
		  touch "$file"
	      fi
	      cat >> $file <<EOF

# Add iraf setup commands
if ( -e $HOME/.iraf/setup.csh ) then
    source $HOME/.iraf/setup.csh
endif
EOF
	  fi
      done
  fi

  if [ "$exec" = "yes" ]; then
      bfiles="$HOME/.bashrc $HOME/.profile $HOME/.bash_profile $HOME/.bash_login"
      for file in ${bfiles}; do
	  if ! grep -qs iraf/setup.sh "$file"; then
	      if [ -e "$file" ] ; then
		  cp "$file" "${file}.bak"
	      else
		  touch "$file"
	      fi
	      cat >> $file <<EOF

# Add iraf setup commands
if [ -e $HOME/.iraf/setup.sh ]; then
    source $HOME/.iraf/setup.sh
fi
EOF
	  fi
      done
  fi
fi

#=============================================================================
#  Finish up and set the exit status.
#=============================================================================


NEWLINE ; NEWLINE

if [ "$err_count" = 0 ]; then
 BOLD_ON
 ECHO "========================================================================"
 ECHO -n "Congratulations!  "
 BOLD_OFF
 ECHO "IRAF has been successfully installed on this system."
 BOLD_ON
 ECHO "========================================================================"
 BOLD_OFF
 NEWLINE

 if [ "$do_system" = 1 ]; then

   ECHO "    To begin using the system simply log in as any user and from the"
   ECHO "directory you wish to use as your iraf login directory type:"
   ECHO ""
   ECHO -n '          % ';
   BOLD_ON; ECHO -n 'mkiraf'; BOLD_OFF
   ECHO '     # create a login.cl file'
   ECHO -n '          % ';
   BOLD_ON; ECHO -n 'cl    '; BOLD_OFF
   ECHO '     # start IRAF'
   ECHO ""
   ECHO "The 'iraf' user is already configured with a login.cl file so a simple"
   ECHO "'cl' command is sufficient to start the system."

 else

   ECHO "    To begin using IRAF you can simply type"
   ECHO ""
   ECHO -n '          % ';
   BOLD_ON; ECHO    'source ~/.login    '; BOLD_OFF
   ECHO -n '          % ';
   BOLD_ON; ECHO    'cl    '; BOLD_OFF
   ECHO ""
   ECHO "From any directory to use the global login files created in the"
   BOLD_ON ; ECHO -n "$HOME/.iraf " ; BOLD_OFF
   ECHO "directory.   If you wish to have a login.cl/uparm that"
   ECHO "is specific to a particular directory, you will need to type"
   ECHO ""
   ECHO -n '          % ';
   BOLD_ON; ECHO -n 'mkiraf'; BOLD_OFF
   ECHO '     # create a login.cl file (needed once)'
   ECHO -n '          % ';
   BOLD_ON; ECHO -n 'cl    '; BOLD_OFF
   ECHO '     # start IRAF'
   ECHO ""
 fi

 ECHO "Additional user information can be found at the iraf community web site:"
 NEWLINE
 BOLD_ON ; ECHO "    https://iraf-community.github.io" ; BOLD_OFF
 NEWLINE
 ECHO "Please create an issue on https://github.com/iraf-community/iraf/issues"
 ECHO "in case of problems."
 NEWLINE
 NEWLINE

 BOLD_ON
 ECHO "========================================================================"
 ECHO "================  Installation Completed With No Errors  ==============="
 ECHO "========================================================================"
 BOLD_OFF
 NEWLINE
 exit 0

else
 BOLD_ON
 ECHO "========================================================================"
 ECHO "=================  Installation Completed With Errors  ================="
 ECHO "========================================================================"
 BOLD_OFF
 NEWLINE
 exit 1

fi

exit 0

