#!/bin/sh

set -e

config=/etc/exult.cfg

setkeys='%d = ("/config/debian-warning" => 0);
%k = @ARGV;
while (<STDIN>) {
  if (m|</(.*?)>|) {
    $p =~ s|/$1$|| or die "line $.: </$1> unexpected\n";
    print if $skip < 2;
    $skip = 0;
    next
  }
  next if $skip;
  if (m|(\s*)<(.*?)>|) {
    $i = $1;
    $p .= "/$2";
    exists $d{$p} and $skip = 2, next;
    print;
    exists $k{$p} and $skip = 1, print "$i$k{$p}\n";
    next;
  }
  print;
}'

case "$1" in
	configure)
		. /usr/share/debconf/confmodule
		db_get exult/blackgate
		blackgate="${RET:-.}"
		db_get exult/serpent
		serpent="${RET:-.}"
		if [ -e $config ]; then
			perl -e "$setkeys" \
				/config/disk/game/blackgate/path $blackgate \
				/config/disk/game/serpentisle/path $serpent \
				<$config >$config.dpkg-new
			mv $config.dpkg-new $config
		else
			cat <<EOF >$config
<config>
 <disk>
  <game>
   <blackgate>
    <title>
    blackgate
    </title>
    <path>
    /home/user/MyDocs/Games/exult/bg
    </path>
    <gamedat_path>
    /home/user/.exult/blackgate/gamedat
    </gamedat_path>
    <savegame_path>
    /home/user/.exult/blackgate
    </savegame_path>
    <keys>
    (default)
    </keys>
    <waves>
    sqsfxbg.flx
    </waves>
   </blackgate>
   <serpentisle>
    <title>
    serpentisle
    </title>
    <path>
    /home/user/MyDocs/Games/exult/SI
    </path>
    <gamedat_path>
    /home/user/.exult/serpentisle/gamedat
    </gamedat_path>
    <savegame_path>
    /home/user/.exult/serpentisle
    </savegame_path>
    <keys>
    (default)
    </keys>
    <waves>
    sqsfxsi.flx
    </waves>
   </serpentisle>
  </game>
  <music_path>
  music
  </music_path>
  <save_compression_level>
  1
  </save_compression_level>
 </disk>
 <video>
  <width>
  400
  </width>
  <height>
  240
  </height>
  <scale_method>
  Scale2X
  </scale_method>
  <fullscreen>
  yes
  </fullscreen>
  <disable_fades>
  no
  </disable_fades>
  <fps>
  10
  </fps>
  <scale>
  2
  </scale>
 </video>
 <gameplay>
  <right_click_closes_gumps>
  no
  </right_click_closes_gumps>
  <gumps_dont_pause_game>
  no
  </gumps_dont_pause_game>
  <bg_paperdolls>
  yes
  </bg_paperdolls>
  <smooth_scrolling>
  100
  </smooth_scrolling>
  <mouse3rd>
  no
  </mouse3rd>
  <fastmouse>
  no
  </fastmouse>
  <double_click_closes_gumps>
  yes
  </double_click_closes_gumps>
  <combat>
   <difficulty>
   0
   </difficulty>
   <mode>
   original
   </mode>
   <show_hits>
   yes
   </show_hits>
  </combat>
  <step_tile_delta>
  8
  </step_tile_delta>
  <allow_double_right_move>
  yes
  </allow_double_right_move>
  <formation>
  yes
  </formation>
  <skip_splash>
  no
  </skip_splash>
  <skip_intro>
  no
  </skip_intro>
  <cheat>
  no
  </cheat>
  <facestats>
  0
  </facestats>
  <textbackground>
  -1
  </textbackground>
 </gameplay>
 <audio>
  <disablepause>
  no
  </disablepause>
  <enabled>
  yes
  </enabled>
  <effects>
   <enabled>
   yes
   </enabled>
   <convert>
   gs
   </convert>
   <driver>
   normal
   </driver>
  </effects>
  <midi>
   <enabled>
   yes
   </enabled>
   <convert>
   gs127
   </convert>
   <reverb>
    <enabled>
    yes
    </enabled>
    <level>
    0
    </level>
   </reverb>
   <chorus>
    <enabled>
    no
    </enabled>
    <level>
    0
    </level>
   </chorus>
   <volume_curve>
   1.0000
   </volume_curve>
   <looping>
   yes
   </looping>
   <driver>
   normal
   </driver>
  </midi>
  <speech>
   <enabled>
   yes
   </enabled>
  </speech>
 </audio>
</config>
EOF
		fi
	;;

	abort-upgrade|abort-remove|abort-deconfigure)

	;;

	*)
		echo "postinst called with unknown argument \`$1'" >&2
		exit 1
	;;
esac

#DEBHELPER#

exit 0
