#!/bin/sh

# by Aleksey Cheusov <vle@gmx.net>

sysconfdir=@sysconfdir@

#####################################
if test -z "$DICTL_SERVER_CHARSET"; then
    DICTL_SERVER_CHARSET=utf-8
fi

if test -z "$DICTL_CHARSET"; then
    if which locale >/dev/null 2>&1; then
	DICTL_CHARSET=`locale -k LC_CTYPE | sed -n 's/charmap="\(.*\)"/\1/p'`;
    else
	DICTL_CHARSET=C
    fi
fi

if test -z "$DICTL_PAGER"; then
    if test -r "$HOME/.dictrc"; then
	DICTL_PAGER=`awk '$1 == "pager" {print $2}' < "$HOME/.dictrc"`
    fi

    if test -z "$DICTL_PAGER" && test -r "$sysconfdir/dict.conf"; then
	DICTL_PAGER=`awk '$1 == "pager" {print $2}' < $sysconfdir/dict.conf`
    fi

    if test -z "$DICTL_PAGER"; then
	DICTL_PAGER=$PAGER
    fi

    if test -z "$DICTL_PAGER"; then
	DICTL_PAGER=less
    fi
fi

charset2charset (){
    if test "$DICTL_USE_ICONV"; then
	iconv -f $1 -t $2
    else if test "$DICTL_USE_KONWERT"; then
	konwert $1-$2
    else
	recode -f $1..$2
    fi fi
}

#####################################

if
    test "_$DICTL_CHARSET" = "_C" ||
    test "_$DICTL_CHARSET" = "_POSIX"
then
    echo "iconv/recode/konwert do not support coversions to/from locale \"$DICTL_CHARSET\""
    exit 1
fi

if
    test "_$DICTL_SERVER_CHARSET" = "_C" ||
    test "_$DICTL_SERVER_CHARSET" = "_POSIX"
then
    echo "iconv/recode/konwert do not support coversions to/from locale \"$DICTL_SERVER_CHARSET\""
    exit 1
fi

if test "$DICTL_USE_ICONV"; then
    if ! which iconv >/dev/null 2>&1; then
	echo "'iconv' is not availbale. See dictl(1) for a help." 1>&2
	exit 2
    fi
elif test "$DICTL_USE_KONWERT"; then
    if ! which konwert >/dev/null 2>&1; then
	echo "'konwert' is not availbale. See dictl(1) for a help." 1>&2
	exit 2
    fi
else
    if ! which recode >/dev/null 2>&1; then
	echo "'recode' is not availbale. See dictl(1) for a help." 1>&2
	exit 2
    fi
fi

#####################################

params="dict"

while test $# -ne 0; do
    case $1 in
	--run-a-pipe)
	    charset2charset $DICTL_SERVER_CHARSET $DICTL_CHARSET
	    exit $?;;
	-P|--pipe)
	    DICTL_PAGER=$2
	    shift;;
	*)
	    p=`echo "$1" | charset2charset $DICTL_CHARSET $DICTL_SERVER_CHARSET`
	    params="$params '$p'"

	    # ...to be comatible with dict
	    if echo $1 |
		awk '{
		    exit ($0 !~ /^(-L|--license|-V|--version|--help)$/)
		}'
	    then
		DICTL_PAGER=
		break
	    fi;;
    esac

    shift
done

if test "_$DICTL_PAGER" = '_-'; then
    DICTL_PAGER=
fi

if test -z "$DICTL_PAGER"; then
    eval $params -P - | $0 --run-a-pipe
else
    eval $params -P - | $0 --run-a-pipe | $DICTL_PAGER
fi
