#!/bin/sh -e

PATH=/bin:/usr/bin
PROG="${0##*/}"

usage()
{
    [ "$1" = 0 ] || exec >&2
    cat <<EOF
colorize - local wrapper setup tool to colorize some program's output

Usage: $PROG [options] <program-name>

Valid options are:
    -c,--configdir            path to the directory with configs
    -h, --help                show this text and exit.
    -v, --version             print program version and exit;

Report bugs to inger@altlinux.org
EOF
    [ -n "$1" ] && exit "$1" || exit
}


#parse command line
TEMP=`getopt -n $PROG -o c,help,version: -l configdir:,help,version -- "$@"` || exit 1
eval set -- "$TEMP"

options=
while :; do
	case "$1" in
		-c|--configdir)
			shift
			configdir="$1"
			options="COLORIFER_CONFIGDIR=$1"
			shift
		    ;;
		-v|--version)
			cat <<EOF
$PROG version 1.0.1
Copyright (C) 2004  ALT Linux Team
		
Written by Stanislav Ievlev <inger@altlinux.org>
EOF
			exit 0
			;;
		-h|--help) usage 0
			;;
		--) shift; break
			;;
		*) "unrecognized option: $1"
		    exit 1
			;;
	esac
done

[ "$#" -ge 1 ] || usage


for prg in "$@"
do
full_path="$(type -p "$prg")" || continue
mkdir -p ${HOME?}/bin
cat >${HOME?}/bin/$prg<<EOF
#!/bin/sh
COLORIFER_NAME="\$0" COLORIFER_REDIRECT=$full_path $options colorifer "\$@"
exit \$?
EOF
chmod 755 ${HOME?}/bin/$prg
done
