#!/bin/sh -

xrlogin=xt
# Note: also update colors in /app/env/etc/config.pl
opsyscolors='aix6   => "violet",	win08  => "gold",
	     aix7   => "magenta",	win12  => "orange",
	     darwin => "purple",	win16  => "peru",
	     redhat => "sea green",	win19  => "sienna",
	     suse   => "lime green",	win    => "gold",
	     ubuntu => "green",
	     linux  => "pale green"'	# "tan"
test "$1" = -colors && { echo "$opsyscolors"; exit; }

# makehostmenus - generate and insert customized host menus into a .twmrc file
# Steve Kinzler, steve@kinzler.com, Feb 94/Aug 15/Nov 23
# https://kinzler.com/me/home.html#x11

append=; stdout=; bad=; GEN=
file="${TWMRC-$HOME/.twmrc}"
string='# DO NOT DELETE THIS LINE -- makehostmenus depends on it'

while :
do
	case $# in
	0)	break;;
	*)	case "$1" in
		-a)	append=t;;
		-o)	stdout=t;;
		-f)	shift; file="$1";;
		-s)	shift; string="$1";;

		-GEN)	GEN="$1";;

		-h)	bad=t; break;;
		-*)	bad=t; echo "$0: unknown option ($1)" 1>&2;;
		*)	bad=t; break;;
		esac
		shift;;
	esac
done

case "$bad" in
?*)	cat << EOF 1>&2
usage: $0 [ -a | -o ] [ -f file ] [ -s string ]
	-a	append host menus to file (default replace)
	-o	only output the host menus to stdout
	-f	modify the given file (default ${TWMRC-$HOME/.twmrc})
	-s	recognize and use the given delimiter string in the file
EOF
	exit 1;;
esac

case "$GEN" in
?*)	exec perl -ane '
		%c = ('"$opsyscolors"');
		$c = ($c = $c{$F[6]}) ? qq<("white":"$c") > : "";
		printf qq(\t"%-23s%s!"%s %s &"\n), qq($F[0]"), $c,
						   "'"$xrlogin"'", $F[0];';;
esac

tmp="$file"~
trap "rm -f $tmp; exit" 0 1 2 13 15

case "$append" in
?*)	cat < "$file";;
*)	delim=`echo "$string" | sed 's/[$*.[\\\\^]/\\\\&/g'`
	sed "/^$delim$/,\$d" < "$file";;
esac > $tmp || exit 2

(cat << EOF; echo '') >> $tmp
$string
# Generated by makehostmenus, to end of this file
EOF

(
cat << EOF
menu "MyHosts" {
	"My Hosts"		f.title
EOF
hosts -l | "$0" -GEN
echo '}'

#cat << EOF
#menu "MyHosts" {
#	"My Hosts"		f.title
#	"a. My Hosts 1"		("white":"blue") f.menu "MyHosts1"
#	"b. My Hosts 2"		("white":"blue") f.menu "MyHosts2"
#	"c. My Hosts 3"		("white":"blue") f.menu "MyHosts3"
#	"d. My Hosts 4"		("white":"blue") f.menu "MyHosts4"
#}
#menu "MyHosts1" {
#	"My Hosts 1"		f.title
#EOF
#hosts -l | perl -e '@_ = <>; print @_[0 ..
#				      int($#_ / 4 * 1)]' | "$0" -GEN; echo '}'
#cat << EOF
#menu "MyHosts2" {
#	"My Hosts 2"		f.title
#EOF
#hosts -l | perl -e '@_ = <>; print @_[int($#_ / 4 * 1) + 1 ..
#				      int($#_ / 4 * 2)]' | "$0" -GEN; echo '}'
#cat << EOF
#menu "MyHosts3" {
#	"My Hosts 3"		f.title
#EOF
#hosts -l | perl -e '@_ = <>; print @_[int($#_ / 4 * 2) + 1 ..
#				      int($#_ / 4 * 3)]' | "$0" -GEN; echo '}'
#cat << EOF
#menu "MyHosts4" {
#	"My Hosts 4"		f.title
#EOF
#hosts -l | perl -e '@_ = <>; print @_[int($#_ / 4 * 3) + 1 ..
#				      $#_]'		 | "$0" -GEN; echo '}'

#menu "Hostlists" {
#	"Hostlists"		f.title
#}
#EOF
#	"unix"			("white":"blue") f.menu "Hostlist unix"
#	"foreign"		("white":"blue") f.menu "Hostlist foreign"
#hostlist -ms unix 2> /dev/null | #foreign |
#	sed 's/	!"xrlogin /	!"'"$xrlogin"' /
#	     s/	f.menu /	("white":"blue") f.menu /'

) |
case "$stdout" in
?*)	cat; exit 0;;
*)	cat >> $tmp;;
esac

cmp $tmp "$file" > /dev/null 2>&1 && exit
test -s $tmp && cat < $tmp > "$file"
