#! /bin/sh
# run-xearth --- run xearth with misc standard options

# Copyright (C) 1995 Noah S. Friedman

# Author: Noah Friedman <friedman@prep.ai.mit.edu>
# Created: 1995-04-05

# $Id: run-xearth,v 1.4 1996/03/03 22:59:47 friedman Exp $

# 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, 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, you can either send email to this
# program's maintainer or write to: The Free Software Foundation,
# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.

# Commentary:
# Code:

# Name by which this script was invoked.
progname=`echo "$0" | sed -e 's/[^\/]*\///g'`

# To prevent hairy quoting and escaping later.
bq='`'
eq="'"

usage="Usage: $progname {options}

Options are:
-D, --debug                  Turn on shell debugging ($bq${bq}set -x$eq$eq).
-c, --city           CITY    Use CITY's geographic coordinates
-h, --help                   You're looking at it.
-o, --once                   Generate a bitmap and display it, then exit.
"

# Various constants

# Cambridge, MA: 42'22''30N 71'07''15W
# Austin, TX: 30'16''01N 97'44''34W
coord_austin='-pos fixed,30.26,-97.73'
coord_cambridge='-pos fixed,42.36,-71.11'
coord_boston="$coord_cambridge"
coord_default=

# Initialize variables.
# Don't use `unset' since old bourne shells don't have this command.
# Instead, assign them an empty value.
debug=
city=
once=

# Usage: eval "$getopt"; value=$optarg
# or     optarg_optional=t; eval "$getopt"; value=$optarg
#
# This function automatically shifts the positional args as appropriate.
# The argument to an option is optional if the variable `optarg_optional'
# is non-empty.  Otherwise, the argument is required and getopt will cause
# the program to exit on an error.  optarg_optional is reset to be empty
# after every call to getopt.  The argument (if any) is stored in the
# variable `optarg'.
#
# Long option syntax is `--foo=bar' or `--foo bar'.  2nd argument
# won't get used if first long option syntax was used.
#
# Note: because of broken bourne shells, using --foo=bar syntax can
# actually screw the quoting of args that end with trailing newlines.
# Specifically, most shells strip trailing newlines from substituted
# output, regardless of quoting.
getopt='
  {
    optarg=
    case "$1" in
      --*=* )
        optarg=`echo "$1" | sed -e "1s/^[^=]*=//"`
        shift
       ;;
      * )
        case ${2+set} in
          set )
            optarg="$2"
            shift
            shift
           ;;
          * )
            case "$optarg_optional" in
              "" )
                case "$1" in
                  --*=* ) option=`echo "$1" | sed -e "1s/=.*//;q"` ;;
                  * ) option="$1" ;;
                esac
                exec 1>&2
                echo "$progname: option $bq$option$eq requires argument."
                echo "$progname: use $bq--help$eq to list option syntax."
                exit 1
               ;;
           esac
         ;;
        esac
     ;;
    esac
    optarg_optional=
  }'

# Parse command line arguments.
# Make sure that all wildcarded options are long enough to be unambiguous.
# It's a good idea to document the full long option name in each case.
# Long options which take arguments will need a `*' appended to the
# canonical name to match the value appended after the `=' character.
while : ; do
  case $# in 0) break ;; esac
  case "$1" in
    -D | --debug | --d* )
      debug=t
      shift
     ;;
    -c | --city* | --c* )
      eval "$getopt"
      city="$optarg"
     ;;
    -h | --help | --h )
      echo "$usage" 1>&2
      exit 0
     ;;
    -o | --once | --o* )
      once=t
      shift
     ;;
    -- )     # Stop option processing
      shift
      break
     ;;
    -? | --* )
      case "$1" in
        --*=* ) arg=`echo "$1" | sed -e 's/=.*//'` ;;
        * )     arg="$1" ;;
      esac
      exec 1>&2
      echo "$progname: unknown or ambiguous option $bq$arg$eq"
      echo "$progname: Use $bq--help$eq for a list of options."
      exit 1
     ;;
    -??* )
      # Split grouped single options into separate args and try again
      optarg="$1"
      shift
      set fnord `echo "x$optarg" | sed -e 's/^x-//;s/\(.\)/-\1 /g'` ${1+"$@"}
      shift
     ;;
    * )
      break
     ;;
  esac
done

case "$debug" in t ) set -x ;; esac

case "$city" in
  '' ) : ;;
  * )
    eval coord_default=\"\${coord_$city}\"
    case "$coord_default" in
      '' )
        exec 1>&2
        echo "$progname: No coordinates for $bq$city$eq.  Aborting."
        exit 1
       ;;
    esac
   ;;
esac

case "$once" in
  t )
    # First, determine the size of the gif file to create
    case "${XRDB_SYMBOLS+set}" in
      set ) : ;;
      * )
        XRDB_SYMBOLS="`xrdb -symbols`"
        case "$DISPLAY" in
          # If display does not contain an explicit screen number (e.g. 0.0),
          # then there are multiple screens associated with the display and
          # xrdb -symbols is going to spit out comments.
          # We really want a way to determine which syms we want since they
          # may vary from screen to screen, but I just don't know of any
          # method of deciding which to use.
          *:*.* ) : ;;
          * )     XRDB_SYMBOLS="`echo \"$XRDB_SYMBOLS\" | sed -e '/^#/d'`" ;;
        esac
       ;;
    esac

    xrdb_symbol_value='{
                         echo "$XRDB_SYMBOLS" \
                          | sed -ne "/-D${sym}=/!d
                                     s/.*-D${sym}=//
                                     s/-D.*//
                                     s/[ 	][ 	]*/ /g
                                     s/^[ 	][ 	]*//g
                                     s/[ 	][ 	]*\$//g
                                     p
                                     q"
                       }'

    width=`sym=WIDTH; eval "$xrdb_symbol_value"`
    height=`sym=HEIGHT; eval "$xrdb_symbol_value"`

    xearth -nolabel \
           -noshade \
           -size "$width,$height" \
           -gif \
           $coord_default \
           ${1+"$@"} \
     | xv -root -rmode 5 -quit -
   ;;
  * )
    exec xearth \
         -label \
         -stars \
         -twopix \
         -nomarkers \
         -night 15 \
         -wait 300 \
         -nice 19 \
         $coord_default \
         ${1+"$@"}
   ;;
esac

# run-xearth ends here
