#!/bin/sh
#
# Copyright (c) 2004  Peter Pentchev.  All rights reserved.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# $Ringlet: sh/misc/cicquin/cicquin.sh,v 1.1 2004/06/04 07:50:17 roam Exp $

# A couple of helper functions

usage() {
	echo 'Usage: cicquin [-hpV] nickname' 1>&2
	echo 'cicquin -h for help' 1>&2
}

help() {
	echo 'Usage: cicquin [-hpV] nickname'
	echo '       -h      display this help message'
	echo '       -p      display the full path to the info directory'
	echo '       -V      display the cicquin version'
}

version() {
	echo 'cicquin version 0.1'
}

# Initialize and process command-line options

fullpath=0

args=`getopt hpV $*`
if [ $? != 0 ]; then
	usage
	exit 1
fi
set -- $args
for i
do
	case "$i" in
		-h)
			help
			exit 0
			;;
		-p)
			fullpath=1
			shift
			;;
		-V)
			version
			exit 0
			;;
		--)
			shift
			break
			;;
		*)
			usage
			exit 1
			;;
	esac
done

# Sanity check
if [ $# = 0 ]; then
	usage
	exit 1
fi

# Actually do the work :)
find "$HOME/.centericq" -mindepth 2 -maxdepth 2 -type f -name info | \
	xargs awk -v fullpath="$fullpath" -v nickname="$1" '\
FNR == 2	{ nextfile } \
FNR == 1 && tolower($0) == tolower(nickname) { \
	s=FILENAME; \
	sub("/[^/]*$", "", s); \
	if (fullpath == "0") { sub("^.*/", "", s) };
	print s \
}'
