#!/bin/sh

SEND_CONFIG_TO="asvogler@cip.informatik.uni-erlangen.de"

MANUFACT="Unknown"
MODEL="Unknown"
MODES="GM_ALL"
FLAGS="FL_MON_POWERSAVE"
MAXDOTS="0x0"
DIAGONALSIZE="0"
PHYSIZEMM="0x0"
DISPTYPE="VGA_COLOR"
SYNCTYPE="SYNC_SEPARATE"
BANDWIDTH="70"
HFREQ="31-90"
VFREQ="50-160"
WHITE="[Not set]"
GAMMA="[Not set]"

show_menu() {
dialog \
--title "Define new monitor" \
--menu "Please fill in as much information as possible about your monitor" \
21 70 10 \
"Manufacturer" "$MANUFACT" \
"Model" "$MODEL" \
"Modes (8/16bit etc)" "$MODES" \
"Special flags" "$FLAGS" \
"Resolution" "$MAXDOTS" \
"Picture tube diagonal in inch" "$DIAGONALSIZE" \
"Physical size in mm" "$PHYSIZEMM" \
"Displaytype" "$DISPTYPE" \
"Syncing" "$SYNCTYPE" \
"Bandwidth (in MHz)" "$BANDWIDTH" \
"Horizontal freq. range" "$HFREQ" \
"Vertical freq. range" "$VFREQ" \
"View config" "" \
"Mail entry"  "Mail entry to the database" \
2> /tmp/.new-monitor
INPUT=`cat /tmp/.new-monitor`
}

simple_input() {
dialog \
--title "Define new monitor" \
--inputbox "$1" 21 60 2>/tmp/.new-monitor
INPUT2=`cat /tmp/.new-monitor`
}

list_to_c() {
	echo $1 | sed -e ':x
s/\([A-Z_0-9]\+\)[^A-Z|_0-9]\+\([A-Z_0-9]\+\)/\1\|\2/
s/[^A-Z_0-9]\+$//
s/^[^A-Z_0-9]\+//
t x'
}

range_to_c() {
	echo -n "{ "
	echo -n `list_to_c $1 | sed -e 's/|/, /'`
	echo -n " }"
}

create_config() {
cat <<EOF
/*
 Manufacturer      : $MANUFACT
 Model             : $MODEL
 Modes             : $MODES
 Flags             : $FLAGS
 Resolution        : $MAXDOTS
 Tube diagonal     : $DIAGONALSIZE
 Physical size     : $PHYSIZEMM
 Displaytype       : $DISPTYPE
 Syncing           : $SYNCTYPE
 Bandwidth         : $BANDWIDTH
 Horiz freq. range : $HFREQ
 Vert freq. range  : $VFREQ
*/
#ifdef __${MANUFACT}_${MODEL}__
struct kgi_monitor
monitor = {
	"$MANUFACT",
	"$MODEL",
	`list_to_c "$MODES"`,
	`list_to_c "$FLAGS"`,
	`range_to_c $MAXDOTS`,
	NULL,
	`range_to_c $PHYSIZEMM`,
	$DISPTYPE,
	`list_to_c "$SYNCTYPE"`,
	{ 0, ${BANDWIDTH}000000 },
	`range_to_c $HFREQ | sed -e 's/\([0-9]\+\)/\1000/g'`,
	`range_to_c $VFREQ`
};
#endif /* __${MANUFACT}_${MODEL}__ */
EOF
}

show_config() {
	create_config >/tmp/.create_config
	dialog --textbox /tmp/.create_config 21 70
	rm /tmp/.create_config
}



parse_input() {
case "$INPUT" in
   Manu*)
	simple_input '
Please enter the name of the manufacturer.\n(excluding 
extensions such as "Inc.")'
	MANUFACT="$INPUT2"
	;;
   Model*) 
	simple_input 'Please enter the model name'
	MODEL="$INPUT2"
	;;
   Modes*) 
	simple_input '
Please enter a comma-separated list of the modes 
supported by the monitor. GM_1,GM_4,GM_8,GM_15,
GM_16,GM_24,GM_32, and GM_ALL are allowed.'
	MODES="$INPUT2"
	;;
   Special*)
	simple_input '
Please enter a comma-separated list of special 
features for this monitor. Available features are:
    FL_MON_RIGHT_EYE (stereo right screen), 
    FL_MON_LEFT_EYE  (stereo left screen), 
    FL_MON_INTERLACE (interlaced operation), 
    FL_MON_POWERSAVE (has powersave option), 
    FL_MON_PROPSIZE  (size depends on mode)'
	FLAGS="$INPUT2"
	;;
   Res*)
	simple_input ' 
Please enter the maximal resolution this monitor 
supports eg. 1600x1280.'
	MAXDOTS="$INPUT2"
	;;
   Pictu*)
	simple_input '
Please enter the length of the picture tube diagonal
in inch. '
	DIAGONALSIZE="$INPUT2"
	;;
   Physical*)
	simple_input '
Please enter the physical size of your monitor in mm. 
(1 inch = 25.4 mm) eg. 350x300. '
	PHYSIZEMM="$INPUT2"
	;;
   Disp*)
	simple_input '
Please enter the type of display. Valid types are EGA_8, 
EGA_16, CGA, VGA_COLOR, PLASMA, TFT and COLOR_LCD'
	DISPTYPE="$INPUT2"
	;;
   Sync*)
	simple_input \
'Please enter the type of syncing the display supports. 
Valid types are (one or more of) 
          SYNC_SEPARATE (normal separate sync), 
          SYNC_ON_RED/GREEN/BLUE (sync on r/g/b line), 
          SYNC_SERRATE (do hsync during vsync), 
          SYNC_COMPOSITE (digital composite).
          SYNC_STANDBY (standby mode allowed),
          SYNC_SUSPEND (suspend mode allowed),
          SYNC_OFF     (switch off allowed),
          SYNC_VESA_DPMS (all 3/full VESA-DPMS).
          SYNC_SSYNC_RIGHT (right image on SSYNC),
          SYNC_SSYNC_LEFT (left image on SSYNC),
          SYNC_MULTISYNC (multisync monitor)'
	SYNCTYPE="$INPUT2"
	;;
   Band*)
	simple_input '
Please enter the maximal bandwidth of your monitor in MHz'
	BANDWIDTH="$INPUT2"
	;;
   Horiz*)
	simple_input '
Please enter the horizontal frequency range the monitor 
syncs on in kHz.'
	HFREQ="$INPUT2"
	;;
   Vert*)
	simple_input '
Please enter the vertical frequency range the monitor
syncs on in Hz.'
	VFREQ="$INPUT2"
	;;
    View*)
	show_config
	;;
    Mail*)
	echo "`create_config`" | mail -s "MONITOR: add $MANUFACT/$MODEL" "$SEND_CONFIG_TO"
	dialog --title "Confirm" --yesno "Are you sure you want to send this entry to the
monitor maintainer $SEND_CONFIG_TO?" 6 64
	if [ "$?" = "0" ]; then
		create_config | mail -s "MONITOR: add $MANUFACT/$MODEL" $SEND_CONFIG_TO
		if [ "$?" = "0" ]; then
			dialog --title 'Thank you!' \
			--msgbox "Your submission has been sent and will hopefully 
be part of the next GGI release." 6 60
		else
			create_config >/tmp/monitor_config
			dialog --title 'Trouble!' \
			--msgbox "There might be some trouble getting this entry mailed 
to the monitor maintainer. Please try to mail the contents of
/tmp/monitor_config to $SEND_CONFIG_TO yourself." 9 64
		fi
	fi
	;;
esac
}

INPUT="x"
while [ "$INPUT" != "Finish" -a "$INPUT" != "" ]; do
	show_menu
	parse_input
done
