#!/bin/sh
#############################################################################
#
# Licensed Materials - Property of IBM
#
# 5648-A30
# (C) COPYRIGHT International Business Machines Corp. 1993, 1998
#
# 5648-A32
# (C) COPYRIGHT International Business Machines Corp. 1993, 1998
#
# 5648-A29
# (C) COPYRIGHT International Business Machines Corp. 1993, 1998
#
# 5648-A34
# (C) COPYRIGHT International Business Machines Corp. 1993, 1998
#
# All Rights Reserved
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#############################################################################
# Filename : install_fix
# Type     : sh (Bourne shell)
# Function : Script to fix db2_install for Solaris 5.7+
#
########################################################################

# Set the directory name (TOPDIR) where this file is located.
CURDIR=`/usr/bin/pwd`
TOPDIR=`/usr/bin/dirname $0`
cd ${TOPDIR?}
TOPDIR=`/usr/bin/pwd`
cd ${CURDIR?}

PROGNAME=`/usr/bin/basename $0`         # Program name


#############################################################
##               Start of function definitions             ##
#############################################################


# Define/initialize all default variables here
defaults()
{
    OSN=`/usr/bin/uname    | /usr/bin/awk '{print $1}' `    # Operating system
    OSV=`/usr/bin/uname -v | /usr/bin/awk '{print $1}' `    # OS version number
    OSR=`/usr/bin/uname -r | /usr/bin/awk '{print $1}' `    # OS release number

    YES="y"
    NO="n"

    DIR_CDROM=""           # CD-ROM mount point
    DIR_DEST=""            # New linked directory path
    DB2_INSTALLER=""       # Original db2_installer file
    DB2_INSTALLER_NEW=""   # Patched db2_installer file
    FORCE_PATCH="${NO?}"   # Force emptying DIR_DEST
}


# Define commands
commands()
{
    CMD_ECHO="/usr/bin/echo"
    CMD_ID="/usr/bin/id"
    CMD_GREP="/usr/bin/grep"
    CMD_MKDIR="/usr/bin/mkdir"
    CMD_LN="/usr/bin/ln"
    CMD_RM="/usr/bin/rm"
    CMD_CP="/usr/bin/cp"
    CMD_SED="/usr/bin/sed"
    CMD_PATCH="/usr/bin/patch"
}


# stop_prog() - cleanup and exit
stop_prog()
{
    sig=${1:-1}

    ${CMD_ECHO?}
    case ${sig?} in
        0) ${CMD_ECHO?} "${PROGNAME?} completed successfully." ;;
        1) ${CMD_ECHO?} "${PROGNAME?} terminated prematurely." ;;
        2) ${CMD_ECHO?} "${PROGNAME?} terminated by request." ;;
        *) ${CMD_ECHO?} "${PROGNAME?} generated an unknown error." ;;
    esac

    exit ${sig?}
}


# Displays an error
show_error()
{
    err_msg=$1

    ${CMD_ECHO?} "${err_msg?}"
    stop_prog 1
}


# check_root() - checks if root is executing script
check_root()
{
    ${CMD_ID?} | ${CMD_GREP?} 'uid=0(' 1> /dev/null 2> /dev/null
    if [ $? -ne 0 ]; then
        show_error "You must be root to execute this program."
    fi
}


# Displays calling syntax and exits
syntax()
{
    ${CMD_ECHO?} "Usage: ${PROGNAME?} [-c CDPath] [-d DestDir] [-f]"
    ${CMD_ECHO?} "  -c CDPath    specify the source DB2 CD-ROM directory"
    ${CMD_ECHO?} "  -d DestDir   specify the destination directory"
    ${CMD_ECHO?} "  -f           force patching"
    stop_prog 1
}


# Prompts for an input path
get_input()
{
    prompt=$1
    INPUT_PATH=""

    while [ -z "${INPUT_PATH?}" ]; do
        ${CMD_ECHO?} "${prompt?}"
        read INPUT_PATH
    done

    if [ "${INPUT_PATH?}" = "q" ]; then
        stop_prog 2
    fi
}


# Loops until a "y" or an "n" is entered
yes_or_no()
{
    question=$1
    YES_OR_NO=""

    while [ "${YES_OR_NO?}" != "${YES?}" -a \
            "${YES_OR_NO?}" != "${NO?}" ]; do
        ${CMD_ECHO?} "${question?}"
        read YES_OR_NO
    done
}


# Patch original db2_install and output to new db2_install
patch_db2_install()
{
    ${CMD_ECHO?} '202,205c202,214
<     case ${oslevel?} in
<         SunOS5.[56] )   ok=${TRUE?}  ;;
<         SunOS5.[56].* ) ok=${TRUE?}  ;;
<         * )             ok=${FALSE?} ;;
---
>     osbase=`/usr/bin/echo "${oslevel?}" | /usr/bin/cut -c1-6`
>     case ${osbase?} in
>         SunOS5 )
>             osminor=`/usr/bin/echo "${oslevel?}" | /usr/bin/cut -d. -f2`
>             if [ "${osminor?}" -ge 5 ]; then
>                 ok=${TRUE?}
>             else
>                 ok=${FALSE?}
>             fi
>             ;;
>         * )
>             ok=${FALSE?}
>             ;;' | ${CMD_PATCH?} -n ${DB2_INSTALL_NEW?} 1> /dev/null 2> /dev/null
    if [ $? -ne 0 ]; then
        show_error "Patching ${DB2_INSTALL?} failed."
    fi
}


# Main function
fix_db2_install()
{
    # Input the CD directory, if it wasn't passed in
    if [ -z "${DIR_CDROM?}" ]; then
        get_input "Please enter the CD mount point (q to quit):"
        DIR_CDROM=${INPUT_PATH?}
    fi
    DB2_INSTALL="${DIR_CDROM?}/db2_install"

    # Check to make sure it exists
    if [ ! -r ${DB2_INSTALL?} ]; then
        show_error "${DB2_INSTALL?} could not be opened."
    fi

    # Input the destination directory, if it wasn't passed in
    if [ -z "${DIR_DEST?}" ]; then
        ${CMD_ECHO?}
        get_input "Please enter the destination path (q to quit):"
        DIR_DEST=${INPUT_PATH?}
    fi
    DB2_INSTALL_NEW="${DIR_DEST?}/db2_install"

    # Emit warning message
    ${CMD_ECHO?}
    ${CMD_ECHO?} "Patching will now proceed, with"
    ${CMD_ECHO?} "    Source CD-ROM directory = \"${DIR_CDROM?}\","
    ${CMD_ECHO?} "  and Destination directory = \"${DIR_DEST?}\"."
    if [ "${FORCE_PATCH?}" != "${YES?}" ]; then
        yes_or_no "Do you wish to continue (y/n)?"
        if [ "${YES_OR_NO?}" != "${YES?}" ]; then
            stop_prog 2
        fi
    fi

    # Create the destination directory, if required
    if [ ! -d ${DIR_DEST?} ]; then
        ${CMD_MKDIR?} -p ${DIR_DEST?}
        if [ $? -ne 0 ]; then
            show_error "Could not create directory ${DIR_DEST?}."
        fi
    else
        # Delete the contents of the destination directory, if it exists
        if [ "${FORCE_PATCH?}" != "${YES?}" ]; then
            ${CMD_ECHO?}
            ${CMD_ECHO?} "Warning: The contents of ${DIR_DEST?} will be deleted."
            yes_or_no "Do you wish to continue (y/n)?"
            if [ "${YES_OR_NO?}" != "${YES?}" ]; then
                stop_prog 2
            fi
        fi

        ${CMD_RM?} -rf ${DIR_DEST?}/*
        if [ $? -ne 0 ]; then
            show_error "Could not remove the contents of ${DIR_DEST?}."
        fi
    fi

    # Link the CD directory to the destination directory
    ${CMD_LN?} -s ${DIR_CDROM?}/* ${DIR_DEST?}
    if [ $? -ne 0 ]; then
        show_error "Could not create symbolic links."
    fi

    # Remove the link to db2_install, and copy it over
    ${CMD_RM?} -f ${DB2_INSTALL_NEW?}
    if [ $? -ne 0 ]; then
        show_error "Could not remove link ${DB2_INSTALL_NEW?}."
    fi
    ${CMD_CP?} ${DB2_INSTALL?} ${DB2_INSTALL_NEW?}
    if [ $? -ne 0 ]; then
        show_error "Could not copy ${DB2_INSTALL?} to ${DB2_INSTALL_NEW?}."
    fi

    # Apply the patch
    patch_db2_install
}


###################################################
##          End of function definitions          ##
###################################################


defaults
commands

if [ $# -gt 0 ]; then
    while getopts fc:d: optchar; do
        case ${optchar?} in
            f)  # Force cleaning of DIR_DEST
                FORCE_PATCH="${YES?}"
                ;;
            c)  # CD-ROM mount point
                DIR_CDROM="${OPTARG?}"
                ;;
            d)  # New linked directory
                DIR_DEST="${OPTARG?}"
                ;;
            *)
                syntax
                ;;
        esac
    done
fi

trap "stop_prog 2" 1 2 3 15

fix_db2_install

stop_prog 0

# End of Script
