#!/bin/bash
#
# Copyright (C) 2008-2011 Francesco P. Lovergine <frankie@debian.org>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, 
# as published by the Free Software Foundation.
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

usage() { 
	echo "usage: $0 [high|full|high-full|low|all] [version]" 
}

set -e

if [ $(id -u) -ne 0 ]; then
	echo "Run this script as root"
	exit 1
fi

#
# Default is downloading the latest version available.
#
VERSION=""

if [ ! -z "$2" ]; then
		case "$2" in
			1.9)
				;&
			1.10)
				;&
			2.0)
				;&
			2.0.1)
				;&
			2.0.2)
				;&
			2.1.0)
				;&
			2.1.1)
				VERSION="$2"
				;;
			*)
				echo "Unknown version $2, assumed correct anyway"
				VERSION="$2"
				;;
		esac
fi

#
# Default is downloading both full and high res coast lines.
#
if [ $# -eq 0 ]; then
	COAST_LINES=high-full
else
	case "$1" in
		high-full)
			COAST_LINES=high-full
			;;
		high)
			COAST_LINES=high
			;;
		low)
			COAST_LINES=low
			;;
		full)
			COAST_LINES=full
			;;
		all)
			COAST_LINES=all
			;;
		help|-h|--help)
			usage
			exit 5
			;;
		*)
			echo "Unknown argument '$1'" && usage
			exit 4
			;;
	esac
fi

MIRROR[1]=ftp://ftp.soest.hawaii.edu/gmt
MIRROR[2]=ftp://ibis.grdl.noaa.gov/pub/gmt
MIRROR[3]=ftp://ftp.iris.washington.edu/pub/gmt
MIRROR[4]=ftp://ftp.iag.usp.br/pub/gmt
MIRROR[5]=ftp://ftp.geologi.uio.no/pub/gmt
MIRROR[6]=ftp://gd.tuwien.ac.at/pub/gmt
MIRROR[7]=ftp://ftp.scc.u-tokai.ac.jp/pub/gmt
MIRROR[8]=ftp://mirror.geosci.usyd.edu.au/pub/gmt

cat <<EOF

 This script downloads both high-resolution and full resolution coast lines
 for GMT (Generic Mapping Tools) and installs the resulting CDF files under 
 /usr/share/gmt/coast for use.

 Choose a mirror site (default 1, * marked sites maintain old versions too):

 1) ${MIRROR[1]}
 2) ${MIRROR[2]}
*3) ${MIRROR[3]}
 4) ${MIRROR[4]}
 5) ${MIRROR[5]}
 6) ${MIRROR[6]}
*7) ${MIRROR[7]}
 8) ${MIRROR[8]}

EOF

echo -n "Insert a mirror ID: "
read -n 1 mirror 

case "$mirror" in
	[1-8])  SITE=${MIRROR[$mirror]}
		;;
	*)      SITE=${MIRROR[1]}
		;;
esac

SHARE_DIR=/usr/share/gmt

if [ ! -d $SHARE_DIR ]; then
	echo "GMT target directory $SHARE_DIR not found"
	exit 2
fi

TMPDIR=`mktemp -t -d gmt-coast.XXXXXXXX` || exit 3

URL_HIGH="$SITE/GSHHS${VERSION}_high.tar.bz2"
URL_FULL="$SITE/GSHHS${VERSION}_full.tar.bz2"
if [ -x /usr/bin/wget ]; then
	GET_HIGH="wget --progress=bar -O$TMPDIR/GSHHS_high.tar.bz2 $URL_HIGH"
	GET_FULL="wget --progress=bar -O$TMPDIR/GSHHS_full.tar.bz2 $URL_FULL"
	else if [ -x /usr/bin/curl ]; then
		GET_HIGH="curl --progress-bar -o$TMPDIR/GSHHS_high.tar.bz2 $URL_HIGH"
		GET_FULL="curl --progress-bar -o$TMPDIR/GSHHS_full.tar.bz2 $URL_FULL"
	     fi
fi

echo $GET_HIGH && $GET_HIGH && tar xojvf $TMPDIR/GSHHS_high.tar.bz2 -C $SHARE_DIR/coast --strip 2 share/coast
echo $GET_FULL && $GET_FULL && tar xojvf $TMPDIR/GSHHS_full.tar.bz2 -C $SHARE_DIR/coast --strip 2 share/coast

rm -rf $TMPDIR

cat <<EOF

 Coast lines downloaded and installed under $SHARE_DIR/coast.

EOF

exit 0
