openpkg/openpkg.sh

Mon, 28 Jan 2013 17:37:18 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Mon, 28 Jan 2013 17:37:18 +0100
changeset 758
a2c6460cfb16
parent 13
cb59d6afeb61
permissions
-rw-r--r--

Correct socket error reporting improvement with IPv6 portable code,
after helpful recommendation by Saúl Ibarra Corretgé on OSips devlist.

     1 #!@l_prefix@/lib/openpkg/bash
     2 ##
     3 ##  openpkg -- OpenPKG Tool Chain
     4 ##  Copyright (c) 2000-2012 OpenPKG GmbH <http://openpkg.com/>
     5 ##
     6 ##  This software is property of the OpenPKG GmbH, DE MUC HRB 160208.
     7 ##  All rights reserved. Licenses which grant limited permission to use,
     8 ##  copy, modify and distribute this software are available from the
     9 ##  OpenPKG GmbH.
    10 ##
    11 ##  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
    12 ##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    13 ##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    14 ##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
    15 ##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    16 ##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    17 ##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    18 ##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    19 ##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    20 ##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    21 ##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    22 ##  SUCH DAMAGE.
    23 ##
    24 ##  openpkg.sh: Execution Frontend (Language: Bourne-Shell)
    25 ##
    27 ##
    28 ##  command line parsing
    29 ##
    31 #   command line options defaults
    32 opt_prefix=""
    33 opt_tools=""
    34 opt_version=no
    35 opt_help=no
    37 #   process command line options by iterating over arguments
    38 for opt
    39 do
    40     case "${opt}" in
    41         -*=*) arg=`echo "${opt}" | sed 's/^[-_a-zA-Z0-9]*=//'` ;;
    42            *) arg='' ;;
    43     esac
    44     case "${opt}" in
    45         -v|--version ) opt_version=yes;     shift ;;
    46         -h|--help    ) opt_help=yes;        shift ;;
    47         --prefix=*   ) opt_prefix="${arg}"; shift ;;
    48         --tools=*    ) opt_tools="${arg}";  shift ;;
    49         -* ) echo "openpkg:ERROR: Invalid command-line option \"${opt}\"." 1>&2
    50              echo "openpkg:ERROR: Run \"${0} --help\" for list of valid options" 1>&2; exit 1 ;;
    51         *  ) break ;;
    52     esac
    53 done
    55 ##
    56 ##  determine OpenPKG locations
    57 ##
    59 #   determine path to OpenPKG instance
    60 openpkg_prefix="@l_prefix@"
    61 if [ ".${OPENPKG_PREFIX}" != . ]; then
    62     openpkg_prefix="${OPENPKG_PREFIX}"
    63 fi
    64 if [ ".${opt_prefix}" != . ]; then
    65     openpkg_prefix="${opt_prefix}"
    66 fi
    67 if [ -x "${openpkg_prefix}/bin/openpkg" -a -x "${openpkg_prefix}/libexec/openpkg/rpm" ]; then
    68     #   OpenPKG 2.0 and higher
    69     true
    70 elif [ -f "${openpkg_prefix}/bin/rpm" -a -x "${openpkg_prefix}/lib/openpkg/rpm" ]; then
    71     #   OpenPKG 1.x
    72     echo "openpkg:ERROR: OpenPKG 1.x instance found under \"${openpkg_prefix}\" (not supported)" 1>&2
    73     exit 1
    74 else
    75     echo "openpkg:ERROR: no OpenPKG instance found under \"${openpkg_prefix}\"" 1>&2
    76     exit 1
    77 fi
    79 #   allow convenient all-in-one specification of OpenPKG Tool Chain locations
    80 #   (assuming the filesystem layout of an uninstalled OpenPKG Tool Chain)
    81 openpkg_tools="${OPENPKG_TOOLS}"
    82 openpkg_tools_cmdpath="${OPENPKG_TOOLS_CMDPATH}"
    83 openpkg_tools_apipath="${OPENPKG_TOOLS_APIPATH}"
    84 if [ ".${opt_tool}" != . ]; then
    85     openpkg_tools="${opt_tools}"
    86 fi
    87 if [ ".${openpkg_tools}" != . -a ".${openpkg_tools_cmdpath}" = . ]; then
    88     openpkg_tools_cmdpath="${openpkg_tools}/cmd:@"
    89 fi
    90 if [ ".${openpkg_tools}" != . -a ".${openpkg_tools_apipath}" = . ]; then
    91     openpkg_tools_apipath="${openpkg_tools}/api:@"
    92 fi
    94 #   determine path to OpenPKG Tool Chain commands
    95 cmdpath="${openpkg_prefix}/libexec/openpkg"
    96 if [ -d "${openpkg_prefix}/libexec/openpkg-bf" ]; then
    97     #   openpkg-bf package overrides
    98     cmdpath="${openpkg_prefix}/libexec/openpkg-bf:${cmdpath}"
    99 fi
   100 if [ -d "${openpkg_prefix}/libexec/openpkg-rg" ]; then
   101     #   openpkg-rg package overrides
   102     cmdpath="${openpkg_prefix}/libexec/openpkg-rg:${cmdpath}"
   103 fi
   104 if [ ".${openpkg_tools_cmdpath}" != . ]; then
   105     #   user supplied path overrides
   106     cmdpath=`echo "${openpkg_tools_cmdpath}" | sed -e "s;@;${cmdpath};"`
   107 fi
   108 openpkg_tools_cmdpath=`echo "${cmdpath}" | sed -e 's/::/:/g' -e 's/^://' -e 's/:$//'`
   110 #   determine path to OpenPKG Tool Chain API
   111 apipath=""
   112 if [ -d "${openpkg_prefix}/lib/openpkg-bf" ]; then
   113     #   openpkg-bf package overrides
   114     apipath="${openpkg_prefix}/lib/openpkg-bf:${apipath}"
   115 fi
   116 if [ -d "${openpkg_prefix}/lib/openpkg-rg" ]; then
   117     #   openpkg-rg package overrides
   118     apipath="${openpkg_prefix}/lib/openpkg-rg:${apipath}"
   119 fi
   120 if [ ".${openpkg_tools_apipath}" != . ]; then
   121     #   user supplied path overrides
   122     apipath=`echo "${openpkg_tools_apipath}" | sed -e "s;@;${apipath};"`
   123 fi
   124 openpkg_tools_apipath=`echo "${apipath}" | sed -e 's/::/:/g' -e 's/^://' -e 's/:$//'`
   126 ##
   127 ##  execute stand-alone option commands in advance
   128 ##
   130 #   implement stand-alone "--help" option
   131 if [ ".${opt_help}" = .yes ]; then
   132     release=`${openpkg_prefix}/libexec/openpkg/rpm --eval '%{l_openpkg_release}'`
   133     echo ""
   134     echo "This is ${release} <http://www.openpkg.org/>"
   135     echo "Cross-Platform Unix Software Packaging Facility"
   136     echo ""
   137     echo "Copyright (c) 2000-2012 OpenPKG GmbH <http://openpkg.com/>"
   138     echo ""
   139     echo "The command \"${openpkg_prefix}/bin/openpkg\" is the official command-line"
   140     echo "execution frontend of the OpenPKG tool chain. Its usage is:"
   141     echo ""
   142     echo "  \$ ${openpkg_prefix}/bin/openpkg [<option> ...] \\"
   143     echo "     <command> [<command-option> ...] [<command-argument> ...]"
   144     echo ""
   145     echo "where <option> is one of the following global options:"
   146     echo "  -p, --prefix    sets the OpenPKG instance prefix   (also: \${OPENPKG_PREFIX})"
   147     echo "  -t, --tools     sets the OpenPKG tool chain prefix (also: \${OPENPKG_TOOLS})"
   148     echo "  -v, --version   display OpenPKG version/release"
   149     echo "  -h, --help      display this usage help message"
   150     echo ""
   151     echo "where <command> is one of the following commands:"
   152     echo "  rpm             (provided by bootstrap package)"
   153     echo "  rpmcache        (provided by bootstrap package)"
   154     echo "  rpmconstant     (provided by bootstrap package)"
   155     echo "  rpmgrep         (provided by bootstrap package)"
   156     echo "  rpm2cpio        (provided by bootstrap package)"
   157     echo "  rpm-config      (provided by bootstrap package)"
   158     echo "  build           (provided by bootstrap package)"
   159     echo "  curl            (provided by bootstrap package)"
   160     echo "  dev             (provided by bootstrap package)"
   161     echo "  index           (provided by bootstrap package)"
   162     echo "  license         (provided by bootstrap package)"
   163     echo "  lint-fsl        (provided by bootstrap package)"
   164     echo "  lint-rc         (provided by bootstrap package)"
   165     echo "  lint-rpm        (provided by bootstrap package)"
   166     echo "  lint-spec       (provided by bootstrap package)"
   167     echo "  lsync           (provided by bootstrap package)"
   168     echo "  makeproxy       (provided by bootstrap package)"
   169     echo "  man             (provided by bootstrap package)"
   170     echo "  rc              (provided by bootstrap package)"
   171     echo "  register        (provided by bootstrap package)"
   172     echo "  release         (provided by bootstrap package)"
   173     echo "  svs             (provided by bootstrap package)"
   174     echo "  uuid            (provided by bootstrap package)"
   176     #   install command intentionally left out in above display!
   178     #   dynamically figure out add-on commands
   179     for cmd in rpm rpmbuild rpm2cpio rpm-config uuid rc lsync man install; do
   180         id=`echo "${cmd}" | sed -e 's/-/_/g'`
   181         eval "__cmd_seen_${id}=yes"
   182     done
   183     OIFS="${IFS}"; IFS=":"
   184     for dir in ${openpkg_tools_cmdpath}; do
   185         IFS="${OIFS}"
   186         if [ ! -d ${dir} ]; then
   187             continue
   188         fi
   189         for cmd in `cd ${dir} 2>/dev/null && echo *`; do
   190             name=`echo "${cmd}" | sed -e 's/\.sh$//' -e 's/\.pl$//' \
   191                  -e 's/^/X/' -e 's/^X\([a-z][a-zA-Z0-9_-]*\)$/\1/' -e 's/^X.*$//'`
   192             if [ ".${name}" != . ]; then
   193                 if [ -f ${dir}/${cmd} ]; then
   194                     id=`echo "${name}" | sed -e 's/-/_/g'`
   195                     eval "seen=\$__cmd_seen_${id}"
   196                     if [ ".${seen}" != .yes ]; then
   197                         echo "${name}" | awk '{ printf("  %-15s (provided by add-on package)\n", $0); }'
   198                         eval "__cmd_seen_${id}=yes"
   199                     fi
   200                 fi
   201             fi
   202         done
   203     done
   204     IFS="${OIFS}"
   205     echo ""
   206     echo "where <command-option> and <command-argument> are <command> specific"
   207     echo "options and arguments. Run \"${openpkg_prefix}/bin/openpkg <command> --help\""
   208     echo "and \"${openpkg_prefix}/bin/openpkg man <command>\" for more details."
   209     echo ""
   210     exit 0
   211 fi
   213 #   implement stand-alone "--version" option
   214 if [ ".${opt_version}" = .yes ]; then
   215     release=`${openpkg_prefix}/libexec/openpkg/rpm --eval '%{l_openpkg_release}'`
   216     version=`${openpkg_prefix}/libexec/openpkg/rpm -q --qf '%{version}' openpkg`
   217     echo "${release} (${version})"
   218     exit 0
   219 fi
   221 ##
   222 ##  determine command details and execute command appropriately
   223 ##
   225 #   command line sanity check
   226 if [ ${#} -eq 0 ]; then
   227     echo "openpkg:ERROR: Invalid command-line arguments." 1>&2
   228     echo "openpkg:ERROR: Run \"${openpkg_prefix}/bin/openpkg --help\" for list of valid arguments." 1>&2
   229     exit 1
   230 fi
   232 #   search command by iterating over all command directories
   233 cmd="${1}"
   234 shift
   235 cmd_path=""
   236 cmd_shell=""
   237 cmd_stack="${OPENPKG_TOOLS_CMDSTACK}"
   238 OIFS="${IFS}"; IFS=":"
   239 for dir in ${openpkg_tools_cmdpath}; do
   240     IFS="${OIFS}"
   242     #   skip (currently) not existing directory
   243     if [ ! -d ${dir} ]; then
   244         continue
   245     fi
   247     #   check for various command implementations
   248     if [ -x ${dir}/${cmd} ]; then
   249         #   found executable stand-alone binary
   250         cmd_path="${dir}/${cmd}"
   251         cmd_shell=""
   252     elif [ -f ${dir}/${cmd}.sh ]; then
   253         #   found non-executable Bourne-Shell script
   254         cmd_path="${dir}/${cmd}.sh"
   255         cmd_shell="${openpkg_prefix}/lib/openpkg/bash"
   256     elif [ -f ${dir}/${cmd}.pl ]; then
   257         #   found non-executable Perl script
   258         cmd_path="${dir}/${cmd}.pl"
   259         if [ -x ${openpkg_prefix}/bin/perl ]; then
   260             cmd_shell="${openpkg_prefix}/bin/perl"
   261         else
   262             cmd_shell=`${openpkg_prefix}/lib/openpkg/shtool path -p "$PATH:$openpkg_prefix/lib/openpkg" -m perl 2>&1`
   263             if [ ".${cmd_shell}" = . ]; then
   264                 echo "openpkg:ERROR: No Perl interpreter found in \${PATH}" 1>&2
   265                 exit 1
   266             fi
   267         fi
   268         #   provide Perl module include path(s) to API
   269         OIFS="${IFS}"; IFS=":"
   270         for dir2 in ${openpkg_tools_apipath}; do
   271             IFS="${OIFS}"
   272             if [ ! -d ${dir2} ]; then
   273                 continue
   274             fi
   275             cmd_shell="${cmd_shell} -I${dir2}"
   276         done
   277         IFS="${OIFS}"
   278     else
   279         #   command not found, continue searching
   280         continue
   281     fi
   283     #   check whether to use this found command or to continue searching
   284     #   for next command implementation in sequence (in order to support
   285     #   flexible nested command wrapping)
   286     cmd_last=`echo "${cmd_stack}" | sed -e 's;:.*$;;'`
   287     if [ ".${cmd_last}" = ".${cmd}" ]; then
   288         #   we were last command on stack, so pop us from call
   289         #   stack and continue searching for next implementation
   290         cmd_stack=`echo "${cmd_stack}" | sed -e 's;^[^:][^:]*:*;;'`
   291         continue
   292     else
   293         #   last command on stack was different, so stop
   294         #   searching because we found the implementation
   295         break
   296     fi
   297 done
   298 IFS="${OIFS}"
   300 #   sanity check search result
   301 if [ ".${cmd_path}" = . ]; then
   302     echo "openpkg:ERROR: No such command \"${cmd}\" found in command path" 1>&2
   303     echo "openpkg:ERROR: (${openpkg_tools_cmdpath})." 1>&2
   304     echo "openpkg:ERROR: Set \${OPENPKG_TOOLS_CMDPATH} appropriately." 1>&2
   305     echo "openpkg:ERROR: Run \"${openpkg_prefix}/bin/openpkg --help\" for list of valid commands." 1>&2
   306     exit 1
   307 fi
   309 #   export essential run-time information to command
   310 export OPENPKG_PREFIX="$openpkg_prefix"
   311 export OPENPKG_TOOLS_CMDPROG="${0}"
   312 export OPENPKG_TOOLS_CMDNAME="${cmd}"
   313 export OPENPKG_TOOLS_CMDSTACK=`echo "${OPENPKG_TOOLS_CMDSTACK}" | sed -e 's;^\(.\);:\1;' -e "s;^;${cmd};"`
   315 #   execute command
   316 eval "exec ${cmd_shell} ${cmd_path} \${1+\"\$@\"}"

mercurial