#! /bin/sh -e

# if [ -r /usr/share/debconf/confmodule ]
# then
#     . /usr/share/debconf/confmodule
#     HAS_DEBCONF=1
# fi


CONFDIR=/etc/console-tools
OLDCONFDIR=/etc/kbd

# Rename old local keymaps dir if any
if [ -d /usr/local/share/keytables ]
then
    if [ -d "`readlink -f /usr/local/share/keymaps`" ]
    then
	files=`find /usr/local/share/keytables -mindepth 1 -maxdepth 1`
        [ "$files" ] && mv $files /usr/local/share/keymaps/
        rmdir /usr/local/share/keytables
    elif [ -e /usr/local/share/keymaps ]
    then
        echo >&2 "/usr/local/share/keymaps is buggy - please check it"
        ls -ld /usr/local/share/keymaps >&2
        echo >&2 "Press RETURN to continue"
        read foo
    else
        mv /usr/local/share/keytables /usr/local/share/keymaps
    fi
elif [ -e /usr/local/share/keytables ]
then
    echo >&2 <<EOF
/usr/local/share/keytables is not a dir.  I won't try to mess
with it and you will surely want to do something with it, as the
new location for local keymaps is now /usr/local/share/keymaps/.

Press RETURN to continue
EOF
    read foo
fi
    
# Import local files from obsolete /usr/lib/kbd directory
if [ -d /usr/lib/kbd ]
then
#      if [ "$HAS_DEBCONF" ]
#      then
#  	db_input low console-tools/usrlib_import || true
#  	db_go || true
#      else
	echo >&2 Importing local files from obsolete /usr/lib/kbd directory into /usr/local/:
#      fi
    set +e	# there may be no files in those directories !
    for d in keytables consolefonts consoletrans videomodes; do
	# if the dir exists and is not a link (into /usr/share)
	if [ -d /usr/lib/kbd/${d} -a ! -L /usr/lib/kbd/${d} ]
	then
	    # maybe create local dir
	    test -d /usr/local/share/${d} || mkdir -p /usr/local/share/${d}
	    files=`find /usr/lib/kbd/${d} -mindepth 1 -maxdepth 1`
	    [ "$files" ] && mv $files /usr/local/share/${d}/
	    rmdir /usr/lib/kbd/${d}
	fi
	# remove an eventual symlink
	rm -f /usr/lib/kbd/${d}
    done
    set -e
    [ -z "$HAS_DEBCONF" ] && echo " done importing."
    # try to remove the old dir, but be nice when not empty
    rmdir /usr/lib/kbd 2>/dev/null || echo >&2 <<EOF 
WARNING: /usr/lib/kbd/ not removed - other files there ?
Check this and remove it by hand.
EOF
fi

# rename any /etc/kbd/default.map file to .kmap
for GZ in "" ".gz"
do
    if [ -f /etc/kbd/default.map$GZ ]
    then
	if [ -f /etc/kbd/default.kmap -o -f /etc/kbd/default.kmap.gz ]
	then
	    rm -f /etc/kbd/default.map$GZ
	else
	    mv /etc/kbd/default.map$GZ /etc/kbd/default.kmap$GZ
	fi
    fi
done

# Convert from kbd
if [ ! -f ${CONFDIR}/default.kmap.gz -a ! -f ${CONFDIR}/default.kmap ]
then
    if [ -f ${OLDCONFDIR}/default.kmap.gz ] ; then
	OLDMAP=${OLDCONFDIR}/default.kmap.gz
	NEWMAP=${CONFDIR}/default.kmap.gz
    elif [ -f ${OLDCONFDIR}/default.kmap ] ; then
	OLDMAP=${OLDCONFDIR}/default.kmap
	NEWMAP=${CONFDIR}/default.kmap
    elif [ -f ${OLDCONFDIR}/default.map.gz ] ; then
	OLDMAP=${OLDCONFDIR}/default.map.gz
	NEWMAP=${CONFDIR}/default.kmap.gz
    elif [ -f ${OLDCONFDIR}/default.map ] ; then
	OLDMAP=${OLDCONFDIR}/default.map
	NEWMAP=${CONFDIR}/default.kmap
    fi

    if [ x${OLDMAP} != x ] ; then
	echo >&2 Moving default keymap ${OLDMAP} to ${NEWMAP}
	mkdir -p ${CONFDIR}
	mv ${OLDMAP} ${NEWMAP}
    fi
fi



exit 0
