#!/bin/sh
# Katalog by Maciej Korze <maciekk@linux.sky.pl>
# KATalogLiSt - list names of databases, or content(s) of base(s).
# $Id: katls,v 1.5 2002/06/28 19:18:58 eaquer Exp $
#
#    Copyright (C) 2001,2002  Maciej Korze
#
#    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, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# Default settings
data="/usr/share/katalog"
BOLD="\033[1;37m"
NORMAL="\033[0m"
verbose="off"
debug="off"
reverse="off"
compressed="on"

verbose() {
        if [ "$verbose" = "on" ] ; then
                echo $@
        fi
}

version() {
	echo "Katalog 1.8b"
}

# Compressed file CAT
ccat() {
        type="`file $1 | cut -d ' ' -f 2`"
        case $type
        in
                gzip)
                        cat $1 | gunzip
                        ;;
                bzip2)
                        cat $1 | bunzip2
                        ;;
        esac
}

check_arg() {
        if [ "$2" != "on" -a "$2" != "off" ] ; then
                echo "$1 - bad value: $2."
                exit 1
        fi
}

is_empty() {
        if [ -z "$2" ] ; then
                echo "Argument for $1 needed, see 'katadd -h'."
                exit 1
        fi
}

usage() {
cat << EOF
Usage: katls [-k dir] [-h] [-d] [-v] [-V] [-r] [-o|-O] [ first_base second_base ... | all ]
	-k dir		'dir' is a dircetory with bases,
        -h              this help,
        -d              turn debug mode on,
        -v              verbose mode,
        -V              display version number,
        -r              display bases names as
                                'file_name (Description of base)',
        -o              show also contents of compressed files,
        -O              don't show contents of compressed files.

	If you don't specify any base name, then instead of content of base(s),
	bases list will be shown. If you want to display contents of all bases
	use 'all' as parameter.
EOF
}

find_filter() {
	egrep -v '(^ *$|^./$|^.$)' | sed 's/^.\///' | sort
}

# Load settings
for i in /etc/katalogrc /usr/local/etc/katalogrc .katalogrc katalogrc $HOME/.katalogrc ; do
        if [ -e "$i" ] ; then
		verbose "Sourcing file '$i'."
                . $i
        fi
done

check_arg verbose "$verbose"
check_arg debug "$debug"
check_arg reverse "$reverse"
check_arg compressed "$compressed"

while [ "$#" -gt 0 ] ; do
        case $1 in
                -k)
                        is_empty "-k" "$2"
                        data="$2"
                        shift 2
                        ;;
                -h)
                        usage
                        exit 0
                        ;;
                -d)
                        debug=on
                        shift
			;;
                -v)
                        verbose=on
                        shift
			;;
                -V)
                        version
                        exit 0
			;;
                -r)
                        reverse=on
                        shift
			;;
                -o)
                        compressed=on
                        shift
                        ;;
                -O)
                        compressed=off
                        shift
                        ;;
                -*)
			usage
			exit 1
			;;
                *)
			bases="$bases $1"
			shift
			;;
         esac
done

if [ "$debug" = "on" ] ; then
        verbose "Turning debug on."
        set -x
fi

if [ "$compressed" = "on" ] ; then
        verbose "Displaying compressed files contents enabled."
fi

if [ ! -d "$data" ]
then
    echo "Data dir '$data' doesn't exist!"
    exit 1
fi

verbose "Data dir is set to '$data'."

cd "$data"

if [ -z "`find ./ | find_filter`" ]
then
    echo "You don't have any databases in '$data'."
    exit 1
fi
		
for i in $bases ; do
	if [ "$i" = "all" ] ; then
		show_all=yes
		verbose "Showing all databases contents."
	fi
done

if [ "x$bases" = "x" ]
then
    for base in `find ./ ! -name '*.dsc' | find_filter`
    do
	desc="`cat $data/$base.dsc`"
	if [ "$reverse" = "on" ] ; then
		echo -e "${BOLD}${base}${NORMAL} ($desc)"
	else
		echo -e "${BOLD}${desc}${NORMAL} ($base)"
	fi
    done
    exit 0
fi

if [ "$show_all" = "yes" ]
then
    for base in `find ./ ! -name '*.dsc' | find_filter`
    do
	desc="`cat $data/$base.dsc`"
	if [ "$reverse" = "on" ] ; then
		echo -e "${BOLD}${base}${NORMAL} ($desc):"
	else
		echo -e "${BOLD}${desc}${NORMAL} ($base):"
	fi
	if [ "$compressed" = "on" ] ; then
		ccat $base | sed 's/^/  /'
	else
		ccat $base | egrep -v '#' | sed 's/^/  /'
	fi
    done
    exit 0
fi

for base in $bases
do
	if [ -e "$base" ]
	then
		desc="`cat $data/$base.dsc`"
		if [ "$reverse" = "on" ] ; then
			echo -e "${BOLD}${base}${NORMAL} ($desc):"
		else
			echo -e "${BOLD}${desc}${NORMAL} ($base):"
		fi
		if [ "$compressed" = "on" ] ; then
			ccat $base | sed 's/^/  /'
		else
			ccat $base | egrep -v '#' | sed 's/^/  /'
		fi
	else
		echo "Base $base doesn't exist!"
	fi
done
exit 0
