#!/bin/sh -e
#
# $Id$
# Copyright (C) 2005  Sir Raorn <raorn@altlinux.org>
# 
# find-tags utility
#
# This file 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

PROG="${0##*/}"

if [ "$0" = "./$PROG" ]; then
    etc="$(pwd)/etc"
else
    etc="/etc/find-tags"
fi

[ -f "$etc/find-tags.conf" ] && . "$etc/find-tags.conf" ||:

Usage()
{
    [ "$1" = "0" ] || exec >&2
    cat <<EOF
Usage: $PROG [OPTIONS... ] -- OUTNAME FILE...

The $PROG program generates shared tags for different languages

Valid options are:
  -h, --help    Display help screen
  -l, --lang    Set source file language
  -k, --kinds   Set language-specific kinds of tags (see ctags(1))
  -f, --fields  Set extension fields to include in tags file (see ctags(1))
  -B, --base    Set base directory for tags file
  --list-langs  List supported languages

Tags will be stored in \$BASE_DIRECTORY/\$LANG/\$OUTNAME.tags

FILE is a list of files and/or directories to search tags in.  It is
allowed to specify any ctags options, for example --exclude=PATTERN

If \$RPM_BUILD_ROOT is set, it will be prepended to base directory.
It is also will be stripped from paths in generated tags file (needs
patched version of ctags that understands --buildroot option).

Report bugs to <raorn@altlinux.ru>
EOF
    [ -n "$1" ] && exit "$1" || exit
}

[ $# -gt 0 ] || Usage 0

TEMP=`getopt -n $PROG -o h,l:,k:,f:,B: -l help,lang:,kinds:,fields:,base:,list-langs -- "$@"` || Usage
eval set -- "$TEMP"

while :; do
    case "$1" in
	-h|--help) Usage 0
	;;
	-l|--lang) shift; lang="$1"
	;;
	-k|--kinds) shift; kinds="$1"
	;;
	-f|--fields) shift; fields="$1"
	;;
	-B|--base) shift; tags_base="$1"
	;;
	--list-langs) shift;
	echo "Available languages:"
	for i in $etc/lang/*.conf; do
	    l=${i#$etc/lang/}
	    l=${l%.conf}
	    echo "  $l"
	done
	exit 0
	;;
	--) shift; break
	;;
	*) Usage 1
	;;
    esac
    shift
done

[ -f "$etc/lang/$lang.conf" ] && . "$etc/lang/$lang.conf" || {
	echo "Dunno about $lang..."
	exit 1
}

outname="$1"; shift

[ -d "$RPM_BUILD_ROOT$tags_base/$lang" ] || mkdir -p "$RPM_BUILD_ROOT$tags_base/$lang"

ctags -f "$RPM_BUILD_ROOT$tags_base/$lang/$outname.tags" -n -R \
	--languages="$ctags_lang" \
	"--$ctags_lang-kinds=$kinds" \
	${fields+--fields="$fields"} \
	--file-scope=no \
	--tag-relative=no \
	${RPM_BUILD_ROOT+--buildroot="$RPM_BUILD_ROOT"} \
	${extra_args+$extra_args} \
	"$@"

