#!/bin/sh

showhelp () {
cat << EOF
Usage: chkfontscale [ [--help] or [-a file directory] or [-r filename directory]]
-a	:add information of font to fonts.scale
file	  --font.scale file which new font information is included.
directory --target directory which includes font.scale
-r	:remove information of font from fonts.scale
fontname  --Name of fontfile
directory --Target directory which includes font.scale
-help	:help information
EOF
}

for ac_option
do
  case "$ac_option" in
    help | --help | --hel | --he)
	showhelp  
 	exit 0 ;;
  esac
done
if test -z $3; then
	echo "Error: missing argument"
	showhelp
	exit 0 
fi
if [ $1 = -a ]; then
	if !(test -f $2); then
	echo "Error: file is not found: $2"
	exit 0
	fi
	if !(test -d $3); then
	echo "Error: Target directory is not found: $3"
	exit 0
	fi
        #Add to fonts.scale
	if test -f $3/fonts.scale; then
        number1=`head -n 1 $3/fonts.scale`
        number2=`head -n 1 $2`
        echo `expr $number1 + $number2` > $3/fonts.scale.tmp
        tail -n `head -n 1 $3/fonts.scale` $3/fonts.scale \
                         >> $3/fonts.scale.tmp
        tail -n $number2 $2 \
                         >> $3/fonts.scale.tmp
        mv $3/fonts.scale.tmp $3/fonts.scale
	else
	#copy font.scale
	cp $2 $3
	fi
elif [ $1 = -r ]; then
	if !(test -d $3); then
        echo "Error: Target directory is not found: $3"
        exit 0
        fi
	#Remove from fonts.scale 
	number1=`head -n 1 $3/fonts.scale`
	number2=`grep -c $2 $3/fonts.scale`
	echo `expr $number1 - $number2` > $3/fonts.scale.tmp
	grep -v $2 $3/fonts.scale |tail -n `expr $number1 - $number2` \
        	        >> $3/fonts.scale.tmp
	mv $3/fonts.scale.tmp $3/fonts.scale
else
	showhelp
	exit 0
fi
