##
 #
 # Licensed to the Apache Software Foundation (ASF) under one
 # or more contributor license agreements.  See the NOTICE file
 # distributed with this work for additional information
 # regarding copyright ownership.  The ASF licenses this file
 # to you under the Apache License, Version 2.0 (the
 # "License"); you may not use this file except in compliance
 # with the License.  You may obtain a copy of the License at
 #
 #   http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing,
 # software distributed under the License is distributed on an
 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
 #
##


##
 #
 #
 #
##

defaultpath="../autotest/tests/"

##
 # regex
##
# regex for id'ing a spec
specreg="^[ \t]*it(\".*"
# regex for id'ing a tagged spec
tagged=".*spec\.[0-9]*"
# default suite name
suitename="SUITE"

toetagtargets() {
    for file in ${targets[@]}
    do
        echo toetagging $file
        suitename $file
        echo Suite Name: $suitename
        found=$(grep -c "$specreg" $defaultpath$file)
        foundtagged=$(grep -c "$tagged" $defaultpath$file)
        echo found $found specs to toetag
        echo found $foundtagged specs already tagged
        tagcode=0
        declare -i tagcode
        temp=$IFS
        IFS="
"
        totag=$(grep $specreg $defaultpath$file | grep -v $tagged)
        for line in $totag
        do
            tagcode=$tagcode+1
            modline=$(echo "$line" | sed s/it\(\"/it\(\"$suitename.spec.$tagcode\ /)
            echo "$line" 
            sed -i '.bak' s#"$line"#"$modline"# $defaultpath$file
        done
        echo 
        IFS=$temp
        echo 
	    echo
    done
}

##
 # suite name is first segment of file name delimited by '.'
##
suitename() {
    temp=$IFS
    IFS='.'
    read -ra suitename <<< "$1"
    IFS=$temp
}

# read all entries in default_path
read -a targets <<< $(ls $defaultpath)
toetagtargets
