#!/bin/bash

QUIT=no

while [ $QUIT = "no" ]; do

dialog --title "Greek support 1.2" \
--menu "\\nWe are about to install Greek support for your Linux \
console.\n\n(Use the arrow keys or the +/- and Tab keys to move)" 13 70 3 \
"R" "Read the README file" \
"I" "Install Greek support 1.2" \
"E" "Exit" 2>/tmp/option~

case `cat /tmp/option~` in
	R) dialog --title "Greek support README file" --textbox README 25 80;;
	I) QUIT="install";;
	E) QUIT="yes";;
	"") QUIT="yes";;
esac
done

rm -f /tmp/option~

if [ $QUIT != "install" ]; then exit; fi

dialog --title "Greek support 1.2" \
--msgbox "Step 1 of 7:\n\n"\
"We'll copy the following fonts to /usr/lib/kbd/consolefonts:\n\n"\
"grk8x11.fnt . . . . . . . 8x11 437 font with Greek characters\n"\
"grk8x8.fnt . . . . . . . . . .  Likewise, using an 8x8 matrix\n"\
"grk9x14.fnt . . . . . . . . . . . . . . . . . 9x14 Greek font\n"\
"grk9x16.fnt  . . . . . . . . . . . . . . . .  9x16 Greek font\n"\
"grk9x16mdv.fnt  . . .  9x16 medieval font w/ Greek characters\n" \
14 67 2>/tmp/option~
cp grk*.fnt /usr/lib/kbd/consolefonts
chown root.root /usr/lib/kbd/consolefonts/grk*.fnt
chmod u=rw,go=r /usr/lib/kbd/consolefonts/grk*.fnt


dialog --title "Greek support 1.2" \
--msgbox "Step 2 of 7:\n\n"\
"We'll copy the greek character translation table to\n"\
"/usr/lib/kbd/consoletrans.\n" \
10 67 2>/tmp/option~
cp trivial /usr/lib/kbd/consoletrans
chown root.root /usr/lib/kbd/consoletrans/trivial
chmod u=rw,go=r /usr/lib/kbd/consoletrans/trivial


dialog --title "Greek support 1.2" \
--msgbox "Step 3 of 7:\n\n"\
"We'll copy the greek keyboard map to /usr/lib/kbd/keytables.\n" \
9 67 2>/tmp/option~
cp greek.map /usr/lib/kbd/keytables
chown root.root /usr/lib/kbd/keytables/greek.map
chmod u=rw,go=r /usr/lib/kbd/keytables/greek.map


dialog --title "Greek support 1.2" \
--msgbox "Step 4 of 7:\n\n"\
"We'll copy the following scripts to /usr/bin:\n\n"\
"gren . . . . . . . . .  Enables Greek support for the console\n"\
"grdis . . . . . . . . . .  Re-Enables LATIN-1 and US keyboard\n"\
"reset  . . . . . . . . . . . . An updated version of reset(1)\n"\
"grconv  . . Converts Greek and 437 graphics to standard ASCII\n" \
13 67 2>/tmp/option~
cp /usr/bin/gren /usr/bin/grdis /usr/bin/reset /usr/bin/grconv /tmp >&/dev/null
cp gren grdis reset grconv /usr/bin >&/dev/null
chown root.bin /usr/bin/gren /usr/bin/grdis /usr/bin/reset /usr/bin/grconv
chmod u=rwx,go-rwx /usr/bin/gren /usr/bin/grdis /usr/bin/reset
chmod u=rwx,go=rx /usr/bin/grconv

dialog --title "Greek support 1.2" \
--msgbox "\nCopies of any old versions of the gren, grdis, reset(1)\n"\
"and grconv scripts can be found in /tmp.\n\n"\
"You might like to change /usr/bin/gren and /usr/bin/grdis to\n"\
"cater for the fonts and keymaps you normally use.\n" \
12 67 2>/tmp/option~


if grep gren /etc/rc.d/rc.local >&/dev/null; then \
	NB="NB: /etc/rc.d/rc.local appears to contain this already.\n";
	LINES=15
else
	NB="";
	LINES=13
fi

if \
dialog --title "Greek support 1.2" \
--yesno "Step 5 of 7:\n\n"\
"The following lines can be appended to /etc/rc.d/rc.local:\n\n"\
"             # Enabling Greek support 1.2\n"\
"             /usr/bin/gren\n\n"\
"$NB"\
"\nWould you like to patch /etc/rc.d/rc.local?" \
$LINES 67 2>/tmp/option~; \
then
	cat <<EOF >>/etc/rc.d/rc.local

# Enabling Greek support 1.2
/usr/bin/gren

EOF
fi


if \
dialog --title "Greek support 1.2" \
--yesno "Step 6 of 7:\n\n"\
"In order to make Greek support more 'sticky', we must make\n"\
"sure that the system does not revert back to LATIN-1 when\n"\
"the rs directive is sent to the console. A modified termcap\n"\
"is provided for this purpose. If you choose to install it,\n"\
"the old version will be gzipped and stored as\n"\
"/etc/termcap-old.gz\n\n"\
"Would you like to install the Greek-aware /etc/termcap file?" \
15 67 2>/tmp/option~; \
then
   if [ ! -e /etc/termcap-old.gz ]; then
	cd /etc
	mv termcap termcap-old
	gzip -9 termcap-old
	cd -
	cp termcap.gz /etc/
	cd /etc
	gunzip termcap.gz
	chown root.root termcap
	chmod u=rx,go=r termcap
	cd -
   else
dialog --title "Greek support 1.2" \
--msgbox "\nThis change appears to have already been applied.\n"\
"To force it to be re-applied, remove file /etc/termcap-old.gz\n"\
"and re-run the installation script.\n" \
9 67 2>/tmp/option~;
   fi
fi


if \
dialog --title "Greek support 1.2" \
--yesno "Step 7 of 7:\n\n"\
"[N]Curses is in the habit of resetting the terminal, and\n"\
"every time it switches to its alternate character set, it\n"\
"forces the console out of the Greek translation table. To\n"\
"counter this, a modified version of the console terminfo\n"\
"entry can be placed in /usr/lib/terminfo/c/console. The\n"\
"old version will be retained as console.orig.\n\n"\
"Would you like to install the Greek-aware terminfo entry?" \
15 67 2>/tmp/option~; \
then
    if [ ! -e /usr/lib/terminfo/c/console.grk ]; then
	mv /usr/lib/terminfo/c/console /usr/lib/terminfo/c/console.orig
	cp console.grk console.source.grk /usr/lib/terminfo/c/
	chown root.root /usr/lib/terminfo/c/*
	chmod u=rw,go=r /usr/lib/terminfo/c/*
	ln -s /usr/lib/terminfo/c/console.grk /usr/lib/terminfo/c/console
   else
dialog --title "Greek support 1.2" \
--msgbox "\nThis change appears to have already been applied.\n"\
"To force it to be re-applied, remove the symbolic link\n"\
"/usr/lib/terminfo/c/console, keep a backup copy of\n"\
"/usr/lib/terminfo/c/console.orig, and re-run the installation\n"\
"script.\n" \
11 67 2>/tmp/option~;
    fi
fi

clear
gren
echo
echo "Greek support 1.2 is now (hopefully) installed."
echo "Here's a little test:"
echo
cat TEST.GREEK
echo
echo Please read the README file for more information.
echo
