#!/bin/bash
# Script d'affichage des infos de version d'une application
# Portage de getinfoversion.sh from OTU8000v2
#
# La syntax avec RESCUE a été gradée pour compatibilité : le rescue est la partition non utilisée
#

#set -x

# Get partition information
#source /etc/platform.conf
source /etc/otu/otuparts.sh
[ -f /etc/profile.d/40_otu_platform.sh ] && source /etc/profile.d/40_otu_platform.sh

APP_VERSION_DIR=/etc/release/version
ACTERNADIR=/etc/acterna

RESCUE_PART=/mnt/fs/unused_part

# Keep usage here, easier to find :)
usage () {
   echo "usage:  $0 [-r] application_name"
   exit 1
}

# Get commandline options
while getopts [r:] opt
do
    case "$opt" in
      r) RESCUE=yes
         APPFILE="$OPTARG".ver
         ;;
      \?) usage;;
    esac
done

# Check parameter
if [ "${RESCUE}" == "yes" ]
then
   if [ -z ${RESCUE_PART} ] ; then
	  echo "ERROR : Recue file system not mounted"
	  exit 1
   fi
   APP_VERSION_DIR=${RESCUE_PART}${APP_VERSION_DIR}
   ACTERNADIR=${RESCUE_PART}${ACTERNADIR}
else
   if [ $# -eq 1 ];
   then
       APPFILE="$1".ver
   else
       echo "ERROR : You must specify an application file name"
       exit 1
   fi
fi

# Check .ver file
VERFILE=""

if [ -e ${APP_VERSION_DIR}/${APPFILE} ]
then
    VERFILE="${APP_VERSION_DIR}/${APPFILE}"
elif [ -e ${ACTERNADIR}/${APPFILE} ]
then
    VERFILE="${ACTERNADIR}/${APPFILE}"
else
    echo "ERROR : ${APPFILE} not found"
    exit 1
fi

# Create string
INFOVER=$(grep -v "^#" ${VERFILE} | tr "\n" ";" | sed '$ s/;*$//')
echo ${CFG_MKT_PLATFORM_NAME}";"${INFOVER}

exit 0
