#!/bin/sh
# Copyright 09/01/00 Sun Microsystems, Inc. All Rights Reserved.
#pragma ident  "@(#)es-makeagent	1.8 01/04/18 Sun Microsystems"

#Check if CD will get locked if this script runs from its present location and
#if necessary, copy all scripts and locales to tmp location and call es-makeagent from there.

checkIfCalledFromCD() {

    cd $SCRIPT_DIR
    set_copy_exec_flags

#
#Here if COPY_FLAG = 1 then this script is called from
#CD. If EXEC_FLAG = 1 then it needs to exec cmdtool else
#it does not need to exec cmdtool.
#If COPY_FLAG = 0 then we dont need to set TEXTDOAMIN or
#TEXETDOMAINDIR at this time. But if COPY_FLAG = 1 then
#we need to set the TEXTDOMAIN and TEXTDOMAINDIR for the
#following messages related to copy of scripts to tmp dir.
#One thing to note here is if we exec es-makeagent, it carries
#all other env variables but not LANG. So we will have to
#pass LANG somehow.
#


    if [ $COPY_FLAG -eq 0 ] ; then
        cd $PREV_DIR
    else
        echolog "You have called es-makeagent from the cd image."
        remove_temp_directory
        create_temp_directory
        copySbinLocale

        to_call_again=1
        processInstallOptions $@
        if [ $EXEC_FLAG -eq 1 ] ; then
            commandline=`$CMD_ECHO "exec ${TMP_DIR}/sbin/es-makeagent $OPTION"`
            $commandline
        else
            multiline_echolog 'You must run Sun Management Center installation by using the\ncomplete path after changing directory to /.'
            exit_SunMC $DISPLAY_ERROR
        fi
        exit
    fi
    unset COPY_FLAG EXEC_FLAG commandline

}



# $1 Product subdir
# $2 OS versions

getAgentTargetDirectory() {
    if [ -z "$TARGET_DIR" ] ; then
        while get_input_from_user 'Enter the target directory:'
        do
            [ "$answer" = "" ] && continue
            if [ ! -d "$answer" ] ; then
                echolog 'Directory $2 does not exist' "$answer"
                TARGET_DIR="$answer"
                ask_user "Do you want to create it"
                if [ $answer -eq 0 ]; then
                    sleep 5
                    exit 1
                fi
                /usr/bin/mkdir -p "$TARGET_DIR" > /dev/null 2>&1
                if [ $? -ne 0 ] ; then
                    echolog 'Could not create the directory $2' "$TARGET_DIR"
                    sleep 5
                    exit 1
                fi
            else
                TARGET_DIR="$answer"
            fi
            break
        done
    else
        if [ ! -d "$TARGET_DIR" ] ; then
            echolog 'Directory $2 does not exist' "$TARGET_DIR"
            ask_user "Do you want to create it"
            if [ $answer -eq 0 ]; then
                sleep 5
                exit 1
            fi
            /usr/bin/mkdir -p "$TARGET_DIR" > /dev/null 2>&1
            if [ $? -ne 0 ] ; then
                echolog 'Could not create the directory $2' "$TARGET_DIR"
                sleep 5
                exit 1
            fi
        fi
    fi
}

makeListOfAgentPackages ()
{
    PACKAGES=""
    COMPONENT_SCRIPTS=""
    MKAGT_PATCHES=""
    prod_subdir="$SRC_DIR/$1"
    os_dir=$2

    # For S10, check if there is a Solaris_10 directory
    # If that does not exist, then look in Solaris_9 directory
    [ $os_dir -eq 10 -a ! -d "${prod_subdir}/Solaris_10" ] && os_dir=9

    os_vers="Solaris_${2}"

    INSTALLATION_INFO="${prod_subdir}/Solaris_${os_dir}/installation.info"
    INSTALLATION_PROPERTY="${prod_subdir}/Solaris_${os_dir}/installation.properties"

    if [ ! -s "$INSTALLATION_INFO" -o ! -s "$INSTALLATION_PROPERTY" ]; then
	echologverbose 'Installation configuration files not found'
	return
    fi

    getProductInfoFromFile

    echo " "
    if [ "$DISPLAY_PRODUCT" = "TRUE" ]; then
        echolog 'Product: $2' "$PRODUCT_NAME"
	echo "$LOG_LINE"
        DISPLAY_PRODUCT="FALSE"
    fi
    echolog 'Copying $2 agent layer packages...' "$os_vers"

    getComponentListFromFile

    final_pkgs=""
    final_scripts=""
    final_patches=""

    for COMPONENT_KEY in `$CMD_ECHO "$COMPONENT_LIST" | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
    do
	getComponentInfo "PACKAGES"
	packages=${COMPONENT_INFO}
        packages=`$CMD_ECHO "${packages}" | $CMD_SED "s/ /,/g" 2> /dev/null`
	if [ -z "$final_pkgs" ]; then
	    final_pkgs="$packages"
	else
	    final_pkgs="${packages},${final_pkgs}"
	fi

        getComponentInfo "COMPONENT_SCRIPT"
	[ -n "${COMPONENT_INFO}" ] && scripts="${COMPONENT_INFO}"
	if [ -z "$final_scripts" ]; then
	    final_scripts="$scripts"
	else
	    final_scripts="${scripts},${final_scripts}"
	fi

        getComponentInfo "PATCHES_REQUIRED"
	[ -n "${COMPONENT_INFO}" ] && patches="${COMPONENT_INFO}"
	if [ -z "$final_patches" ]; then
	    final_patches="$patches"
	else
	    final_patches="${patches},${final_patches}"
	fi
	

    done

    PACKAGES="$final_pkgs"
    COMPONENT_SCRIPTS="$final_scripts"
    MKAGT_PATCHES="$final_patches"

    unset final_patches final_pkgs final_scripts patches packages scripts
    
}

copyAgentProduct () 
{
    PRODUCT_DIR="$1"
    DISK="$2"
    DISPLAY_PRODUCT="TRUE"

    S10Exists=0
    [ -d $SRC_DIR/${PRODUCT_DIR}/Solaris_10 ] && S10Exists=1

    /usr/bin/mkdir -p "$TARGET_DIR"/disk${DISK}/${IMGPFIX}/image/${PRODUCT_DIR}/CommonForSolaris/Basic
    for each_os in `$CMD_ECHO "$SELECTED_OS" | $CMD_AWK -F ',' '{ for (i = 1; i<= NF; i++) print $i }'`
    do
	[ $S10Exists -eq 0 -a $each_os -eq 10 ] && each_os=9
        /usr/bin/mkdir -p "$TARGET_DIR"/disk${DISK}/${IMGPFIX}/image/${PRODUCT_DIR}/Solaris_${each_os}/Basic
    done

    if [ "$PRODUCT_DIR" = "PE" ]; then
       /usr/bin/cp $SRC_DIR/${PRODUCT_DIR}/.locales "$TARGET_DIR"/disk${DISK}/${IMGPFIX}/image/${PRODUCT_DIR}/.locales
    fi

    for each_os in `$CMD_ECHO "$SELECTED_OS" | $CMD_AWK -F ',' '{ for (i = 1; i<= NF; i++) print $i }'`
    do
	[ $S10Exists -eq 0 -a $each_os -eq 10 ] && each_os=9
	[ ! -d $SRC_DIR/${PRODUCT_DIR}/Solaris_${each_os}/locale ] && continue
        /usr/bin/cp -Rf $SRC_DIR/${PRODUCT_DIR}/Solaris_${each_os}/locale "$TARGET_DIR"/disk${DISK}/${IMGPFIX}/image/${PRODUCT_DIR}/Solaris_${each_os}/
    done

    for each_os in `$CMD_ECHO "$SELECTED_OS" | $CMD_AWK -F ',' '{ for (i = 1; i<= NF; i++) print $i }'`
    do
	[ $S10Exists -eq 0 -a $each_os -eq 10 ] && each_os=9
	[ ! -d $SRC_DIR/${PRODUCT_DIR}/Solaris_${each_os} ] && continue
        /usr/bin/cp -f $SRC_DIR/${PRODUCT_DIR}/Solaris_${each_os}/installation* "$TARGET_DIR"/disk${DISK}/${IMGPFIX}/image/${PRODUCT_DIR}/Solaris_${each_os}/
    done

    COMMON_PACKAGES=""

    for each_os in `$CMD_ECHO "$SELECTED_OS" | $CMD_AWK -F ',' '{ for (i = 1; i<= NF; i++) print $i }'`
    do
	os_dir=$each_os
	[ $S10Exists -eq 0 -a $os_dir -eq 10 ] && os_dir=9
	[ ! -d $SRC_DIR/${PRODUCT_DIR}/Solaris_${os_dir} ] && continue

        makeListOfAgentPackages "${PRODUCT_DIR}" "$each_os"
        for each_pkg in `$CMD_ECHO "$PACKAGES" | $CMD_AWK -F ',' '{ for (i = 1; i<= NF; i++) print $i }'`
	do
            common_dir=$SRC_DIR/${PRODUCT_DIR}/CommonForSolaris
            [ ! -d "$common_dir" ] && common_dir=$SRC_DIR/${PRODUCT_DIR}/Common
	    [ ! -d "$common_dir" ] && continue
	    common_dirname=`/usr/bin/basename "$common_dir"`

	    pkgdir=${common_dir}/Basic/${each_pkg}
	    [ ! -d "$pkgdir" ] && continue

	    checkString "$COMMON_PACKAGES" $each_pkg
	    if [ $? -eq 0 ]; then
	        echolog 'Adding $2' "$each_pkg"
		continue
	    else
		COMMON_PACKAGES="$each_pkg,$COMMON_PACKAGES"
	    fi 

	    echolog 'Adding $2' "$each_pkg"
            /usr/bin/cp -Rf $SRC_DIR/${PRODUCT_DIR}/${common_dirname}/Basic/$each_pkg $TARGET_DIR/disk${DISK}/${IMGPFIX}/image/${PRODUCT_DIR}/CommonForSolaris/Basic/
        done

        for each_pkg in `$CMD_ECHO "$PACKAGES" | $CMD_AWK -F ',' '{ for (i = 1; i<= NF; i++) print $i }'`
	do
	    pkgdir=${SRC_DIR}/${PRODUCT_DIR}/Solaris_${os_dir}/Basic/${each_pkg}
	    [ ! -d "$pkgdir" ] && continue
	    echolog 'Adding $2' "$each_pkg"
            /usr/bin/cp -Rf $pkgdir $TARGET_DIR/disk${DISK}/${IMGPFIX}/image/${PRODUCT_DIR}/Solaris_${os_dir}/Basic/
        done

        for each_script in `$CMD_ECHO "$COMPONENT_SCRIPTS" | $CMD_AWK -F ',' '{ for (i = 1; i<= NF; i++) print $i }'`
	do
	    script_dir="${SRC_DIR}/${PRODUCT_DIR}/Solaris_${os_dir}/Basic/"
	    [ ! -f "${script_dir}/${each_script}" ] && continue
	    /usr/bin/cp -f ${script_dir}/${each_script} $TARGET_DIR/disk${DISK}/${IMGPFIX}/image/${PRODUCT_DIR}/Solaris_${os_dir}/Basic/
	done

        for each_patch in `$CMD_ECHO "$MKAGT_PATCHES" | $CMD_AWK -F ',' '{ for (i = 1; i<= NF; i++) print $i }'`
	do
	    patch_dir="${SRC_DIR}/Patches/Solaris_${os_dir}"
	    [ ! -d "${patch_dir}/${each_patch}" ] && continue
            echolog 'Copying required patch $2' "$each_patch"
            target_patch_dir="${TARGET_DIR}/disk${DISK}/${IMGPFIX}/image/Patches/Solaris_${os_dir}"
            /usr/bin/mkdir -p $target_patch_dir
	    /usr/bin/cp -Rf ${patch_dir}/${each_patch} $target_patch_dir
	done
    done

}

copyEssentialPatches ()
{
    essential_patch_dir="${SRC_DIR}/Patches/Essential"
    DISK=$1
    if [ -d $essential_patch_dir ]; then
	# Copy the whole Essential patch directory to target
	/bin/mkdir -p ${TARGET_DIR}/disk${DISK}/${IMGPFIX}/image/Patches
	echolog 'Copying Essential Patches directory on disk $2' "$DISK"
	/bin/cp -Rf $essential_patch_dir ${TARGET_DIR}/disk${DISK}/${IMGPFIX}/image/Patches
    fi
}

copyAddonProductsAndPatches ()
{
    addon_dir_list=`$CMD_LS "${SRC_DIR}/Addons"`
    addon_dir_list=`$CMD_ECHO "$addon_dir_list" | $CMD_SED "s/\\n/ /g"`

    for dir in `$CMD_ECHO "$addon_dir_list" | $CMD_AWK 'BEGIN{RS=" "} {print}'`
    do   
        if [ "$dir" != "SunMC2.X" -a "$dir" != "Windows" ] ; then # Not SunMC 2.X addon products
	    copyAgentProduct "Addons/${dir}" "1"
	fi
    done

    # Copy any Essential Patches from disk1
    copyEssentialPatches "1"

    #
    #Get the src dir from cd #2
    #

    while [ 1 -eq 1 ]
    do
        if [ -d "${SRC_DIR}/${DOTPFIX}../../disk2/${IMGPFIX}/image" ]; then 
            curr_dir=`pwd`
            cd "${SRC_DIR}/${DOTPFIX}../../disk2/${IMGPFIX}/image"
            answer=`pwd`
            cd $curr_dir
        else
            cd /
            eject cd
	    tmp_auto="/cdrom/cdrom0/image"
            get_input_from_user 'Insert the second CD or enter the source directory for disk 2 \\\[$2\\\]' "$tmp_auto"
        fi
        if [ -z "$answer" ] ; then
            answer="/cdrom/cdrom0/image"
        fi

        cd_count=0
        while [ 1 ]
        do
            sleep 1
            if [ -d "$answer" ] ; then
                break
            fi
            cd_count=`/usr/bin/expr $cd_count + 1`
            if [ $cd_count -ge 10 ] ; then
                echolog 'Invalid directory $2' "$answer"
                break
            fi
        done
        if [ ! -d "$answer" ]; then
            echolog 'Invalid directory.'
            echolog ''
            continue
        fi
        SRC_DIR="$answer"
        checkSourceDirArch $TGTARCH # Now check against the target arch
        [ $? -ne 0 ] && continue
        break
    done
    
    #
    #Copy from disk2
    #
    echo " "
    echolog 'Processing disk2 of the source image'
    echo "$LOG_LINE"
    addon_dir_list=`$CMD_LS "${SRC_DIR}/Addons"`
    addon_dir_list=`$CMD_ECHO "$addon_dir_list" | $CMD_SED "s/\\n/ /g"`

    for dir in `$CMD_ECHO "$addon_dir_list" | $CMD_AWK 'BEGIN{RS=" "} {print}'`
    do   
        if [ "$dir" != "SunMC2.X" -a "$dir" != "Windows" ] ; then # Not SunMC 2.X addon products
	    copyAgentProduct "Addons/${dir}" "2"
	fi
    done

    # Copy any Essential Patches from disk2
    copyEssentialPatches "2"
}

makeTarFile () 
{
    echolog "Creating tar file, please wait..." 
    /usr/bin/tar -cf ${TARGET_DIR}/SunMC35.tar ${TARGET_DIR}/disk1
    /usr/bin/tar -uf ${TARGET_DIR}/SunMC35.tar ${TARGET_DIR}/disk2
    echolog "Done."
}

selectOSVersions()
{
    echolog ' '
    echolog 'Choose the Solaris versions'
    multiline_echolog 'The source files for only the Solaris versions you choose\nwill be added into the tar files created by this script.'
    echolog ' '
    echolog 'Supported os version are:'

    for each_sup_os in `$CMD_ECHO "$ALL_SUPPORTED_OS" | $CMD_AWK -F ',' '{ for (i = 1; i<= NF; i++) print $i }'`
    do
        echolog "Solaris_${each_sup_os}"
    done

    while [ 1 -eq 1 ]
    do
        SELECTED_OS=""
        echolog ' '

        ask_user 'Do you want to select ALL OS versions '
        if [ "$answer" = "1" ]; then
            SELECTED_OS="$ALL_SUPPORTED_OS"
        else
            echolog ' '
            for each_os in `$CMD_ECHO "$ALL_SUPPORTED_OS" | $CMD_AWK -F ',' '{ for (i = 1; i<= NF; i++) print $i }'`
            do
                ask_user 'Do you want to include source files for $2 ' "Solaris_${each_os}"
                if [ "$answer" = "1" ]; then
                    if [ -z "$SELECTED_OS" ]; then
                        SELECTED_OS="$each_os"
                    else
                        SELECTED_OS="$SELECTED_OS,$each_os"
                    fi
                fi
            done
        fi
        if [ -z "$SELECTED_OS" ]; then
            echolog ' '
            echolog 'No OS version has been selected'
        else
            echolog ' '
            echolog 'The following OS versions have been selected:'
            for each_sel_os in `$CMD_ECHO "$SELECTED_OS" | $CMD_AWK -F ',' '{ for (i = 1; i<= NF; i++) print $i }'`
            do
                echolog "Solaris_${each_sel_os}"
            done
            break
        fi
    done
}

##############################
#
# Start Main Here
#
##############################

prog_base=`/usr/bin/basename $0`
PROGNAME=$0
PREV_DIR=`/usr/bin/pwd`
SCRIPT_DIR=`/usr/bin/dirname $PROGNAME`
ALL_SUPPORTED_OS="2.6,7,8,9,10"
SELECTED_OS=""
COMMON_PACKAGES=""
PACKAGES=""
COMPONENT_SCRIPTS=""
DISPLAY_PRODUCT="TRUE"
VAROPTDIR="/var/opt/SUNWsymon"


. ${SCRIPT_DIR}/es-common.sh

TOOLS_DIR="${SCRIPT_DIR}/../lib/sbin"

SCRIPT_DIR=`getPathName $SCRIPT_DIR`
TOOLS_DIR=`getPathName $TOOLS_DIR`
PATH="$PATH:${SCRIPT_DIR}:${TOOLS_DIR}"

. ${TOOLS_DIR}/install-functions.sh
. ${TOOLS_DIR}/script-errors.sh

# Set this variable so that getComponentList returns agent layer components
INSTALL_LAYERS="LAYER.AGENT"

# Get command line parameters
SRC_DIR=""
TARGET_DIR=""

checkOSVersion
setInstallFlags

check_for_l10n
if [ -d "$SCRIPT_DIR/${DOTPFIX}../../disk1/${IMGPFIX}/image" ]
then
    getLocaleDir
else
    LOCALE_DIRECTORY="$SCRIPT_DIR/../image/PE/CommonForSolaris/locale"
fi

setup_textdomain

LOCALE_CODE="$L10N_CODE"
[ "$LOCALE_CODE" = "en" ] && LOCALE_CODE="C"


while getopts L:S:T:v ARG
do
    case $ARG in
        L)  LOCALE_CODE="$OPTARG"
            ;;
        S)  SRC_DIR="$OPTARG"
            if [ ! -d "$SRC_DIR" ]; then
                echo ''
                echolog 'Invalid directory: $2' "$SRC_DIR"
                exit 1
            fi
            ;;
        T)  
            TARGET_DIR="$OPTARG"
            ;;
        v)  VERBOSE=ON
            ;;
    esac
done
#set_commondirs ""
#check_for_l10n
#LOCALE_CODE="$L10N_CODE"
#[ "$LOCALE_CODE" = "en" ] && LOCALE_CODE="C"

#setup_textdomain
check_root
create_temp_directory
LOGFILE=""
check_logfile "makeagent"

checkIfCalledFromCD

echo ''
getSourceDirectory
checkSourceDirArch	# Find out the source architecture

# TGTARCH is set
# reset the IMGPFIX based on the target architecture
case $TGTARCH in
    sparc) IMGPFIX=""
    	DOTPFIX=""
    ;;
    i386) IMGPFIX=x86
    	DOTPFIX="../"
    ;;
esac

getSourceVersion
echo ''
getAgentTargetDirectory

#SRC_DIR and TARGET_DIR is set

ALL_SUPPORTED_OS=`find_available_os_versions`

# Get required OS versions

selectOSVersions

# Set INSTALL_LOCALES to ALL_LOCALES so that l10n packages are included in the image
ALL_LOCALES=`get_value_from_cfgfile $LOCALE_CONFIG_FILE ALL_LOCALES =`
INSTALL_LOCALES="$ALL_LOCALES"

#
#Remove disk1 and disk2 directory is already present
#

/usr/bin/rm -Rf "$TARGET_DIR"/disk1
/usr/bin/rm -Rf "$TARGET_DIR"/disk2

#
#Creeate disk1 directory
#

/usr/bin/mkdir -p "$TARGET_DIR"/disk1/${IMGPFIX}/image 
/usr/bin/mkdir -p "$TARGET_DIR"/disk2/${IMGPFIX}/image 
/usr/bin/mkdir -p "$TARGET_DIR"/disk1/${IMGPFIX}/sbin 
/usr/bin/mkdir -p "$TARGET_DIR"/disk1/${IMGPFIX}/lib
/usr/bin/mkdir -p "$TARGET_DIR"/disk1/${IMGPFIX}/install
/usr/bin/mkdir -p "$TARGET_DIR"/disk1/${IMGPFIX}/classes

/usr/bin/cp -Rf "$SRC_DIR"/../sbin "$TARGET_DIR"/disk1/${IMGPFIX}/
/usr/bin/cp -Rf "$SRC_DIR"/../lib "$TARGET_DIR"/disk1/${IMGPFIX}/
/usr/bin/cp -Rf "$SRC_DIR"/../install "$TARGET_DIR"/disk1/${IMGPFIX}/
/usr/bin/cp -Rf "$SRC_DIR"/../classes "$TARGET_DIR"/disk1/${IMGPFIX}/

[ -f "$SRC_DIR"/../.CD ] && /usr/bin/cp -fp "$SRC_DIR"/../.CD* "$TARGET_DIR"/disk1/${IMGPFIX}/

copyAgentProduct "PE" "1"
copyAddonProductsAndPatches

[ -f "$SRC_DIR"/../.CD ] && /usr/bin/cp -fp "$SRC_DIR"/../.CD* "$TARGET_DIR"/disk2/${IMGPFIX}/

makeTarFile
echolog 'Agent only product is ready in $2' "$TARGET_DIR"
echolog 'Use ${2}/disk1/sbin/es-inst -a to install the Sun Management Center Agent.' "$TARGET_DIR"
echo   " "
echolog 'Logfile: $2' "$LOGFILE"
echo   " "
echologverbose "$LOG_LINE"
echo   " "
echologverbose "$LOG_LINE"
echo   " "
remove_temp_directory
exit 0

