#!/bin/bash

#list the systems installed on the host
if [ -d /sapmnt ]
then
    SAP=(`ls /sapmnt`)
    VER=0
    SAP_T=""
else
    exit 0
fi

#get the arch
arch=`uname -m`
for i in "${SAP[@]}"
do
    if [ -f /sapmnt/$i/exe/uc/linux${arch}/saprouter ]
    then
    sapadm=`echo $i | awk '{ print tolower($0) "adm"}'`
    VER_N=`sudo -i -u $sapadm /sapmnt/$i/exe/uc/linux${arch}/saprouter | head -3 | grep "SAP Network Interface Router, Version" | awk -F " " '{ print $6}'`
    SAP_T=$i
    if [ $VER_N -gt $VER ]
         then
             SAP_T=$i
             VER=$VER_N
         fi
    fi
done
[ -z $SAP_T ] && exit 2

cp /sapmnt/${SAP_T}/exe/uc/linux${arch}/saprouter /usr/sbin/saprouter
systemctl daemon-reload
systemctl enable saprouter

