#!/bin/bash
#

PRODUCT_NAME=appliance

BOGEN_ROOT=/opt/bogen
BOGEN_INCOMING_DIR=$BOGEN_ROOT/incoming
BOGEN_PREV_VER_DIR=$BOGEN_ROOT/previous
BOGEN_FACT_DEF_DIR=$BOGEN_ROOT/default
BOGEN_ARCHIVE_DIR=$BOGEN_ROOT/archive

if [ -e "${BOGEN_ROOT}/${PRODUCT_NAME}/VERSION.config" ]; then
	CURRENT_NYQUIST_VERSION=$(cat $BOGEN_ROOT/$PRODUCT_NAME/VERSION.config)
else
	CURRENT_NYQUIST_VERSION=0.0.0
fi


count=`ls -1 $BOGEN_INCOMING_DIR/*.tar.gz 2>/dev/null | wc -l`
if [ $count != 0 ]
then
        NEW_NYQUIST_FILE=$(ls $BOGEN_INCOMING_DIR/*tar.gz)
        TMP_NYQUIST_VERSION=$(ls $BOGEN_INCOMING_DIR/*tar.gz | cut -d'-' -f2)
        NEW_NYQUIST_VERSION=$(echo $TMP_NYQUIST_VERSION | cut -f1,2,3 -d'.')
        #echo " -- Update to version $NEW_NYQUIST_VERSION is waiting to be installed"
else
        #echo " -- No updates pending install"
	NEW_NYQUIST_VERSION=None
fi

: <<'END'
if [ $NEW_NYQUIST_VERSION == $CURRENT_NYQUIST_VERSION ]; then
       #echo " -- Nyquist version $NEW_NYQUIST_VERSION is already installed"
        exit 0
elif [ "$NEW_NYQUIST_VERSION" \< "$CURRENT_NYQUIST_VERSION" ]; then
       #echo " -- Nyquist version $NEW_NYQUIST_VERSION is older than current version ($CURRENT_NYQUIST_VERSION) already installed"
        exit 0
else
       #echo " -- Nyquist version $NEW_NYQUIST_VERSION is newer than current version ($CURRENT_NYQUIST_VERSION) already installed"
fi
END

echo "$CURRENT_NYQUIST_VERSION|$NEW_NYQUIST_VERSION"

