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

echo "### Setting up fonts ###"

. /etc/lang.conf
. /etc/lang.funcs

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

# set menu and help files according to language setting
MENU_LANG=`sed -n "s/.*lang=\([^ ]*\).*/\1/p" /proc/cmdline`
[ -z $MENU_LANG ] && MENU_LANG=en

. /etc/mplayer/en.lang # default language file, always present
[ -f /etc/mplayer/${MENU_LANG}.lang ] && . /etc/mplayer/${MENU_LANG}.lang
TAGS=`grep "#.*#" /etc/mplayer/menu.conf | sed 's%.*#\(.*\)#.*%\1%'`
for i in $TAGS; do
  v=`eval echo \\\$$i`
  sed -i "s%#${i}#%$v%" /etc/mplayer/menu.conf;
done

cp -f /usr/share/mplayer/help_en.txt /etc/mplayer/help.txt
[ -f /usr/share/mplayer/help_${MENU_LANG}.txt ] && cp -f /usr/share/mplayer/help_${MENU_LANG}.txt /etc/mplayer/help.txt

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="iso-8859-1"

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

MENU_FONT="/usr/share/fonts/$MENU_FONT"
[ ! -f "$MENU_FONT" ] && exit 1

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"
  [ ! -f "$SUB_FONT" ] && exit 1

  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"
  [ -f /usr/bin/Xorg -a -f /var/use_xorg ] && mp_set_option subfont-osd-scale 2
fi

exit 0
