#!/bin/csh -f
###########################################################################
# chfont - change OpenWindows X server font or font scale
#
# chfont is a menu driven cshell script that allows the user to change
# the Window.Scale Resource or the Font.Name resource in the OpenWindows
# X11/NeWS server resource database. The user may also reset both settings
# back to default if either have been changed. The scale menu provides the 
# user with the 4 choices for the Window.Scale resource:
#
#	small, medium, large, extra_large
#
# The fonts menu provides the user with a choice of 11 fixedwidth fonts
# of various widths and point sizes. The fonts were chosen because they
# all work well with terminal emulator windows and are compatible with
# the vi utility.
#
# If the user has previously specified a specific font then he will not
# be allowed to change the scale. This is because setting the Font.Name
# resource will nullify any Window.Scale settings.
#
# The user is prompted to make the runtime changes permanent by editing
# the user's existing ~./Xdefaults. If the user responds positively the
# temporary file used to update the run-time database is copied to the
# users ~/.Xdefaults. The old version is written to ~/.Xdefaults.old
#
# 9/24/91 - Michael O'Connor Sun Microsystems Computer Corp.
#
###########################################################################

set scale = ""
set font = ""
set change_font = FALSE
set change_scale = FALSE
set RESOURCE_FILE = ~/.Xdefaults
set SED_FILE = /tmp/sed.out

# Define various commands
set SED = /usr/bin/sed
set MV = /bin/mv
set XRDB = $OPENWINHOME/bin/xrdb

# Delete old temp files 
/bin/rm -rf $SED_FILE

# Read current scale and font settings (if any) from database
set current_font  = `$XRDB -query | egrep -i font.name`
set current_scale = `$XRDB -query | egrep -i window.scale`

# Create a copy of current existing resource file with Font.Name and
# Window.Scale entries commented out. Will use this new file below
$SED 's/^\([Ww]indow\.[Ss]cale\)/#\1/; s/^\([Ff]ont\.[Nn]ame\)/#\1/'\
	$RESOURCE_FILE > $SED_FILE

clear
echo 	"Which would you like:"
echo	""
echo	"	1) Change scale of default font"
echo	"	2) Select an alternate font"
if ( "$current_font" != "" || "$current_scale" != "" ) then
   echo	"	3) Return to default font and scale settings"
endif
echo	""
echo -n "Enter Choice: "
	
set temp=$<

switch($temp)
	case 1: 
		if ("$current_font" != "") then
			echo ""
			echo "Sorry, you currently have a specific font set"
			echo "To remove setting and return to default font"
			echo "re-run $0 and select (3) from menu."
			echo ""
			exit 1
		endif
		set change_scale = TRUE
		breaksw
	case 2:
		set change_font = TRUE
		breaksw
	case 3:
		echo "Removing all font and scale settings"
		breaksw
	default:
		echo "Invalid selection"
		exit 1
		breaksw
endsw
		
if ($change_scale == "TRUE") then

	clear
	echo    "Select New OpenWindows Font Scale:"
	echo    ""
	echo    "	1. Small"
	echo    "" 
	echo    "	2. Medium (default)"
	echo    ""
	echo    "	3. Large"
	echo    ""
	echo    "  	4. Extra Large"
	echo    ""
	echo -n "Enter Choice [or Ret for default]: "
	
	set temp=$<

	switch($temp)
		case 1: 
			set scale = small
			breaksw
		case 2:
			set scale = medium
			breaksw
		case 3:
			set scale = large
			breaksw
		case 4:
			set scale = extra_large
			breaksw
		default:
			set scale = default
			breaksw
	endsw

	echo ""
	if ( "$current_scale" != "" ) then
		echo "Current value of $current_scale"
	endif
	echo ""
	echo "Setting value of Window.Scale to: $scale"
	if ($scale != "default") then
		echo "Window.Scale: 	$scale" >> $SED_FILE
	endif

else if ($change_font == "TRUE") then

	clear
	echo    "Select New OpenWindows Font:"
	echo    ""
	echo    "	 1.  5 x  8 (very small)"
	echo    "	 2.  6 x 10 (small)"
	echo    "	 3.  7 x 13 lucida bold"
	echo    "  	 4.  8 x 13"
	echo	"	 5.  8 x 13 bold"
	echo	"	 6.  8 x 16 sony"
	echo	"	 7.  9 x 15"
	echo	"	 8.  9 x 15 bold"
	echo	"	 9. screen 16 (big)"
	echo	"	10. gallant 19 (very big)"
	echo	"	11. 12 x 24 sony (huge)"
	echo    ""
	echo -n "Enter Choice [or Ret for default font]: "
	
	set temp=$<

	switch($temp)
		case 1:
			set font = 5x8
			breaksw
		case 2:
			set font = 6x10
			breaksw
		case 3: 
			set font = lucidasans-typewriterbold
			breaksw
		case 4:
			set font = 8x13 
			breaksw
		case 5:
			set font = 8x13bold 
			breaksw
		case 6:
			set font = rk16
			breaksw
		case 7:
			set font = 9x15 
			breaksw
		case 8:
			set font = 9x15bold 
			breaksw
		case 9:
			set font = screen.r.16 
			breaksw
		case 10: 
			set font = gallant.r.19
			breaksw
		case 11:
			set font = rk24
			breaksw
		default:
			set font = default
			breaksw
	endsw

	echo ""
	if ( "$current_font" != "" ) then
		echo "Current value of $current_font"
	endif
	echo ""
	echo "Setting value of Font.Name to: $font"
	if ( $font != "default" ) then
		echo "Font.Name: 	$font" >> $SED_FILE
	endif

endif

# Use xrdb utility to rewrite runtime database with new settings
echo ""
echo "Updating X server resource database... "
$XRDB $SED_FILE >& /dev/null

echo ""
echo -n "Do you wish to save this change for future sessions [y/n]: "
	
set temp=$<

switch($temp)
	case 'y':
	   echo ""
	   echo "Saving current $RESOURCE_FILE to: $RESOURCE_FILE.old"
	   echo ""
	   $MV $RESOURCE_FILE $RESOURCE_FILE.old
	   $MV $SED_FILE $RESOURCE_FILE
	   echo "New settings saved to your $RESOURCE_FILE file"
	   breaksw
	case 'n':
	default:
		echo ""
		echo Changes not saved to $RESOURCE_FILE
		breaksw
endsw

echo ""
echo "done."
echo ""

exit 0
