#!/bin/bash
#
# Copyright (c) 1998 S.u.S.E. Gmbh Fuerth, Germany.
#               2002 SuSE Linux AG, Nuernberg, Germany.
#
# please send bugfixes or comments to <feedback@suse.de>
#
# Authors: Werner Fink, <werner@suse.de>
#          Mike Fabian <mfabian@suse.de>

# only one of UID and USER must be set correctly
if test "$UID" != 0 -a "$USER" != root; then
    echo "You must be root to start $0."
    exit 1
fi

test -f $ROOT/etc/sysconfig/suseconfig || {
    echo "No /etc/sysconfig/suseconfig found." && exit 1 ;
}
. $ROOT/etc/sysconfig/suseconfig

test -f /lib/YaST/SuSEconfig.functions || {
        echo "ERROR - can not find /lib/YaST/SuSEconfig.functions!!"
        echo "This should not happen.  Exit..."
        exit 1
}
. /lib/YaST/SuSEconfig.functions

test -n "$UPDATE_GROFF_CONF" -a "$UPDATE_GROFF_CONF" = "yes" || exit 0

function get_paper () {
    (
	. /etc/sysconfig/language &> /dev/null
	read h w r < <(LANG=$RC_LANG locale -k LC_PAPER)
	case "$h" in
	    height=297) echo a4	    ;;
	    *)		echo letter ;;
	esac
    )
}

# first get the paper size from the locale information
PAPER_SIZE=$(get_paper)

# if GROFF_PAGESIZE is set, it overrides the locale information:
if [ -n "$GROFF_PAGESIZE" ] ; then
    # make GROFF_PAGESIZE lower case only:
    GROFF_PAGESIZE=$( echo $GROFF_PAGESIZE | tr '[:upper:]' '[:lower:]' )
    case $GROFF_PAGESIZE in
	a4|letter)
	    PAPER_SIZE=$GROFF_PAGESIZE
	;;
	*)
            echo "illegal value of GROFF_PAGESIZE, using 'a4' instead."
            PAPER_SIZE=a4
        ;;
    esac
fi

echo "$PAPER_SIZE" > /etc/papersize

exit 0
