
# **************************** LICENSE START ***********************************
#
# Copyright 2012 ECMWF and INPE. This software is distributed under the terms
# of the Apache License version 2.0. In applying this license, ECMWF does not
# waive the privileges and immunities granted to it by virtue of its status as
# an Intergovernmental Organization or submit itself to any jurisdiction.
#
# ***************************** LICENSE END ************************************

##################################################################
## Apply patches
##
## The idea is that we maintain a file in ~/metview/System/.PatchNumber
## which keeps track of the last patch to be applied to this Metview
## user directory so that we don't unnecessarily test for patches
## each time Metview is started up. We also maintain a log file
## containing details of the patches applied.
## Each patch should have a meaningful test to ensure that it is only
## applied if necessary - and always consider what should happen if
## a user is running Metview for the first time.
## Note that we use integers as patch numbers in order to avoid
## problems with LANG environments (confusion between '.' and ',').
## This means that we have the 'major number' of the patch being
## multiplied by 10, e.g. 14: major=1,minor=4.
##################################################################

MVU=$METVIEW_USER_DIRECTORY
PATCH_FILE=$MVU/System/.PatchNumber
PATCH_LOG=$MVU/System/.PatchLog
LAST_PATCH_IN_THIS_FILE=14 # MUST be updated if a new patch is added

LAST_PATCH_APPLIED=0
if [ -f $PATCH_FILE ] # read the last patch applied from file
then
    LAST_PATCH_APPLIED=`cat $PATCH_FILE`
fi




if [ $LAST_PATCH_APPLIED -lt $LAST_PATCH_IN_THIS_FILE ] # any action required?
then

    # patch 1
    if [ $LAST_PATCH_APPLIED -lt 14 ]
    then
        # FUNCTION: update_vis_icon - updates a Visualiser icon
        update_vis_icon() {
            ICON=$1
            DRAWER=$2
            BADSTRING=$3
            PATCH_NUM=$4

            SYS_DRAWER="$METVIEW_DIR_SHARE/app-defaults/Drawers/$DRAWER"
            USER_DRAWER="$MVU/System/Drawers/$DRAWER"

            if [ -f "$MVU/System/Defaults/$ICON" ] && [ `grep -c $BADSTRING "$MVU/System/Defaults/$ICON"` != 0 ]
            then
                rm -f "$MVU/System/Defaults/$ICON" ; rm -f "$MVU/System/Defaults/.$ICON"
                echo "Patch ${PATCH_NUM}-a (Odb Visualiser default) applied on `date`" >> $PATCH_LOG
            fi

            if [ -f "$USER_DRAWER/$ICON" ] && [ `grep -c $BADSTRING "$USER_DRAWER/$ICON"` != 0 ]
            then
                echo "Updating $ICON drawer icon to latest version"
                rm -f  "$USER_DRAWER/$ICON" ; rm -f  "$USER_DRAWER/.$ICON"
                cp "$SYS_DRAWER/$ICON"   "$USER_DRAWER" ; cp "$SYS_DRAWER/.$ICON"  "$USER_DRAWER"
                echo "Patch ${PATCH_NUM}-b ($ICON icon) applied on `date`" >> $PATCH_LOG
            fi
        }


        update_vis_icon "Input Visualiser"  "Modules (Plotting)"  INPUTVISUALISER_FAMILY  11
        update_vis_icon "Odb Visualiser"    "Modules (Plotting)"  ODBVISUALISER_FAMILY    12
        update_vis_icon "Table Visualiser"  "Modules (Plotting)"  TABLEVISUALISER_FAMILY  13
        update_vis_icon "NetCDF Visualiser" "Modules (Plotting)"  NETCDFPLUS_FAMILY       14
    fi


    echo $LAST_PATCH_IN_THIS_FILE > $PATCH_FILE
fi
