#! /bin/sh
#
# Name: msttcorefonts
# Date: 2007-10-23
# Author: SpinetiX S.A.
# Copyright: Copyright 2007 SpinetiX S.A.
# License: 2007 (c) SpinetiX S.A. This file is licensed
#          under the terms of the GNU General Public License version 2.
#          This program is licensed "as is" without any warranty of any
#          kind, whether express or implied.
#
# Copyright 1999-2003 MontaVista Software, Inc.
# Copyright 2002, 2003, 2004 Sony Corporation
# Copyright 2002, 2003, 2004 Matsushita Electric Industrial Co., Ltd.
#
### BEGIN INIT INFO
# Required-Start: 
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 
# Default-Stop: 
# Short-Description: unpackage core fonts for the web
# Description: unpackage core fonts for the web
### END INIT INFO 
# chkconfig: 235 50 0

# Init script information
INIT_NAME=msttcorefonts
DESC="core fonts"

# Constants
SYSFONTDIR=/usr/share/fonts
COREFONTDIR="$SYSFONTDIR"/msttcorefonts
FONTPKGDIR=/usr/share/msttcorefonts
TSFILE="$COREFONTDIR"/timestamp
FCCACHE=/usr/bin/fc-cache

# Load init script configuration
[ -f /etc/default/$INIT_NAME ] && . /etc/default/$INIT_NAME

# Source the init script functions
. /etc/init.d/init-functions

# Each init script action is defined below...

start() {
	local RET ERROR=

	local run_fccache=

	if [ ! -f "$TSFILE" ]; then
	    log_status_msg "Starting $DESC: " -n
	    log_status_msg "unpacking" -n
	    cabextract -F "*.ttf" -L -q -d "$COREFONTDIR" "$FONTPKGDIR"/*.exe
	    RET=$?
	    if [ $RET -eq 0 ]; then
		log_success_msg ", " -n
		touch "$TSFILE"
	    else
		log_failure_msg " failed ($RET: $ERROR)."
		return 1
	    fi
	    run_fccache="yes"
	fi
	
	if [ -x "$FCCACHE" -a "$run_fccache" = "yes" ]; then
	    log_status_msg "updating fc-cache" -n
	    # NOTE: we need to run fc-cache on the parent system font dir
	    # for the cache to completely catch the fonts.
	    "$FCCACHE" -s "$SYSFONTDIR"
	    RET=$?
	    if [ $RET -eq 0 ]; then
		log_success_msg "."
	    else
		log_failure_msg " failed ($RET: $ERROR)."
		return 1
	    fi
	fi
	log_status_msg ""
	return 0
}

parse() {
	case "$1" in
  start)
			start
			return $?
	;;
  stop)
			return 0
	;;
  *)
			echo "Usage: $INIT_NAME " \
			"{start|stop}" >&2
	;;
	esac
	
	return 1
}

parse $@

