#!/bin/bash
# ######################################################################
# "a2gs/debian/postinst" by Kenneth MacDonald <K.MacDonald@ed.ac.uk>
# I hereby place this shell script in the public domain. February 1995
# ######################################################################
# This script  allows the  user  to  update the   contents of   a  file,
# "/etc/papersize" by default, to be one of the  valid media types known
# by ghostview. The `a2gs' Debian package makes use of this file to help
# determine the required paper size.
# ######################################################################

set -e

# ######################################################################
# Check if I'm running as 'root'. Exit with message if not.
# ######################################################################

if [ $EUID != 0 ] ; then
	echo "$0: Cannot execute: Try again as user 'root'!"
	exit 1;
fi

PATH=/bin:/usr/bin
if [ ${1:-null} = "-f" ] ; then
	FILE_PAPERSIZE=${2:-/etc/papersize};
else
	FILE_PAPERSIZE=/etc/papersize;
fi

readonly PATH FILE_PAPERSIZE

# ######################################################################
# Function to set the PAPERSIZE variable to either "A4" or "Letter"
# The third option gives a less common selection.
# ######################################################################

papersize () {
	echo -e ' 1..A4\n 2..Letter\n 3..Other\n'
	echo -n "Which paper size (1-3) ?"
	read NO_PAPERSIZE
	echo
	case $NO_PAPERSIZE in
		1) PAPERSIZE="a4";;
		2) PAPERSIZE="letter";;
		3) PAPERSIZE= others ;;
		*) echo -e "Try again!!\n" ;
		   papersize;;
	esac
}
readonly -f papersize


# ######################################################################
# Function to choose a less common media size.
# ######################################################################

others () {
	echo -e " 1..tabloid\n 2..ledger\n 3..legal\n 4..statement"
	echo -e " 5..executive\n 6..a3\n 7..a5\n 8..b4"
	echo -e " 9..b5\n10..folio\n11..quarto\n12..10x14\n"
	echo -n "Which paper size (1-12) ? "
	read NO_PAPERSIZE
	echo
	case $NO_PAPERSIZE in
		1) PAPERSIZE="tabloid";;
		2) PAPERSIZE="ledger";;
		3) PAPERSIZE="legal";;
		4) PAPERSIZE="statement";;
		5) PAPERSIZE="executive";;
		6) PAPERSIZE="a3";;
		7) PAPERSIZE="a5";;
		8) PAPERSIZE="b4";;
		9) PAPERSIZE="b5";;
		10) PAPERSIZE="folio";;
		11) PAPERSIZE="quarto";;
		12) PAPERSIZE="10x14";;
		*) echo -e "Try again!!\n";
		   papersize;;
	esac
}
readonly -f others

# ######################################################################
# What is the present paper size? "undefined" if none.
# ######################################################################

if [ ! -f $FILE_PAPERSIZE ]; then

# ######################################################################
# Get the new media size
# ######################################################################

	echo -e "Please choose the system wide default paper size...\n"
	papersize;

# ######################################################################
# Make the new file, and set it world readable and owned by root.root
# ######################################################################

	echo $PAPERSIZE > $FILE_PAPERSIZE
	chmod a+r $FILE_PAPERSIZE

# ######################################################################
# Report what was done.
# ######################################################################

	echo "System is now configured to use \"`cat $FILE_PAPERSIZE`\" media."
fi

exit 0
