#!/bin/sh
#
#   Copyright (C) 2014 Masatake YAMATO
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.

CTAGS=./ctags

line()
{
    local i
    for i in $(seq 72); do
	echo -n -e -
    done
    echo
}

header()
{
    echo
    echo "$1"
    line

}

check_include_general_h_first()
{
    local f
    local l
    local i=0

    header "Check whether general.h is included first: $1"
    # Added -maxdepth 1 to skip parsers/cxx
    for f in $(find $1 -maxdepth 1 -name '*.c'); do
	if grep -a -q -e '^#[[:space:]]*include' $f; then
	    l=$()
	    if ! ( grep -a -e '^#[[:space:]]*include' $f | head -1 | grep -q "general.h" ); then
		i=$(expr $i + 1)
		echo "$f: general.h should be included FIRST" 2>&1
	    fi
	fi
    done

    return $i
}

check_name_cpp_macro()
{
    local dir=$1
    local r=0
    local n

    header "Check whether '_' is not used as ctags own macro name"
    for f in $(find $dir -name '*.[ch]' | grep -v portable-dirent_p.h); do
	if ${CTAGS} --language-force=C -x --_xformat='%F:%N' --kinds-C=d -o - $f | grep -q '.*:_.*H'; then
	    for n in $(${CTAGS} --language-force=C -x --_xformat='%N' --kinds-C=d -o - $f | grep  '^_.*H'); do
		echo "#" $n
		echo sed -i \""s|$n|CTAGS_$(echo $dir | tr a-z A-Z)_${n#_}|g\"" $f
	    done
	   r=1
	fi
    done
    return $r
}

check_vStringCatS_usage()
{
    local i=0

    header "Check wrong vStringCatS usage(use vStringPut instead): $1"
    for f in $(find $1 -name '*.c'); do
	if grep -H -a -e 'vStringCatS[[:space:]]*(.*,[[:space:]]*"."[[:space:]]*)' $f; then
	    i=$(expr $i + 1)
	elif grep -H -a -e 'vStringCatS[[:space:]]*(.*,[[:space:]]*"\\."[[:space:]]*)' $f; then
	    i=$(expr $i + 1)
	fi
    done

    return $i
}

check_eof_chars_in_vcxproj()
{
    local r=4
    local f

    # *.vcxproj* should not have the last CRLF.
    for f in win32/ctags_vs2013.vcxproj.in win32/ctags_vs2013.vcxproj.filters.in \
             win32/ctags_vs2013.vcxproj    win32/ctags_vs2013.vcxproj.filters; do
	header "Check the EOF characters of $f"
	local s=$(od -t c  -j $(expr $(stat -c %s $f) - 1) $f)
	if echo "$s" | grep -q '>$'; then
	    r=$(expr $r - 1)
	else
	    echo "unexpected chars: $s"
	fi
    done

    return $r
}

main()
{
    local i=0

    if ! [ -d ./main ]; then
	echo "cannot find ./main"
	return 2
    fi

    if ! [ -d ./parsers ]; then
	echo "cannot find ./parsers"
	return 2
    fi

    if ! check_include_general_h_first main; then
	i=$(expr $i + 1)
	echo "failed"
    else
	echo "ok"
    fi

    if ! check_include_general_h_first parsers; then
	i=$(expr $i + 1)
	echo "failed"
    else
	echo "ok"
    fi

    if ! check_name_cpp_macro main; then
	i=$(expr $i + 1)
	echo "failed"
    else
	echo "ok"
    fi

    if ! check_name_cpp_macro parsers; then
	i=$(expr $i + 1)
	echo "failed"
    else
	echo "ok"
    fi

    if ! check_vStringCatS_usage main; then
	i=$(expr $i + 1)
	echo "failed"
    else
	echo "ok"
    fi

    if ! check_vStringCatS_usage parsers; then
	i=$(expr $i + 1)
	echo "failed"
    else
	echo "ok"
    fi

    if ! check_eof_chars_in_vcxproj; then
	i=$(expr $i + 1)
	echo "failed"
    else
	echo "ok"
    fi

    return $i
}

main "$@"
exit $?
