#!/bin/sh

# **************************** LICENSE START ***********************************
#
# Copyright 2013 ECMWF and INPE. This software is distributed under the terms
# of the Apache License version 2.0. In applying this license, ECMWF does not
# waive the privileges and immunities granted to it by virtue of its status as
# an Intergovernmental Organization or submit itself to any jurisdiction.
#
# ***************************** LICENSE END ************************************

f_INP=$1
suffix=""

if [ $# -eq 2 ] ; then 
	suffix=$2
fi

d=`dirname "$f_INP"`
n=`basename "$f_INP" "$suffix"`
cd "$d"

if `find -maxdepth 1 -name "$n" | wc -l` -eq 1 ] ; then
	echo $d
	exit
fi 

maxNum=`find -maxdepth 1 -name "*" | wc -l`

i=1
fn="$n"_$i
while [ $i -lt $maxNum ]  ; do	
	fn="$n"_$i
	if [ `find -maxdepth 1 -name "$fn" | wc -l` -eq 0 ] ; then
		echo $fn
		exit
	fi
	result=`expr $a + $a`
	i=`expr $i + 1`
done

echo "$n"_$maxNum

exit