#!/usr/dt/bin/dtksh
#
# Solaris patch checker script
#
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Initial Developer of the Original Code is
# Roland Mainz <roland.mainz@nrubsig.org>
# Portions created by the Initial Developer are Copyright (C) 2004
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****

export PATH=/usr/xpg4/bin:/usr/sbin:/usr/bin

# Enumerate installed patches
enumerate_patch_list()
{
    showrev -p | sort -n |grep "^Patch: "
    #cat patchlist.txt | grep "^Patch: "
}

# Setup list of required patches
typeset -A required_patches
case "$(uname -p)" in
  "sparc")
      case "$(uname -r)" in
        # Solaris 2.7/SPARC
        "5.7")
            required_patches=(
              [0]="106300-22" [0_title]="64-Bit Shared library patch for C++"
              [1]="106327-21" [1_title]="32-Bit Shared library patch for C++"
              [2]="106950-24" [2_title]="SunOS 5.7: linker patch"
              [3]="107650-08" [3_title]="OpenWindows 3.6.1 X11R6.4 Xprint Extension Patch"
              [4]="107636-10" [4_title]="SunOS 5.7: X Input & Output Method patch"
              [5]="108376-44" [5_title]="OpenWindows 3.6.1: Xsun Patch"
              [6]="112689-02" [6_title]="SunOS 5.7: UTF-8 locale ICONV patch"
            )
            num_required_patches=7
            ;;

        # Solaris 2.8/SPARC
        "5.8")
            required_patches=(
              [0]="108434-14" [0_title]="32-Bit Shared library patch for C++"
              [1]="108435-14" [1_title]="64-Bit Shared library patch for C++"
              [2]="108652-79" [2_title]="X11 6.5.1: Xsun patch"
              [3]="108773-18" [3_title]="SunOS 5.8: IIIM and X Input & Output Method patch"
              [4]="109147-27" [4_title]="SunOS 5.8: linker patch"
              [5]="109159-03" [5_title]="SunOS 5.8: Chinese iconv module updates"
              [6]="109704-03" [6_title]="SunOS 5.8: Japanese iconv patch"
              [7]="111721-04" [7_title]="SunOS 5.8: Math Library (libm) patch"
              [8]="113261-02" [8_title]="SunOS 5.8: UTF-8 locale ICONV patch"
              [9]="114542-01" [9_title]="SunOS 5.8: Adds extended Arabic support"
            )
            num_required_patches=10
            ;;

        # Solaris 2.9/SPARC
        "5.9")
            required_patches=(
              [0]="111711-08" [0_title]="32-Bit Shared library patch for C++"
              [1]="111712-08" [1_title]="64-Bit Shared library patch for C++"
              [2]="111722-04" [2_title]="SunOS 5.9: Math Library (libm) patch"
              [3]="112661-06" [3_title]="SunOS 5.9: IIIM and X Input & Output Method patch"
              [4]="112785-34" [4_title]="X11 6.6.1: Xsun patch"
              [5]="112963-10" [5_title]="SunOS 5.9: linker patch"
              [6]="113902-03" [6_title]="SunOS 5.9: Asian UTF-8 iconv modules enhancement"
              [7]="114276-02" [7_title]="SunOS 5.9: Extended Arabic support in UTF-8"
              [8]="114641-02" [8_title]="SunOS 5.9: Japanese iconv for UTF-8 patch"
            )
            num_required_patches=9
            ;;
        *)
            echo "# WARNING: Patch checker not supported for $(uname -s)."
            exit 0
            ;;
      esac
      ;;
  "i386")
      case "$(uname -r)" in
        # Solaris 2.7/x86
        "5.7")
            required_patches=(
              [0]="106328-19" [0_title]="SunOS 5.7_x86: Shared library patch for C++"
              [1]="106951-25" [1_title]="SunOS 5.7_x86: Linker Patch"
              [2]="107651-08" [2_title]="OpenWindows 3.6.1_x86: X11R6.4 Xprint Extension Patch"
              [3]="107637-10" [3_title]="SunOS 5.7_x86: X Input & Output Method patch"
              [4]="108377-39" [4_title]="OpenWindows 3.6.1_x86: Xsun Patch"
              [5]="112690-01" [5_title]="SunOS 5.7_x86: UTF-8 locale ICONV patch"
            )
            num_required_patches=6
            ;;

        # Solaris 2.8/x86
        "5.8")
            required_patches=(
              [0]="108436-13" [0_title]="SunOS 5.8_x86 Shared library patch for C++"
              [1]="108653-68" [1_title]="X11 6.4.1_x86: Xsun patch"
              [2]="108774-18" [2_title]="SunOS 5.8_x86: IIIM and X Input & Output Method patch"
              [3]="109148-28" [3_title]="SunOS 5.8_x86: linker patch"
              [4]="109160-03" [4_title]="SunOS 5.8_x86: Chinese iconv module updates"
              [5]="109705-03" [5_title]="SunOS 5.8_x86: Japanese iconv patch"
              [6]="112757-01" [6_title]="SunOS 5.8_x86: Math Library (libm) patch"
              [7]="113262-02" [7_title]="SunOS 5.8_x86: UTF-8 locale ICONV patch"
              [8]="114543-01" [8_title]="SunOS 5.8_x86: Adds extended Arabic support"
            )
            num_required_patches=9
            ;;

        # Solaris 2.9/x86
        "5.9")
            required_patches=(
              [0]="111713-06" [0_title]="SunOS 5.9_x86: Shared library patch for C++"
              [1]="111728-03" [1_title]="SunOS 5.9_x86: Math Library (libm) patch"
              [2]="112662-04" [2_title]="SunOS 5.9_x86: IIIM and X Input & Output Method patch"
              [3]="112786-22" [3_title]="X11 6.6.1_x86: Xsun patch"
              [4]="113986-06" [4_title]="SunOS 5.9_x86: linker Patch"
              [5]="113903-03" [5_title]="SunOS 5.9_x86: Asian UTF-8 iconv modules enhancement"
              [6]="114277-02" [6_title]="SunOS 5.9_x86: Extended Arabic support in UTF-8 "
              [7]="114642-02" [7_title]="SunOS 5.9_x86: Japanese iconv for UTF-8 patch"
            )
            num_required_patches=8
            ;;

        *)
            echo "# WARNING: Patch checker not supported for $(uname -s)."
            exit 0
            ;;
      esac
      ;;
  *)
      echo "# ERROR: Patch checker not supported for $(uname -p)."
      exit 1
      ;;
esac


typeset -A patchmap

# Fill patchmap
enumerate_patch_list | while read i1 i2 i3 ; do
  patch_id="${i2%-*}"
  patch_rev="${i2#*-}"
  patchmap[${patch_id}]="${patch_rev}"
done

# Check patch requirements against the list of installed patches and
# print out missing patches ...
missing_patches_msg="$(
count=0
while [ ${count} -lt ${num_required_patches} ] ; do
  req_patch_id="${required_patches[${count}]%-*}"
  req_patch_rev="${required_patches[${count}]#*-}"

  if [ "${patchmap[${req_patch_id}]}" = "" ] ; then
    echo "- patch ${req_patch_id} (\"${required_patches[${count}_title]}\"). Install ${required_patches[${count}]} (or higher)."
  fi
  
  count=$((${count} + 1))
done
)"

# ... and then the patches which need updates:
low_rev_patches_msg="$(
count=0
while [ ${count} -lt ${num_required_patches} ] ; do
  req_patch_id="${required_patches[${count}]%-*}"
  req_patch_rev="${required_patches[${count}]#*-}"

  if [ "${patchmap[${req_patch_id}]}" != "" ] ; then
    if [ ${req_patch_rev} -gt ${patchmap[${req_patch_id}]} ] ; then
      echo "- patch ${required_patches[${count}]} (or higher), currently installed patch ${req_patch_id}-${patchmap[${req_patch_id}]} (\"${required_patches[${count}_title]}\")."
    fi
  fi
  
  count=$((${count} + 1))
done
)"

# Build message text
display_warning=false
# Intro...
msg=" \
One or more of the following Solaris patches are missing in your OS which are requirements to run Mozilla.\n \
Please contact your system adminstrator to install these patches. \n \
"
# ... missing patches ...
if [ "${missing_patches_msg}" != "" ] ; then
    msg="$(printf "%s\n%s\n%s\n " "${msg}" "* The following patches are not installed:" "${missing_patches_msg}")"
    display_warning=true
fi
# ... and the too old ones ...
if [ "${low_rev_patches_msg}" != "" ] ; then
    msg="$(printf "%s\n%s\n%s\n " "${msg}" "* The following patches need to be updated to higher revisions:" "${low_rev_patches_msg}")"
    display_warning=true
fi

# Do we really need to display the dialog ?
if [ "${display_warning}" = "false" ] ; then
    exit 0
fi

# Display warning dialog
dismissCB()
{
   exit 0
}

exitCB()
{
   exit 1
}

XtInitialize TOPLEVEL moz_patch_error MozPatchError "$@"

XmCreateMessageBox MISSING_PATCHES_DIALOG ${TOPLEVEL} missing_patches_dialog \
  messageString:"${msg}" \
  dialogTitle:"Mozilla patch checker" \
  okLabelString:"Ignore problem and start Mozilla" \
  cancelLabelString:"Exit" \
  dialogType:DIALOG_ERROR
XmMessageBoxGetChild CANCEL_BUTTON ${MISSING_PATCHES_DIALOG} DIALOG_CANCEL_BUTTON
XmMessageBoxGetChild HELP_BUTTON   ${MISSING_PATCHES_DIALOG} DIALOG_HELP_BUTTON

# Remove the unwanted help button
XtUnmanageChild ${HELP_BUTTON}

XtAddCallback ${MISSING_PATCHES_DIALOG} okCallback dismissCB 
XtAddCallback ${MISSING_PATCHES_DIALOG} cancelCallback exitCB 

XtDisplay DISP ${TOPLEVEL}
XmInternAtom CLOSE_ATOM ${DISP} WM_DELETE_WINDOW false
XmAddWMProtocolCallback ${TOPLEVEL} ${CLOSE_ATOM} exitCB
 
XtSetValues ${TOPLEVEL} deleteResponse:DO_NOTHING

XtManageChild ${MISSING_PATCHES_DIALOG}

XtRealizeWidget ${TOPLEVEL}

XtMainLoop

echo "# should not be hit."
exit 255
# EOF.
