#!/bin/sh

CONFIG="$SYNOPKG_PKGDEST/userdata/config.properties"
PRODUCT='NAKIVO Backup & Replication'


echo_released_date_by_version() {
    version_released_date_map="
        10.10.1_2023-12-06
        10.10.0_2023-10-09
        10.9.0_2023-07-04
        10.8.0_2023-01-23
        10.7.2_2022-11-07
        10.7.1_2022-10-12
        10.7.0_2022-08-31
        10.6.1_2022-06-09
        10.6.0_2022-05-03
        10.5.1_2021-12-22
        10.5.0_2021-11-16
        10.4.1_2021-10-26
        10.4.0_2021-08-02
        10.3.0_2021-05-04
        10.2.0_2021-01-25
        10.1.1_2020-10-30
        10.1.0_2020-09-21
        10.0.1_2020-08-26
        10.0.0_2020-07-23
        9.4.0_2020-06-03
    "

    result=`date +%Y-%m-%d`
    for i in $version_released_date_map; do
        version=`echo $i | cut -d '_' -f 1`
        release=`echo $i | cut -d '_' -f 2`
        if [ $version = ${1} ]; then
            result=$release
            break
        fi
    done

    echo $result
}

#Update forbidden
if ( false ); then
    echo "An older version is installed on this machine and cannot be updated with this Beta installer. \
Beta versions of the software are intended for testing purposes and should not be used in a production environment. \
Please install the Beta version on a different machine." >>$SYNOPKG_TEMP_LOGFILE
    exit 1
fi

#Maintenance
if ( true ); then
    if [ -e "$CONFIG" ]; then
       PRODUCT_MED=`sed -n '\!^med=\(.*\)$!s!!\1!p' "$CONFIG" 2>/dev/null`
    fi

    if [ "x${PRODUCT_MED}" != "x" -a ${PRODUCT_MED:-0} -lt 1780272000000 ]; then
        echo "The update cannot be started. The maintenance has ended. \
You are no longer eligible for technical support and software updates. \
Contact sales@nakivo.com to renew the maintenance." >>$SYNOPKG_TEMP_LOGFILE
        exit 1
    fi
fi

#Version update check
if [ -e "$SYNOPKG_PKGDEST/userdata/version" -a -e "$SYNOPKG_PKGDEST/userdata/db/version" ]; then
    MAJOR=`cat "$SYNOPKG_PKGDEST/userdata/version"`
    BUILD=`echo "$MAJOR" | cut -d . -f 4`
    if [ "x$BUILD" != "x" ]; then
        curver=`echo "$MAJOR" | cut -d . -f 1,2,3`
        curbuild="$BUILD"
    else
        BUILD=`cat "$SYNOPKG_PKGDEST/userdata/db/version"`
        if [ "x$BUILD" != "x" ]; then
            curver="$MAJOR"
            curbuild="$BUILD"
        fi
    fi
fi
if [ "x${curver}" != "x" -a "x${curbuild}" != "x" ]; then
    curmajor=`echo $curver | cut -d '.' -f 1`
    curminor=`echo $curver | cut -d '.' -f 2`
    curpatch=`echo $curver | cut -d '.' -f 3`
    newmajor=`echo "11.2.1.104556-60026" | cut -d '.' -f 1`
    newminor=`echo "11.2.1.104556-60026" | cut -d '.' -f 2`
    newpatch=`echo "11.2.1.104556-60026" | cut -d '.' -f 3`
    newbuild=`echo "11.2.1.104556-60026" | cut -d '-' -f 1 | cut -d '.' -f 4`
    
    current_date=`date +%Y-%m-%d`
    released_date=`echo_released_date_by_version $curver`
    end_support_date=`date -d "$released_date +3 years" +%Y-%m-%d`
    if [ $curmajor -lt 9 ] || [ "$current_date" ">" "$end_support_date" ] || [ "$current_date" "=" "$end_support_date" ]; then
         echo "$PRODUCT cannot be updated from version $curver.$curbuild to version 11.2.1.104556-60026. \
         Please, contact support@nakivo.com for assistance." >>$SYNOPKG_TEMP_LOGFILE
         exit 1
    fi

    if [ $newmajor -lt $curmajor -o \
         \( $newmajor -eq $curmajor -a $newminor -lt $curminor \) -o \
         \( $newmajor -eq $curmajor -a $newminor -eq $curminor -a $newpatch -lt $curpatch \) -o \
         \( $newmajor -eq $curmajor -a $newminor -eq $curminor -a $newpatch -eq $curpatch -a $newbuild -lt $curbuild \) -o \
         $curbuild -eq 24992 -o \
         $curbuild -eq 27551 -o \
         $curbuild -eq 31961 -o \
         $curbuild -eq 33580 -o \
         $curbuild -eq 37258 -o \
         $curbuild -eq 39609 -o \
         $curbuild -eq 41289 -o \
         $curbuild -eq 41297 -o \
         $curbuild -eq 42905 -o \
         $curbuild -eq 42915 -o \
         $curbuild -eq 44587 -o \
         $curbuild -eq 46141 -o \
         $curbuild -eq 49754 -o \
         $curbuild -eq 53138 -o \
         $curbuild -eq 55763 -o \
         $curbuild -eq 59039 -o \
         $curbuild -eq 63695 -o \
         $curbuild -eq 70318 -o \
         $curbuild -eq 74160 -o \
         $curbuild -eq 76636 -o \
         $curbuild -eq 79108 -o \
         $curbuild -eq 86936 -o \
         $curbuild -eq 92871 -o \
         $curbuild -eq 95454 -o \
         $curbuild -eq 100114 \
    ]; then
        echo "Update of current version is not supported." >>$SYNOPKG_TEMP_LOGFILE
        exit 1
    fi
fi

#Transporter
if [ -e "$SYNOPKG_PKGDEST/transporter" ]; then
    TRANSPORTER_ROOT="$SYNOPKG_PKGDEST/transporter"
    TRANSPORTER_TEMP="$SYNOPKG_PKGINST_TEMP_DIR/transporter"
else
    TRANSPORTER_ROOT="$SYNOPKG_PKGDEST"
    TRANSPORTER_TEMP="$SYNOPKG_PKGINST_TEMP_DIR"
fi

if [ "$(find "$TRANSPORTER_ROOT/bh-mount" -type f)" ]; then
    sleep 5
    if [ "$(find "$TRANSPORTER_ROOT/bh-mount" -type f)" ]; then
        echo "Mount point(s) detected in \"bh-mount\" directory of the onboard transporter. \
        Make sure the \"bh-mount\" folder on the onboard transporter does not contain mount points, \
        check processes which can be using files from \"bh-mount\" directory, and try again." >>$SYNOPKG_TEMP_LOGFILE
        exit 1
    fi
fi

if ls -1 "$TRANSPORTER_ROOT/bh-logs" 2>/dev/null | grep -qE "\.log$"; then
    ARCHIVE=`date '+bh-logs.%F-%H:%M:%S.tgz'`
    tar --force-local -czf "$TRANSPORTER_ROOT/bh-logs/$ARCHIVE" -C "$TRANSPORTER_ROOT/bh-logs" --exclude='*.tgz' . 2>/dev/null
fi

KEEP_LIST="bh-logs bh-base bh-mount bh-cfg bh-vddk bhsvc.id certificate.pem"
for KEEP in $KEEP_LIST; do
    [ -e "$TRANSPORTER_ROOT/$KEEP" ] && cp -r "$TRANSPORTER_ROOT/$KEEP" "$TRANSPORTER_TEMP/"
done

if [ -x /usr/bin/diff -a -d "$TRANSPORTER_ROOT/scripts" ]; then
    SCRIPTS_LIST=`ls -1 "$TRANSPORTER_ROOT/scripts" 2>/dev/null | grep -vE '\.old$'`
    for script in $SCRIPTS_LIST; do
        /usr/bin/diff -q "$TRANSPORTER_ROOT/scripts/$script" "$TRANSPORTER_TEMP/scripts/$script" >/dev/null 2>&1
        [ $? -ne 0 ] && cp "$TRANSPORTER_ROOT/scripts/$script" "$TRANSPORTER_TEMP/scripts/${script}.old"
    done
fi

#Director
if [ -e "$SYNOPKG_PKGDEST/nkv-dirsvc" ]; then
    mkdir "$SYNOPKG_PKGINST_TEMP_DIR/localization"
    LOCALIZATION_FILES=$(ls "$SYNOPKG_PKGINST_TEMP_DIR/userdata" | grep -E 'localization-bundle.*\.zip$')
    if [ ! -z "$LOCALIZATION_FILES" ]; then
        mv "$SYNOPKG_PKGINST_TEMP_DIR/userdata/"localization-bundle*.zip "$SYNOPKG_PKGINST_TEMP_DIR/localization"
    fi

    KEEP_LIST="userdata logs tomcat/cert"
    for KEEP in $KEEP_LIST; do
        if [ -e "$SYNOPKG_PKGDEST/$KEEP" ]; then
            rm -rf "$SYNOPKG_PKGINST_TEMP_DIR/$KEEP" 2>/dev/null
            cp -r "$SYNOPKG_PKGDEST/$KEEP" "$SYNOPKG_PKGINST_TEMP_DIR/$KEEP"
        fi
    done

    if [ ! -z "$LOCALIZATION_FILES" ]; then
       mv "$SYNOPKG_PKGINST_TEMP_DIR/localization/"localization-bundle*.zip "$SYNOPKG_PKGINST_TEMP_DIR/userdata"
    fi

    rm -rf "$SYNOPKG_PKGINST_TEMP_DIR/localization"
    #backup copy
    cp "$SYNOPKG_PKGDEST/nkv-dirsvc" "$SYNOPKG_PKGINST_TEMP_DIR/nkv-dirsvc.old"
fi

exit 0
