#!/bin/sh
#
# IRAF -- IRAF.app application startup script.
#
# Copyright(c) 2023 Association of Universities for Research in Astronomy Inc.

# Patch to install/configure IRAF in user's home withouth launching the
# application
# ncaon, 2024-11-13


# -------------------------------------------------------------------------


#  Global Variables
_arch=`uname -m`
_uid=`/usr/bin/id -u`



# The first time we are run we will create the users $HOME/.iraf directory.
# First see whether we have the /Applications installation, if so we're done.

if ! test -f $HOME/.iraf/initrc; then

    ###############################
    # Login File Setup
    ###############################

    # If we have an existing $HOME/.iraf, back it up, but only once.
    if test -d $HOME/.iraf; then
        if ! test -d $HOME/.iraf.ORIG; then
            mv $HOME/.iraf $HOME/.iraf.ORIG
        fi
    fi


    # Now copy skeleton directory with pre-defined links.
    if ! test -d $HOME/.iraf; then
        skel=/Applications/IRAF.app/Contents/Resources/iraf.skel/
        /usr/bin/rsync -a $skel $HOME/.iraf

        # Apply the current user name in the skeleton files.
        sed -e "s/USER/$USER/g" $HOME/.iraf/login.cl > /tmp/_login.cl
        mv /tmp/_login.cl $HOME/.iraf/login.cl
        sed -e "s/USER/$USER/g" $HOME/.iraf/initrc > /tmp/_initrc
        mv /tmp/_initrc $HOME/.iraf/initrc

        # PyRAF also requires/assumes that the underlying IRAF system is 
        # configured for the current architecture.  The first time we start,
        # do this configuration since it will also help with command-line
        # development of tasks/packages
        if [ "$_arch" == "arm64" ]; then
            echo macosx > $HOME/.iraf/arch
            (cd $iraf && make macosx > /dev/null 2>&1)
        else
            echo macintel > $HOME/.iraf/arch
            (cd $iraf && make macintel > /dev/null 2>&1)
        fi
    fi


    # Try to preserve the user's loginuser.cl file.  NOTE, this may include
    # path definitions that are no longer valid but should still work for
    # locally defined scripts in the user directories.
    if test -f $HOME/.iraf.ORIG/loginuser.cl; then
        if ! test -f $HOME/.iraf/loginuser.cl; then
            cp $HOME/.iraf.ORIG/loginuser.cl $HOME/.iraf
        fi
    fi


    ###############################
    # User Environment Setup
    ###############################

    # Add the setup to the user's login files.
    cfiles=("$HOME/.cshrc $HOME/.tcshrc $HOME/.login")
    bfiles=("$HOME/.bash* $HOME/.profile" $HOME/.zshrc)

    TEMP="/tmp/iraf_install.$$"             # temp output file

    for file in ${cfiles[@]}; do
      touch ${file}
      if [ -e "$file" ]; then
        if [ "$exec" == "yes" ]; then
            /bin/rm -f $TEMP >& /dev/null
            cp $file $TEMP

            egrep -e "iraf/setup.csh" $TEMP                     >& /dev/null
            if (( $?==1 )); then
                echo ""                                         >> $TEMP
                echo "# Add iraf setup commands"                >> $TEMP
                echo "if ( -e $HOME/.iraf/setup.csh ) then"     >> $TEMP
                echo "    source $HOME/.iraf/setup.csh"         >> $TEMP
                echo "endif"                                    >> $TEMP
                cp $file ${file}.bak
                cp -p  $TEMP $file
            fi
            /bin/rm -f $TEMP >& /dev/null
        fi
      else
        /bin/rm -f $TEMP                                        >& /dev/null
        echo ""                                                  > $file
        echo "# Add iraf setup commands"                        >> $file
        echo "if ( -e $HOME/.iraf/setup.csh ) then"             >> $file
        echo "    source $HOME/.iraf/setup.csh"                 >> $file
        echo "endif"                                            >> $file
      fi
    done

    for file in ${bfiles[@]}; do
      touch ${file}
      if [ -e "$file" ]; then
        if [ "$exec" == "yes" ]; then
            /bin/rm -f $TEMP >& /dev/null
            cp $file $TEMP

            egrep -e "iraf/setup.sh" $TEMP                      >& /dev/null
            if (( $?==1 )); then
                echo ""                                         >> $TEMP
                echo "# Add iraf setup commands"                >> $TEMP
                echo "if [ -e $HOME/.iraf/setup.sh ]; then"     >> $TEMP
                echo "    source $HOME/.iraf/setup.sh"          >> $TEMP
                echo "fi"                                       >> $TEMP
                cp $file ${file}.bak
                cp -p  $TEMP $file
            fi
            /bin/rm -f $TEMP >& /dev/null
        fi
      else
        /bin/rm -f $TEMP                                        >& /dev/null
        echo ""                                                  > $file
        echo "# Add iraf setup commands"                        >> $file
        echo "if [ -e $HOME/.iraf/setup.sh ]; then"             >> $file
        echo "    source $HOME/.iraf/setup.sh"                  >> $file
        echo "fi"                                               >> $file
      fi
    done

fi


# Define the proper IRAF environment for this script.
export iraf=/Applications/IRAF.app/Contents/iraf-v218/
export IRAFARCH=`$iraf/unix/hlib/irafarch.sh -actual`
source $iraf/unix/hlib/irafuser.sh

# Pick up any user configuration values/
if test -f $HOME/.iraf/initrc; then
    source $HOME/.iraf/initrc
fi


###############################
# Terminal Setup
###############################

# --------------------------------------------------------
