#!/bin/sh
# Katalog by Maciej Korze <maciekk@linux.sky.pl>
# KATalogADDbase - add base.
# $Id: katadd,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"
cd="/cdrom"
check_cd="on"
verbose="off"
debug="off"
reverse="off"
automount="off"
compress_prog="gzip"
compressed="on"
override="off"
eject="off"

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

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
}

unmount() {
	if [ "$unmount" = "on" ] ; then
		verbose "Unmountig CD-ROM."
		umount $cd || exit 1
		if [ "$eject" = "on" ] ; then
			verbose "Ejecting CD-ROM."
			if which eject >/dev/null 2>&1 ; then
				eject "$cd"
			else
				verbose "I can't find eject program, not ejecting CD-ROM."
			fi
		fi
	fi
}

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

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: katadd [-k dir] [-a|-A] [-b|-g] [-h] [-d] [-v] [-V] [-r] [-c|-C] [-o|-O] [-i|-I] [-e|-E] "name of base" "base description"
	-k dir          'dir' is a dircetory with bases,
	-a		mount CD-ROM if unmounted (and unmount after
			creating base),
	-A		don't mount CD-ROM,
	-b		use bzip2 to compress bases,
	-g		use gzip to compress 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)',
	-c		stop if CD-ROM is unmounted,
	-C		don't stop even if it seems, that CD-ROM is unmounted,
	-o		save contents of compressed files,
	-O		don't save contents of compressed files,
	-i		override existing bases,
	-I		don't override existing bases,
	-m dir		directory where CD-ROM is mounted,
	-e		eject CD-ROM after doing unmount,
	-E		don't eject CD-ROM,
	"name of base"	name of file in $data - content of base,
	"base description"	long description of base.
EOF
}

check_arg automount "$automount"
check_arg check_cd "$check_cd"
check_arg verbose "$verbose"
check_arg debug "$debug"
check_arg reverse "$reverse"
check_arg compressed "$compressed"
check_arg override "$override"
check_arg eject "$eject"
 
while [ "$#" -gt 0 ] ; do
	case $1 in
		-k)
			is_empty "-k" "$2"
			data="$2"
			shift 2
			;;
		-a)
			automount=on
			shift
			;;
		-A)
			automount=off
			shift
			;;
		-b)
			compress_prog=bzip2
			shift
			;;
		-g)
			compress_prog=gzip
			shift
			;;
		-h)
			usage
			exit 0
			;;
		-d)
			debug=on
			shift
			;;
		-v)
			verbose=on
			shift
			;;
		-V)
			version
			exit 0
			;;
		-r)
			reverse=on
			shift
			;;
		-c)
			check_cd=on
			shift
			;;
		-C)
			check_cd=off;
			shift
			;;
		-o)
			compressed=on
			shift
			;;
		-O)
			compressed=off
			shift
			;;
		-i)
			override=on
			shift
			;;
		-I)
			override=off
			shift
			;;
		-m)
                        is_empty "-m" "$2"
                        cd="$2"
                        shift 2
                        ;;
		-e)
			eject=on
			shift
			;;
		-E)
			eject=off
			shift
			;;
		*)
			if [ "x$name" = "x" ] ; then
				name="$1"
				shift
				continue
			fi
			if [ "x$desc" = "x" ] ; then
				desc="$1"
				shift
				continue
			fi
			usage
			exit 0
			;;
	esac
done

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

if [ "$reverse" = "on" ] ; then
	verbose "Reverse mode is on."
fi

if [ "$compress_prog" != "gzip" ] && [ "$compress_prog" != "bzip2" ] ; then
	echo "Bad compress program -- $compress_prog."
	exit 1
fi

if [ -z "$name" -o -z "$desc" ]
then
	[ -z "$name" ] && verbose "Base name not given."
	[ -z "$desc" ] && verbose "Base description not given."
	usage
	exit 1
fi

verbose "Base name is '$name'."
verbose "Base description is '$desc'."

if [ ! -e "$cd" ]
then
	echo "$cd doesn't exist!"
	exit 1
fi

verbose "CD-ROM dir is '$cd'."

if [ -e "$data/$name" ]
then
	if [ "$override" = "off" ] ; then
		echo "There already is base '$name'."
		unmount
		exit 1
	fi
	verbose "There already is base '$name'."
fi

if [ "`find $cd | wc -l`" -gt "1" ] && mount | grep -q cdrom ; then
	cdstatus=mounted
else
	cdstatus=unmounted
fi

if [ ! -d "$data" -a ! -L "$data" ]
then
	if [ -e "$data" ]
	then
		echo "$data must be a dir!"
		exit 1
	else
		verbose "Making data dir '$data'."
		mkdir $data
	fi
fi

verbose "CD-ROM is $cdstatus."

if [ "$cdstatus" = "unmounted" ] ; then
	if [ "$automount" = "on" ]; then
		verbose "Mounting CD-ROM."
		if mount $cd ; then
			check_cd=off
			unmount=on
		else
			exit 1
		fi
	fi

	if [ "$check_cd" = "on" ]; then
		echo "CD is not mounted. Use 'katadd -C' or 'katadd -a' (see help first)."
		exit 1
	fi
fi

echo "$desc" > $data/$name.dsc
verbose "Creating base."

cd "$cd"
find ./ | find_filter > "$data/$name"

if [ "$compressed" = "on" ] ; then
	verbose "Saving content of compressed files."
	# tar.gz files
	if which tar >/dev/null 2>&1 && which gzip >/dev/null 2>&1 ; then
		files="`cat \"$data/$name\" | egrep -v '#' | egrep -i '\.(tar\.gz|tgz)$'`"
		for i in $files ; do
			cat "$cd/$i" | gunzip | tar -tf - | sed "s@^@$i#@" >> "$data/$name"
		done
	fi
	# tar.bz2 files
	if which tar >/dev/null 2>&1 && which bzip2 >/dev/null 2>&1 ; then
		files="`cat \"$data/$name\" | egrep -v '#' | egrep -i '\.tar\.bz2$'`"
		for i in $files ; do
			cat "$cd/$i" | bunzip2 | tar -tf - | sed "s@^@$i#@" >> "$data/$name"
		done
	fi
	# rpm files
	if which rpm >/dev/null 2>&1 ; then
		files="`cat \"$data/$name\" | egrep -v '#' | egrep -i '\.rpm$'`"
		for i in $files ; do
			rpm -ql -p "$cd/$i" | sed "s@^/@$i#@" >> "$data/$name"
		done
	fi
	# zip files
	if which zipinfo >/dev/null 2>&1 ; then
		files="`cat \"$data/$name\" | egrep -v '#' | egrep -i '\.zip$'`"
		for i in $files ; do
			zipinfo -l1 "$cd/$i" | sed "s@^@$i#@" >> "$data/$name"
		done
	fi
fi

verbose "Compressing base content with $compress_prog."
cat "$data/$name" | sort | $compress_prog > "$data/$name.tmp"
mv "$data/$name.tmp" "$data/$name"

if [ "$?" = "0" ]
then
	if [ "$reverse" = "on" ] ; then
		verbose -e "Base for ${BOLD}$name${NORMAL} ($desc) created."
	else
		verbose -e "Base for ${BOLD}$desc${NORMAL} ($name) created."
	fi
	cd - ; unmount
else
	if [ "$reverse" = "on" ] ; then
		echo -e "There was error while creating base for ${BOLD}${name}${NORMAL} ($desc)."
	else
		echo -e "There was error while creating base for ${BOLD}${desc}${NORMAL} ($name)."
	fi
	if [ -f "$katalog/$name" ]; then
		verbose "Deleting '$katalog/$name'."
		rm "$katalog/$name"
	fi
	if [ -f "$katalog/$name.dsc" ]; then
		verbose "Deleting '$katalog/$name.dsc'."
		rm "$katalog/$name.dsc"
	fi
	cd - ; unmount
	exit 1
fi
