#!/bin/bash
#
# Runs ctags recursively, excluding version control directories and backup files
#

CTAGS=${CTAGS:-$(command -v ctags)}

[[ $? != 0 ]] && { echo "No ctags binary found"; exit -1; }

if [[ $# -lt 1 ]] && git rev-parse --git-dir >&/dev/null; then
    git ls-files -z | xargs -0 "$CTAGS"
    exit $?
fi

dir=${1:-.}

find "$dir"  -path "*/.idea" -prune -o \
             -path "*/.backup" -prune -o \
             -path "*/.bak" -prune -o \
             -path "*/.git" -prune -o \
             -path "*/.svn" -prune -o \
             -path "*/CVS" -prune -o \
             -path "*/RCS" -prune -o \
             -path "*/_darcs" -prune -o \
             -path "*/__pycache__" -prune -o \
             -type f \
             -not -name \*.bak \
             -not -name \*.pyc \
             -not -name \*.pyo \
             -not -name \*~ \
             -print0 | xargs -0 "$CTAGS"

