#!/bin/bash

# localize_slack.sh
# 2022-08-14
# by Gernot WALZL

# Localizes Slackware to the given $NEWLANG

LANG=${NEWLANG:-de_AT.utf8}
LANGSHORT=${LANG:0:2}
SLACKDIR=${SLACKDIR:-/mnt/cdrom}

set -e

SLACKVER=$(awk '{print $2}' /etc/slackware-version)

if [ $(locale -a | grep -e '^'$LANG'$' | wc -l) -ne 1 ]; then
  echo "ERROR: LANG=$LANG is not valid."
  echo "Run 'locale -a' to see a list of available locales."
  exit 1
fi

EXTRADIR=$SLACKDIR/extra
if [ ! -d "$EXTRADIR" ]; then
  echo "ERROR: EXTRADIR=$EXTRADIR is not valid."
  exit 1
fi
upgradepkg --install-new $EXTRADIR/aspell-word-lists/*-$LANGSHORT-*.t?z

if [ "$SLACKVER" = "14.0" -o "$SLACKVER" = "14.1" -o "$SLACKVER" = "14.2" ]; then
  KDEIDIR=$SLACKDIR/slackware/kdei
  if [ "$(uname -m)" = "x86_64" ]; then
    KDEIDIR=$SLACKDIR/slackware64/kdei
  fi
  if [ ! -d "$KDEIDIR" ]; then
    echo "ERROR: KDEIDIR=$KDEIDIR is not valid."
    exit 1
  fi
  upgradepkg --install-new $KDEIDIR/*-$LANGSHORT-*.t?z
fi

cp /etc/profile.d/lang.sh /etc/profile.d/lang.sh.orig
sed '1,/^export LANG=/ s/LANG=.*/LANG='$LANG'/' /etc/profile.d/lang.sh.orig \
  > /etc/profile.d/lang.sh

cat /etc/X11/xorg.conf-vesa \
  | sed '/"kbd"/ a\
\
    Option "XkbLayout" "'$LANGSHORT'"\
    Option "XkbVariant" "nodeadkeys"\
' \
  > /etc/X11/xorg.conf.new
if [ -f /etc/X11/xorg.conf ]; then
  echo "WARNING: /etc/X11/xorg.conf already exists."
else
  mv /etc/X11/xorg.conf.new /etc/X11/xorg.conf
fi

if [ -f /usr/share/X11/xorg.conf.d/90-keyboard-layout.conf ]; then
  cat /usr/share/X11/xorg.conf.d/90-keyboard-layout.conf \
    | sed '1,/Option "XkbLayout"/ s/"us"/"'$LANGSHORT'"/' \
    | sed '1,/#Option "KkbVariant"/ s/#Option "XkbVariant" ""/Option "XkbVariant" "nodeadkeys"/' \
    > /etc/X11/xorg.conf.d/90-keyboard-layout.conf.new
  if [ -f /etc/X11/xorg.conf.d/90-keyboard-layout.conf ]; then
    echo "WARNING: /etc/X11/xorg.conf.d/90-keyboard-layout.conf already exists."
  else
    mv /etc/X11/xorg.conf.d/90-keyboard-layout.conf.new /etc/X11/xorg.conf.d/90-keyboard-layout.conf
  fi
fi

if [ -f /usr/share/X11/xorg.conf.d/90-keyboard-layout-evdev.conf ]; then
  cat /usr/share/X11/xorg.conf.d/90-keyboard-layout-evdev.conf \
    | sed '1,/Option "XkbLayout"/ s/"us"/"'$LANGSHORT'"/' \
    | sed '1,/#Option "KkbVariant"/ s/#Option "XkbVariant" ""/Option "XkbVariant" "nodeadkeys"/' \
    > /etc/X11/xorg.conf.d/90-keyboard-layout-evdev.conf.new
  if [ -f /etc/X11/xorg.conf.d/90-keyboard-layout-evdev.conf ]; then
    echo "WARNING: /etc/X11/xorg.conf.d/90-keyboard-layout-evdev.conf already exists."
  else
    mv /etc/X11/xorg.conf.d/90-keyboard-layout-evdev.conf.new /etc/X11/xorg.conf.d/90-keyboard-layout-evdev.conf
  fi
fi

if [ -f /usr/share/hal/fdi/policy/10osvendor/10-keymap.fdi ]; then
  cat /usr/share/hal/fdi/policy/10osvendor/10-keymap.fdi \
    | sed '1,/input.xkb.layout/ s/>us</>'$LANGSHORT'</' \
    | sed '1,/input.xkb.variant/ s/ \/>/>nodeadkeys<\/merge>/' \
    > /etc/hal/fdi/policy/10-keymap.fdi.new
  if [ -f /etc/hal/fdi/policy/10-keymap.fdi ]; then
    echo "WARNING: /etc/hal/fdi/policy/10-keymap.fdi already exists."
  else
    mv /etc/hal/fdi/policy/10-keymap.fdi.new /etc/hal/fdi/policy/10-keymap.fdi
  fi
fi