#!/bin/sh
# -----------------------------------------------------------------------------
#
# mingw-port.sh
#
  APPNAME="mingw-pkg" APPVERSION="1.0-rc-2"
#
# A simple package construction tool for creation of MinGW packages.
#
# $Id: mingw-port.sh,v 53f4b6a40196 2020/08/21 14:28:25 keith $
#
# Written by Keith Marshall <keith@users.osdn.me>
# Copyright (C) 2020, MinGW.org Project
#
  COPYRIGHT_YEARS="2020"
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# -----------------------------------------------------------------------------
#
# Save the effective command name, for use in diagnostic messages.
#
  cmd=`basename "$0" .sh` cmd_version=${APPVERSION-"1.0-rc-2"}
  usage() { cat <<-ETX
	${unbold}usage: $cmd [--help[=synopsis | options | description]]

	       $cmd [-V | --version] | [--licence | --license]

	       $cmd [--download-only] [-P | --ports=dir]
	 	 [-D | --depot=dir] [-R | --repository=URI]
	 	 [--in-tree] package-version
	 
	ETX
  }

# Establish the paths for application support files and extensions.
#
  approotdir=`dirname "$0"`
  test "x`basename "$approotdir"`" = xbin && approotdir=`dirname "$approotdir"`
  libexecdir=${libexecdir-"$approotdir/libexec"}/$APPNAME/$APPVERSION

# Implement the module/plugin loader; (note that the "die" function here
# is a simplified fall-back implementation, to handle any failure to load
# a required module, before a more comprehensive implementation has been
# sourced)...
#
  require() { load "$@" || die "$1 '$2' not found"; }
  load() { test -f "$libexecdir/${1}s/$2.sh" && . "$libexecdir/${1}s/$2.sh"; }
  die() { echo >&2 "$error_colour$cmd: *** FATAL *** $@$unbold"; exit 2; }

# ...then load that more comprehensive diagnostic message handler, followed
# by the options definition handler.
#
  require module dmh
  require module optdefn

# Define options, which are specific to the mingw-port command, and implement
# their handlers; begin with the standard options, for display of the built-in
# help, the program version, and the licence.
#
  optdefine h help optional_argument
  opteval_help() { require module porthelp; }

  optdefine V version
  opteval_version() { require module licence short; }

  optdefine licence; optalias license=licence
  opteval_licence() { require module licence full; }

# Option -C, --cache=dir, specifies a local directory for persistent storage
# of downloaded package files; by default, they are discarded after use.
#
  optdefine C cache requires_argument
  opteval_cache() { APPNAME="$APPNAME DOWNLOAD_CACHE_DIR='$optarg'"; }

# Option -D, --depot=dir, specifies a working directory, in which to build
# mingw-ports; defaults to current directory, when mingw-port is run.
#
  optdefine D depot requires_argument
  opteval_depot() { APPNAME="$APPNAME PACKAGE_DEPOT='$optarg'"; }

# Option -P, --ports=dir, specifies a local directory to store, and search
# for, mingw-port specifications; by default, current directory is searched
# first, followed by internet repository; specifying "-P dir" causes "dir"
# to be searched after current directory, but before any internet search,
# and downloaded files to be stored in "dir".
#
  optdefine P ports requires_argument
  opteval_ports() { APPNAME="$APPNAME MINGW_PORT_DIR='$optarg'"; }

# Option -R, --repository=URL, specifies the internet "URL", from whence
# mingw-port specifications may be downloaded; the default is the standard
# MinGW ports repository, at "https://osdn.net/dl/mingw/mingw-ports".
#
  optdefine R repository requires_argument
  opteval_repository() { APPNAME="$APPNAME MINGW_PORT_REPOSITORY='$optarg'"; }

# Option --download-only suppresses the normal build phase of mingw-port
# execution, terminating after download, and unpacking of the mingw-port
# specification, and associated package sources.
#
  optdefine download-only
  opteval_download_only() { download_only=true; }

# Option --in-tree forces mingw-port to change directory, to the top of the
# package source tree, before proceeding to the build phase of execution,
# thus initiating an in-tree (in-source) build; by default, the current
# directory, at the time of mingw-port invocation, will become the active
# build directory, for an out-of-tree (out-of-source) build.
#
  optdefine in-tree
  opteval_in_tree() { in_tree=true; }

# Evaluate processing options and local variable assignments, as specified
# by the user, on the command line.
#
  while optchk "$@"
  do shift $argshift; test "x${optmatch}x" = "x--x" && break; done

# When all options have been evaluated, there should be exactly one agrument
# remaining, (representing the <package-version> ID for the port); if not,
# diagnose and abort.
#
  test $# -eq 1 || {
    invalid_usage() { local why
      test $1 -gt 1 && why="too many arguments" || why="missing argument"
      echo "syntax error: $why"; usage
    }
    die 1 "`invalid_usage $#`"
  }

# The download, and unpacking, phase is always executed; it is delegated to
# the mingw-pkg application, which, together with any settings to propagate
# from mingw-port options, is named in the APPNAME variable.
#
  $APPNAME --id="$1" import

# The build phase is optional; as with download and unpacking, it too is
# delegated to mingw-pkg, after first changing directory if necessary.
#
  ${download_only-false} || { build_actions="patch configure compile"
    ${in_tree-false} && { cd "${PACKAGE_DEPOT-.}/$1"; $APPNAME $build_actions
    } || $APPNAME --srcdir="${PACKAGE_DEPOT-.}/$1" $build_actions
  } 
#
# -----------------------------------------------------------------------------
# $RCSfile: mingw-port.sh,v $: end of file
