#!/bin/sh
#
# wdb2marker
#
# $Id: wdb2marker,v 1.1 2003/01/23 05:57:15 elho Exp $
#
# Copyright 2003 Elmar Hoffmann
#
# Convert the text version of the CIA World DataBank II available from
# http://www.evl.uic.edu/pape/data/WDB/ to marker files suitable for
# Xplanet.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

export PATH='/usr/sbin:/usr/bin:/sbin:/bin'
set -e

VERBOSE=
USESYMBOLSIZE=

function usage () {
    cat <<EOF
usage: $(basename $0) [options]
  -h, --help            show this usage message.
  -s, --use-symbolsize  use symbolsize instead of image to get 1 pixel symbols
			(this requires a custom Xplanet)
  -v, --verbose         be verbose.
  -V, --version         show version.
EOF
}

function version () {
    cat <<EOF
wdb2marker $Revision: 1.1 $
Copyright 2003 Elmar Hoffmann
This is free software; see the GNU General Public Licence version 2 or
later for copying conditions.  There is NO warranty.
EOF
}

function error () {
    echo "$(basename $0): $1"
}

# wdbtxt-file, marker-file, marker-options
function wdbtxt2marker () {
    [ ${VERBOSE} ] && echo "Processing \"${1}\""
    if [ ! -s "${1}" ]; then
        error "${1}: No such file"
        exit 10
    fi

    sed "/^segment/d;s/^[[:blank:]]*\([0-9.-]*\) \([0-9.-]*\)$/\1 \2 \"\" ${3}/" ${1} >${2}
}

# country
function country2marker () {
    if [ ${USESYMBOLSIZE} ]; then
	wdbtxt2marker "${1}-cil.txt" "${1}-cil.marker" "symbolsize=1 color=green"
	wdbtxt2marker "${1}-riv.txt" "${1}-riv.marker" "symbolsize=1 color=blue"
	wdbtxt2marker "${1}-bdy.txt" "${1}-bdy.marker" "symbolsize=1 color=yellow"
    else
	wdbtxt2marker "${1}-cil.txt" "${1}-cil.marker" "image=green.png"
	wdbtxt2marker "${1}-riv.txt" "${1}-riv.marker" "image=blue.png"
	wdbtxt2marker "${1}-bdy.txt" "${1}-bdy.marker" "image=yellow.png"
    fi
    cat "${1}-cil.marker" "${1}-riv.marker" "${1}-bdy.marker" >"${1}.marker"
}

while [ $# -gt 0 ]; do
    case "$1" in
        -h|--help)
            usage
            exit 0
            ;;
        -V|--version)
            version
            exit 0
            ;;
        -s|--use-symbolsize)
            USESYMBOLSIZE="TRUE"
            ;;
        -v|--verbose)
            VERBOSE="TRUE"
            ;;
        --)                     # stop processing parameters after --
            break
            ;;
        -*)
            error "unrecognized option \`$1'"
            usage
            exit 10
            ;;
        *)
            WDBDIR="$1"
            break
            ;;
    esac
    shift
done

country2marker africa
country2marker asia
country2marker europe
country2marker namer
country2marker samer

exit 0
