#!/bin/sh
#
# Copyright (c) 2002 Michal Moskal <malekith@pld-linux.org>.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#	This product includes software developed by Michal Moskal.
# 4. Neither the name of the author nor the names of any co-contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY MICHAL MOSKAL AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $Id: ui-mirror,v 1.1 2002/07/26 10:24:21 malekith Exp $
#

mirrors=/etc/MIRRORS

export PATH=.:..:$PATH
add_title="_Mirror _selection"
. installer-functions
. ui-functions

output_entry () {
  for u in $url ; do
    echo "<item id='$u'>$u"
    echo -n "<item ghosted=1>  "
    if [ "X$organisation" != "XN/A" ] ; then
      echo -n "$organisation, "
    fi
    echo "$city, $country"
    if [ "X$notes" != "X" ] ; then
      echo "<item ghosted=1>  $(echo "$notes" | sed -e 's/"//g')"
    fi
    echo "<item ghosted=1>"
  done
  organisation=
  country=
  city=
  url=
  notes=
}

parse_mirrors_file () {
  #set -x
  url=
  output_entry # reset
  last_var=
  while read line ; do
    case "$line" in
      "#"* )
        : skip
        ;;
      "["* )
        last_var=
        output_entry
	;;
      "" )
        ;;
      * )
        # can't use pipe here
        echo "$line" > /tmp/p
        read word1 word2 value < /tmp/p
	#echo "line='$line' '$word1' '$word2' '$value'"
        if [ "X$word2" = "X=" ] ; then
	  case "$word1" in
            organisation | country | city | url | notes)
	      last_var=$word1
	      last_value="$value "
	      eval $last_var=\"\$value\"
	      ;;
	    * )
	      last_var=
	      ;;
	  esac
	elif [ "X$last_var" != "X" ] ; then
	  # continuation
	  eval $last_var=\"\$last_value\$line\"
	  last_value="$last_value$line "
        fi
	;;
    esac
  done < $mirrors
  output_entry 
}

if [ "$1" = "just-dump" ] ; then
  parse_mirrors_file
else
  dial <<EOF
<meta title="$(nls "Please select mirror:")">
<menu height=20 id=mirror res=1>
$(parse_mirrors_file)
</menu>
EOF

  echo $mirror
fi
