#!/usr/bin/ksh
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

usage()
{
    echo "$1 [-R rootdir ]"
    echo "    where -R changes the root directory for installing the patches"
    exit 1
}

apply_patch()
{
    patchadd -R "$root_dir" "$pkgdir/patches/$1"
    status=$?
    if [ $status -ne 0 ]; then
        exit $status
    fi
}

####################################################################
# Checks for the existence of a prerequisite patch. System must have
# rev number that's equal or greater than the one we wnat to install.
####################################################################
check_prerequisite_patch()
{
    # Get my patch number, e.g. for 119574-01 that would be 119574
    mypatchnum=`echo "$1" | cut -d- -f 1`
    myrev=`echo "$1" | cut -d- -f 2`

    # Get the patch installed on this system
    do_apply_patch=1
    su_patches=`showrev -p -R "$root_dir" | grep "Patch: $mypatchnum" | awk '{print $2}'`
    for syspatch in $su_patches; do
        syspatchnum=`echo "$syspatch" | cut -d- -f 1`
        if [ "$syspatchnum" == "$mypatchnum" ]; then
            # some rev of this patch is installed on the system
            # is it higher than ours?
            sysrev=`echo "$syspatch" | cut -d- -f 2`
            if [ "$sysrev" -ge "$myrev" ]; then
                do_apply_patch=0
            fi
        fi
    done
    return $do_apply_patch
}


####################################################################
#    MAIN
####################################################################

myuid=`id | sed 's/^[^=]*=\([0-9]*\).*/\1/'`
if [ $myuid -ne 0 ]; then
    echo You must be root to run this script
    exit 1
fi

#default
root_dir=/
while getopts :R:h opt $@
do
    case $opt in
        R) root_dir="$OPTARG"
        ;;
        h) usage
    esac
done

pkgdir=`dirname $0`
if [ "$pkgdir" == "" ]; then
    pkgdir=.
fi

# Generate file that contains packages and their version numbers
this_platform=`uname -p`
if [ "$this_platform" == "sparc" ]; then
    patch1=119574-02
    patch2=119254-06
    patch3=119107-06
    patch4=120335-02
    patch5=120776-03
else
    patch1=119575-02
    patch2=119255-06
    patch3=119108-06
    patch4=120336-02
    patch5=120777-03
fi

# Do we already have this patch on the system?
p=`showrev -p -R "$root_dir" |  grep "Patch: $patch3" | awk '{print $2}'`
if [ "$p" == "$patch3" ]; then

    # see if we need to apply the L10N part
    p=`showrev -p -R "$root_dir" |  grep "Patch: $patch4" | awk '{print $2}'`
    if [ "$p" != "$patch4" ]; then
        apply_patch $patch4
    fi

    echo $patch3 is already installed on your system. Exiting..
    exit 0
fi

####################################################
# su patch
####################################################
check_prerequisite_patch $patch1
if [ $? -eq 1 ]; then
    # patch does not exist on this system
    echo Adding patch $patch1
    apply_patch $patch1
fi

check_prerequisite_patch $patch2
if [ $? -eq 1 ]; then
    # patch does not exist on this system
    echo Adding patch $patch2
    apply_patch $patch2
fi

echo Adding patch $patch3
apply_patch $patch3

echo Adding patch $patch4
apply_patch $patch4

echo Adding patch $patch5
apply_patch $patch5

exit 0



