#!/bin/sh
#
# put the right font in the right place
#
# runlevels: geexbox, debug, configure

echo "### Setting up fonts ###"
. /settings
. /etc/lang.conf
. /etc/lang.funcs

readfirstword() {
  head -n 1 $1 | sed 's/^\([[:graph:]]*\).*$/\1/'
}

[ -z $MENU_LANG ] && MENU_LANG=en

MENU_CHARSET=`lang2charset $MENU_LANG`
[ -z "$MENU_CHARSET" ] && exit 1

MENU_FONT=`lang2font "$MENU_LANG" menu`
if [ -f /etc/theme.conf -a -f "/usr/share/fonts/themefont.ttf" ]; then
  . /etc/theme.conf
  [ -z "$FONT_CHARSETS" ] && FONT_CHARSETS="windows-1252"

  for i in $FONT_CHARSETS; do
    [ "$i" = "$MENU_CHARSET" ] && MENU_FONT="themefont.ttf" && break
  done
fi

MENU_FONT="/usr/share/fonts/$MENU_FONT"
mp_set_option font "$MENU_FONT"

# set subtitles font according to subfont setting
if test -f /etc/subfont; then
  SUB_CHARSET=`readfirstword /etc/subfont`

  SUB_CHARSET=`lang2charset "$SUB_CHARSET"`
  [ -z "$SUB_CHARSET" ] && exit 1

  SUB_FONT=`lang2font "$SUB_CHARSET" sub`
  SUB_FONT="/usr/share/fonts/$SUB_FONT"

  mp_set_option subfont "$SUB_FONT"
  mp_set_option subcp "$SUB_CHARSET"
  fribidi_mp_set_option "$SUB_CHARSET"
fi

# set font settings according to theme
if test -f /etc/theme.conf; then
  . /etc/theme.conf

  mp_set_option subfont-osd-scale "$FONT_SIZE"
  mp_set_option subfont-text-scale "$FONT_SIZE"
#  [ -f /usr/bin/Xorg -a -f /var/use_xorg ] && mp_set_option subfont-osd-scale 2
fi

# set DVD default language
mp_set_option alang "$MENU_LANG"
mp_set_option slang "$MENU_LANG"

exit 0
