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

echo "### Setting up fonts ###"
. /settings
[ -z "$GEEXBOX" ] && GEEXBOX=/mnt/frontsd/GEEXBOX/
. /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 "$GEEXBOX/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 "$GEEXBOX$MENU_FONT" ] && exit 1 || cp -f "$GEEXBOX$MENU_FONT" "$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"
  [ ! -f "$GEEXBOX$SUB_FONT" ] && exit 1
  [ ! -f "$SUB_FONT" ] && cp -f "$GEEXBOX$SUB_FONT" "$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 up swap after font files are copied into aram
freesize=`df | grep rootfs | sed -e 's/\> *\</\ /g' | cut -f 4 -d " "`
swapsize=$(($freesize/1024-2))
if [ -f /etc/network -a -f /etc/netstream ]; then
  . /etc/network
  test "$SHOUTCAST" = "no" -a "$SHOUTCASTTV" = "no" -a "$ICECAST" = "no" || swapsize=$(($swapsize-4))
fi
dd if=/dev/zero of=swapfile bs=1024k count="$swapsize" skip="$swapsize"
mkswap swapfile
swapon swapfile
echo 0 > /proc/sys/vm/swappiness

exit 0
