#!/bin/sh

# Thanks to Mr.KoRoN, Mr.01-0816, Mr.02-0107, Mr.02-370




# Install "mplus" directory [ /usr/X11R6/lib/X11/fonts/mplus ]
DESTDIR=${DESTDIR%/}
PREFIX=${DESTDIR:-/usr/X11R6/lib/X11/fonts/mplus}

# Install M+ BITMAP FONTS EURO (iso8859-15) [ YES or NO ]
EURO15=${EURO15:-YES}

# Install M+ BITMAP FONTS J [ YES or NO ]
MPLUSJ=${MPLUSJ:-YES}

# Install M+ BITMAP FONTS J bold [ YES or NO ]
MKBOLD=${MKBOLD:-YES}

# Select mplus_j10r-iso font [ W4 or W5 ]
J10RIS=${J10RIS:-W4}

# Install M+ cursor [ YES or NO ]
# Original "cursor.pcf.gz" path [ /usr/X11R6/lib/X11/fonts/misc ]
CURSOR=NO
CURPTH=/usr/X11R6/lib/X11/fonts/misc




# Backup/Create mplus directory
if [ -d ${PREFIX}.BAK ]; then
	echo "remove: '${PREFIX}.BAK' directory"
	rm -rf ${PREFIX}.BAK
fi
if [ -d $PREFIX ]; then
	echo "backup: present '${PREFIX##/*/}' directory to '${PREFIX##/*/}.BAK'"
	mv ${PREFIX} ${PREFIX}.BAK
fi
echo "create: new '$PREFIX' directory"
mkdir -p $PREFIX

# Install M+ BITMAP FONTS E
echo -e "\nInstall M+ BITMAP FONTS E (iso8859-1)..."
cd fonts_e

# create hlvw font
awk '/^SWIDTH/{$2 += 80} /^DWIDTH/{$2 += 1} {print}' mplus_h12r.bdf \
| sed 's/hlv/hlvw/'> mplus_h12rw.bdf
for f in *.bdf
do
	b=`basename $f .bdf`
	echo "create: $b.pcf.gz"
	bdftopcf $f | gzip > $PREFIX/$b.pcf.gz
done
echo "create: fonts.alias for M+ BITMAP FONTS E"
cat fonts.alias > $PREFIX/fonts.alias
cd -

# Install M+ BITMAP FONTS EURO
if [ "$EURO15" != "NO" ]; then
	echo -e "\nInstall M+ BITMAP FONTS EURO (iso8859-15)..."
	cd fonts_e/euro
	for f in mplus_*.diff
	do
		b=`basename $f .diff`
		echo "create: $b-euro.pcf.gz"
		cp ../$b.bdf ./
		patch $b.bdf $b.diff
		mv $b.bdf $b-euro.bdf
		bdftopcf $b-euro.bdf | gzip > $PREFIX/$b-euro.pcf.gz
	done

	echo "create: mplus_h12rw-euro.pcf.gz"
	awk '/^SWIDTH/{$2 += 80} /^DWIDTH/{$2 += 1} {print}' \
	mplus_h12r-euro.bdf | sed 's/hlv/hlvw/' > mplus_h12rw-euro.bdf
	bdftopcf mplus_h12rw-euro.bdf \
	| gzip > $PREFIX/mplus_h12rw-euro.pcf.gz

#	rm $b-euro.bdf
	echo "create: fonts.alias for M+ BITMAP FONTS EURO"
	cat fonts.alias >> $PREFIX/fonts.alias
	cd -
fi

# Install M+ BITMAP FONTS J
if [ "$MPLUSJ" != "NO" ]; then
	echo -e "\nInstall M+ BITMAP FONTS J..."
	cd fonts_j

	if [ "$MKBOLD" != "NO" ]; then
		echo "create: mplus_j10b.bdf"
		echo -n "wait a minute..."
		../mkbold -r -R mplus_j10r.bdf | sed 's/medium/bold/' \
		> mplus_j10b.bdf &&
		echo " done"

		echo "create: mplus_j12b.bdf"
		echo -n "wait a minute..."
		../mkbold -r -R mplus_j12r.bdf | sed 's/medium/bold/' \
		> mplus_j12b.bdf &&
		echo " done"

	else
		if [ -f mplus_j1[0,2]b.bdf ]; then
			for f in mplus_j1[0,2]b.bdf
			do
				echo "remove: $f"
				rm $f
			done
		fi
	fi

	cp mplus_j10r-iso-W4 mplus_j10r-iso.bdf
	if [ "$J10RIS" = "W5" ]; then
		echo "select: mplus_j10r-iso.bdf [W5]"
		patch mplus_j10r-iso.bdf mplus_j10r-iso-W5.diff
	else
		echo "select: mplus_j10r-iso.bdf [W4]"
	fi

	for f in mplus_[!j]*-jisx0201.diff
	do
		b=`basename $f -jisx0201.diff`
		echo "create: $b-jisx0201.bdf"
		cp ../fonts_e/$b.bdf ./
		patch $b.bdf $b-jisx0201.diff
		mv $b.bdf $b-jisx0201.bdf
	done

	for f in mplus_j*-jisx0201.diff
	do
		b=`basename $f -jisx0201.diff`
		echo "create: $b-jisx0201.bdf"
		cp $b-iso.bdf $b-jisx0201.bdf
		patch $b-jisx0201.bdf $b-jisx0201.diff
	done

	for f in mplus_*.bdf
	do
		b=`basename $f .bdf`
		echo "create: $b.pcf.gz"
		bdftopcf $f | gzip > $PREFIX/$b.pcf.gz
	done

#	rm mplus_j1*b.bdf
	echo "create: fonts.alias for M+ BITMAP FONTS J"
	cat fonts.alias >> $PREFIX/fonts.alias
	cd -
fi

# Install M+ cursor
if [ "$CURSOR" != "NO" ]; then
	echo -e "\nInstall M+ cursor..."
	if [ -f $CURPTH/cursor.pcf.gz ]; then
		if [ -f $CURPTH/cursor.pcf.gz.ORIGIN ]; then
			:
		else
			echo "backup: cursor.pcf.gz -> cursor.pcf.gz.ORIGIN"
			mv $CURPTH/cursor.pcf.gz $CURPTH/cursor.pcf.gz.ORIGIN
		fi
		echo "create: M+ cursor.pcf.gz"
		bdftopcf mplus_cursors.bdf | gzip > $CURPTH/cursor.pcf.gz
	else
		echo "ERROR: No such file !"
	fi
fi

# Finish
echo -e "\ncreate: fonts.dir file"
mkfontdir $PREFIX
if [ -z $DESTDIR ]; then
    echo -n "xset fp rehash..."
    xset fp rehash &&
    echo " done"
fi
