#!/bin/ksh

BASEDIR=$(readlink -f "$0")
BASE=$(dirname "$BASEDIR")
FIPER_HOME=`cd "$BASE/../.."; \pwd`
export FIPER_HOME

cd $FIPER_HOME/..
mkdir -p config
cd config

touch license.dat
licenseFile=$FIPER_HOME/../config/license.dat

if [ "$1" != "" ] ; then
	server1=$1
	if [ "(echo "$server1" | grep -o "@" )" ] ; then
		port1="$(echo $server1 | cut -d '@' -f1)"
		host1="$(echo $server1 | cut -d '@' -f2)"
		if [ -f $licenseFile ] ; then 
			if [ "$port1" == "" ] || [ "$port1" == "$host1" ] ; then
				echo "SERVER $host1 ANY" > "$licenseFile"
			else
				echo "SERVER $host1 ANY $port1" > "$licenseFile"
			fi					
		fi
	else
		if [ -f $licenseFile ] ; then 
			echo "SERVER $server1 ANY" > "$licenseFile"		
		fi
	fi
fi

if [ "$2" != "" ] ; then
	server2=$2
	if [ "(echo "$server2" | grep -o "@" )" ] ; then
		port2="$(echo $server2 | cut -d '@' -f1)"
		host2="$(echo $server2 | cut -d '@' -f2)"
		if [ -f $licenseFile ] ; then 
			if [ "$port2" == "" ] || [ "$port2" == "$host2" ] ; then
				echo "SERVER $host2 ANY" >> "$licenseFile"
			else
				echo "SERVER $host2 ANY $port2" >> "$licenseFile"
			fi					
		fi
	else
		if [ -f $licenseFile ] ; then 
			echo "SERVER $server2 ANY" >> "$licenseFile"		
		fi
	fi
fi

if [ "$3" != "" ] ; then
	server3=$3
	if [ "(echo "$server3" | grep -o "@" )" ] ; then
		port3="$(echo $server3 | cut -d '@' -f1)"
		host3="$(echo $server3 | cut -d '@' -f2)"
		if [ -f $licenseFile ] ; then 
			if [ "$port3" == "" ] || [ "$port3" == "$host3" ] ; then
				echo "SERVER $host3 ANY" >> "$licenseFile"
			else
				echo "SERVER $host3 ANY $port3" >> "$licenseFile"
			fi					
		fi
	else
		if [ -f $licenseFile ] ; then 
			echo "SERVER $server3 ANY" >> "$licenseFile"		
		fi
	fi
fi


echo "USE_SERVER" >> "$licenseFile"




