#!/bin/bash

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

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

cp /sapmnt/$SAP_T/exe/uc/linuxx86_64/saprouter /usr/sbin/saprouter
systemctl daemon-reload
systemctl enable saprouter

