#!/bin/sh

# indexhtml-update creates index.html as symlink to index-$lang.html in $INDEXHTMLDIR directory
# at least index-en.html is required
# $lang is taken from: /etc/sysconfig/i18n, $LANG, en

# default indexhtmldir
DEFAULTINDEXHTMLDIR=/usr/share/doc/indexhtml
INDEXHTMLDIR=

fatal() {
  echo "indexhtml-update: $*" >&2
  exit 1
}

if [ -n "$1" ]; then
  if [ -d "$1" -a -w "$1" ]; then
    INDEXHTMLDIR="$1"
  else
    fatal "$1 not writeable"
  fi
elif [ -d "$DEFAULTINDEXHTMLDIR" -a -w "$DEFAULTINDEXHTMLDIR" ]; then
  INDEXHTMLDIR="$DEFAULTINDEXHTMLDIR"
else
  fatal "$DEFAULTINDEXHTMLDIR (default) not writeable"
fi

if [ -z "$INDEXHTMLDIR" ]; then
  fatal "no indexhtml directory available"
fi

# avoid systemd automatic dependency
LOCALECONF=/etc/locale.conf
if [ -e "$LOCALECONF" ]; then
  [ -r "$LOCALECONF" ] && . "$LOCALECONF"
else
  [ -r /etc/sysconfig/i18n ] && . /etc/sysconfig/i18n
fi

lang=`echo "$LANG" |cut -b-2`
cd "$INDEXHTMLDIR"

if [ -f "index-$lang.html" ]; then
  ln -sf "index-$lang.html" index.html
elif [ -f index-en.html ]; then
  ln -sf index-en.html index.html
else
  fatal "At least index-en.html should be present in $INDEXHTMLDIR"
fi

