#!/bin/sh
#
# Script to get Cygwin Net release files for ogmtools
#
#   mkdir ogmtools ; cd ogmtools
#   wget -q -O - http://cygwin.cante.net/ogmtools/get.sh | sh
#
# To check existing download, run this; in subshell:
#
#   ( test=1 . get.sh && Verify )

PATH="/usr/bin:$PATH"

set -e

URL="http://cygwin.cante.net/ogmtools"

FILES="\
  setup.hint\
  ogmtools-1.5-1.tar.bz2.sig\
  ogmtools-1.5-1.tar.bz2\
  ogmtools-1.5-1-src.tar.bz2.sig\
  ogmtools-1.5-1-src.tar.bz2\
"

Download ()
{
    # Without this re-downloaded files would be numbered: <file(s)>.1
    [ "$FILES" ]  &&  rm -f $FILES

    current=$(pwd)

    for file in $FILES
    do
        #  Download to current directory unless there are subdirectories
        cd $current

        case $file in
            */*)    dir=$(dirname $file)
                    mkdir -p $dir
                    cd $dir
                    ;;
        esac

        wget --no-verbose "$URL/$file"
    done
}

List ()
{
    for file in $FILES
    do
        ls -la $file
    done
}

Verify ()
{
    sigext=.sig
    gpg=$(which gpg)

    #  Cannot verify signatures without gpg
    [ ! "$gpg" ] && return

    for file in $FILES
    do
        if [ -f $file$sigext ]; then
            if $gpg --verify $file$sigext $file > /dev/null 2>&1 ; then
                echo "gpg signature check [ok]     $file"
            else
                echo "gpg signature check [failed] $file"
            fi
        fi
    done
}

Msg ()
{
     #   Instructions after download
     echo "
[binary] package view instructions:

  tar -jtvf ogmtools-1.5-1.tar.bz2

[binary] package install instructions:

  tar -C / -jxvf ogmtools-1.5-1.tar.bz2
  ... There may be additional scripts, that need to be run. Please check:
  ...    /etc/preremove/ogmtools.sh
  ...    /etc/postinstall/ogmtools.sh

[source] package instructions:

  tar -jxvf  ogmtools-1.5-1-src.tar.bz2
  bash ./ogmtools*.sh --verbose --color all

The shell script in *-src* archive can make a Cygwin Net release [binary]
package, but it is not guaranteed that it can by itself make a [source]
package. If the build interrupts, please install full cygbuild suite:


  http://cygwin.cante.net/cygbuild/readme.txt

[gpg] signature verify commands:

  gpg --keyserver wwwkeys.pgp.net --recv-keys 955A92D8
  gpg --verify  ogmtools-1.5-1-src.tar.bz2.sig ogmtools-1.5-1-src.tar.bz2
  gpg --verify  ogmtools-1.5-1.tar.bz2.sig ogmtools-1.5-1.tar.bz2
"
}

Main ()
{
    Download
    List
    Verify
    Msg
}

${test+echo} Main "$@"

# End of script
