#!/bin/sh
# Copyright (c) 2010 Roman Neuhauser
# Distributed under the MIT license
# vim: ft=sh sw=2 sts=2 ts=2 et fdm=marker cms=\ #\ %s

: ${PYINSTALLER_ROOT:=/usr/local/lib/pyinstaller}

if test x = "x$PYTHONEXE" && which python >/dev/null 2>&1; then
  PYTHONEXE=python
fi
: ${PYTHONEXE:?}

if test 0 -eq $#; then
  set help
fi

usage() # {{{
{
  printf "${0##*/} command args...\n\navailable commands:\n"
  printf "  %-10s  %s\n" \
    help "display this text" \
    show "display configuration" \
    build "process spec file" \
    mkspec "create spec file" \
    reconf "configure pyinstaller"

  exit ${1-0}
} # }}}
show_config() # {{{
{
  local vars="python=PYTHONEXE root=PYINSTALLER_ROOT"
  local named=0 pre=""
  if test 0 -eq $#; then
    named=1
    pre='$a = '
    set root python
  fi
  for arg; do
    a="${arg#--}"
    for spec in $vars; do
      l=${spec%=*}
      v=${spec#*=}
      if test "x$l" = "x$a"; then
        eval echo "$pre\$$v"
      fi
    done
  done
  exit
} # }}}

while :; do
  case "$1" in
  mkspec)
    file=Makespec.py; shift; break
  ;;
  build)
    file=Build.py; shift; break
  ;;
  reconf)
    file=Configure.py; shift; break
  ;;
  show)
    shift; show_config ${1+"$@"}; exit
  ;;
  help|-h|--help)
    usage 0
  ;;
  failed)
    usage 1
  ;;
  *)
    set failed "$@"
  ;;
  esac
done
exec "$PYTHONEXE" "$PYINSTALLER_ROOT/$file" ${1+"$@"}
