openpkg/etc.wrapbin.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 445
43a74e63d4a3
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 #!/bin/sh
     2 #![OpenPKG]
     3 ##
     4 ##  OpenPKG Binary Bootstrap Package (self-extracting shell script)
     5 ##  Copyright (c) 2000-2012 OpenPKG GmbH <http://openpkg.com/>
     6 ##
     7 ##  This software is property of the OpenPKG GmbH, DE MUC HRB 160208.
     8 ##  All rights reserved. Licenses which grant limited permission to use,
     9 ##  copy, modify and distribute this software are available from the
    10 ##  OpenPKG GmbH.
    11 ##
    12 ##  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
    13 ##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    14 ##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    15 ##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
    16 ##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    17 ##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    18 ##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    19 ##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    20 ##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    21 ##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    22 ##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    23 ##  SUCH DAMAGE.
    24 ##
    26 #   configuration
    27 l_me="$0"
    28 o_help=no
    29 o_version=no
    30 o_tar=no
    31 l_prefix='@l_prefix@'
    32 l_musr='@MUSR@'
    33 l_mgrp='@MGRP@'
    34 l_platform="@l_platform@"
    35 l_release="@l_release@"
    36 l_version="@l_version@"
    37 l_unprivileged="@l_unprivileged@"
    39 #   establish standard environment
    40 PATH="$PATH:/bin:/sbin:/usr/bin:/usr/sbin"
    41 LC_CTYPE=C
    42 export LC_CTYPE
    43 umask 022
    45 #   parse command line options
    46 for opt
    47 do
    48     case $opt in
    49         -*=*) arg=`echo "$opt" | sed 's/^[-_a-zA-Z0-9]*=//'` ;;
    50            *) arg='' ;;
    51     esac
    52     case $opt in
    53         -h | --help    ) o_help=yes     ;;
    54         -v | --version ) o_version=yes  ;;
    55         -t | --tar     ) o_tar=yes      ;;
    56         --prefix=*     ) l_prefix=$arg  ;;
    57         *              ) o_help=yes     ;;
    58     esac
    59 done
    60 if [ ".$o_version" = .no -a ".$l_prefix" = . ]; then
    61     o_help=yes
    62 fi
    63 if [ ".$o_help" = .yes ]; then
    64     echo "Usage: sh $l_me" 2>&1
    65     echo "       [--prefix=<prefix>] [-t|--tar]" 2>&1
    66     echo "       [-h|--help] [-v|--version]" 2>&1
    67     exit 1
    68 fi
    70 #   make sure all essential installation tools are available
    71 for tool in sed mkdir dd tar chown chgrp; do
    72     found=no
    73     case $tool in
    74         /* )
    75             if [ -f $tool ]; then
    76                 found=yes
    77             fi
    78             ;;
    79         * )
    80             for p in `IFS=:; echo $PATH`; do
    81                 if [ -f "$p/$tool" ]; then
    82                     found=yes
    83                     break
    84                 fi
    85             done
    86             ;;
    87     esac
    88     if [ ".$found" = .no ]; then
    89         echo "$l_me:ERROR: unable to find installation tool \"$tool\"" 1>&2
    90         exit 1
    91     fi
    92 done
    94 #   optionally extract the embedded tarball only
    95 if [ ".$o_tar" = .yes ]; then
    96     tmpdir="${TMPDIR-/tmp}/openpkg.$$"
    97     ( umask 077 && mkdir $tmpdir) || exit 1
    98     dd if=$l_me bs=8192 skip=10 2>/dev/null |\
    99     ( cd $tmpdir || exit 1
   100       tar xf - 2>/dev/null || exit 1
   101       ./openpkg.bzip2 -d -c openpkg.tar.bz2
   102     ) || exit 1
   103     rm -rf $tmpdir
   104     exit 0
   105 fi
   107 #   display version and copyright header
   108 echo "OpenPKG ${l_release} Binary Bootstrap Package, version ${l_version}"
   109 echo "Built for prefix ${l_prefix} on target platform ${l_platform}"
   110 if [ ".$o_version" = .yes ]; then
   111     exit 0
   112 fi
   114 #   determine current username
   115 cusr=`(id -un) 2>/dev/null ||\
   116       (id | sed -e 's;^[^(]*(\([^)]*\)).*;\1;') 2>/dev/null ||\
   117       (whoami) 2>/dev/null ||\
   118       (who am i | cut "-d " -f1) 2>/dev/null ||\
   119       echo ${LOGNAME-"NN"}`
   121 #   running the embedded %pre script for hooking into the system environment
   122 echo "++ hooking OpenPKG instance into system environment"
   123 prefix="$l_prefix"
   124 susr='@SUSR@'; sgrp='@SGRP@'
   125 musr='@MUSR@'; mgrp='@MGRP@'
   126 rusr='@RUSR@'; rgrp='@RGRP@'
   127 nusr='@NUSR@'; ngrp='@NGRP@'
   128 suid='@SUID@'; sgid='@SGID@'
   129 muid='@MUID@'; mgid='@MGID@'
   130 ruid='@RUID@'; rgid='@RGID@'
   131 nuid='@NUID@'; ngid='@NGID@'
   132 unprivileged="$l_unprivileged"
   133 set -- 1 # emulate RPM's $1 when executing scripts
   134 #   ---- BEGIN EMBEDDED %pre SCRIPT ----
   135 @PRE@
   136 #   ---- END EMBEDDED %pre SCRIPT ----
   138 #   make sure prefix/root directory exists
   139 #   and has correct permissions and owner/group
   140 if [ ! -d $l_prefix ]; then
   141     #   create prefix/root directory from scratch
   142     echo "++ creating OpenPKG instance root directory \"$l_prefix\""
   143     d=''
   144     for c in `IFS=/; echo $l_prefix`; do
   145         d="$d/$c"
   146         if [ ! -d $d ]; then
   147             mkdir $d || exit 1
   148             chmod 755 $d || exit 1
   149             if [ ".$cusr" = .root ]; then
   150                 chown $musr $d >/dev/null 2>&1 || true
   151                 chgrp $mgrp $d >/dev/null 2>&1 || true
   152             fi
   153         fi
   154     done
   155 else
   156     #   adjust already existing prefix/root directory
   157     echo "++ fixating OpenPKG instance root directory \"$l_prefix\""
   158     ( cd $l_prefix || exit 1
   159       chmod 755 . || exit 1
   160       if [ ".$cusr" = .root ]; then
   161           chown $musr . >/dev/null 2>&1 || true
   162           chgrp $mgrp . >/dev/null 2>&1 || true
   163       fi
   164     ) || exit 1
   165 fi
   167 #   extract and install binary distribution files
   168 echo "++ extracting OpenPKG binary distribution"
   169 dd if=$l_me bs=8192 skip=10 2>/dev/null |\
   170     (cd $l_prefix; tar xf - 2>/dev/null)
   171 echo "++ installing OpenPKG binary distribution"
   172 ( cd $l_prefix || exit 1
   173   ./openpkg.bzip2 -d -c openpkg.tar.bz2 | ./openpkg.tar xf - 2>/dev/null
   174   rm -f openpkg.tar openpkg.bzip2 openpkg.tar.bz2 >/dev/null 2>&1 || true
   175 ) || exit 1
   177 #   immediately activate BOOT license to let the
   178 #   following early RPM usage already work correctly
   179 echo "BOOT" >$l_prefix/etc/openpkg/license
   181 #   fixate installation files
   182 #   (ATTENTION: order of chgrp/chown and chmod is important because of "setuid" bits)
   183 echo "++ fixating OpenPKG instance filesystem hierarchy"
   184 ( echo 'fixate () {'
   185   echo '    chgrp "$3" "$4"'
   186   echo '    chown "$2" "$4"'
   187   echo '    chmod "$1" "$4"'
   188   echo '}'
   189   cd / && $l_prefix/bin/openpkg --keep-privileges rpm -q openpkg \
   190       --qf '[fixate %7.7{FILEMODES:octal} %{FILEUSERNAME:shescape} %{FILEGROUPNAME:shescape} ::%{FILENAMES:shescape}\n]' |\
   191       grep -v '(none)' | sed 's/^fixate .../fixate /' | sed -e "s; ::\\(.\\)@l_prefix@; \\1$l_prefix;"
   192 ) | sh 2>/dev/null || true
   194 #   running the embedded %post script
   195 echo "++ postprocessing OpenPKG bootstrap installation"
   196 prefix="$l_prefix"
   197 susr='@SUSR@'; sgrp='@SGRP@'
   198 musr='@MUSR@'; mgrp='@MGRP@'
   199 rusr='@RUSR@'; rgrp='@RGRP@'
   200 nusr='@NUSR@'; ngrp='@NGRP@'
   201 suid='@SUID@'; sgid='@SGID@'
   202 muid='@MUID@'; mgid='@MGID@'
   203 ruid='@RUID@'; rgid='@RGID@'
   204 nuid='@NUID@'; ngid='@NGID@'
   205 unprivileged="$l_unprivileged"
   206 set -- 1 # emulate RPM's $1 when executing scripts
   207 #   ---- BEGIN EMBEDDED %post SCRIPT ----
   208 @POST@
   209 #   ---- END EMBEDDED %post SCRIPT ----
   211 #   display final information
   212 ( echo "Congratulations!"
   213   echo ""
   214   echo "You have successfully installed an OpenPKG ${l_release} instance"
   215   echo "under prefix ${l_prefix} on target platform ${l_platform}."
   216   echo ""
   217   echo "For details about this OpenPKG instance, run any of the"
   218   echo "following typical OpenPKG RPM query commands:"
   219   echo ""
   220   echo "    \$ ${l_prefix}/bin/openpkg rpm -qa"
   221   echo "    \$ ${l_prefix}/bin/openpkg rpm -qi  openpkg"
   222   echo "    \$ ${l_prefix}/bin/openpkg rpm -qlv openpkg"
   223   echo ""
   224   echo "To check the integrity of the entire OpenPKG instance,"
   225   echo "run the following OpenPKG RPM verify command:"
   226   echo ""
   227   echo "    \$ ${l_prefix}/bin/openpkg rpm -Va"
   228   echo ""
   229   echo "To install software packages into this OpenPKG instance, run"
   230   echo "the following two OpenPKG RPM build commands for each package:"
   231   echo ""
   232   echo "    \$ ${l_prefix}/bin/openpkg rpm --rebuild /path/to/foo-*.src.rpm"
   233   echo "    \$ ${l_prefix}/bin/openpkg rpm -Uvh ${l_prefix}/RPM/PKG/foo-*.rpm"
   234   echo ""
   235   echo "To leverage from remote indexes on download.openpkg.org and"
   236   echo "conveniently generate a shell script for all in one downloading"
   237   echo "(openpkg curl), building (openpkg rpm --rebuild) and installing"
   238   echo "(openpkg rpm -Uvh) one or more OpenPKG RPM packages, including all"
   239   echo "their transitive dependencies, in topologically correct order:"
   240   echo ""
   241   echo "    \$ ${l_prefix}/bin/openpkg build foo bar quux | sh"
   242   echo ""
   243   echo "To remove a software package later, just run:"
   244   echo ""
   245   echo "    \$ ${l_prefix}/bin/openpkg rpm -e foo"
   246   echo ""
   247   echo "To remove the whole OpenPKG instance under prefix ${l_prefix},"
   248   echo "just remove every package as shown above. As you finally"
   249   echo "remove the package \"openpkg\", the OpenPKG instance itself"
   250   echo "will be unlinked from the system and removed as well."
   251   echo ""
   252   echo "Thank you for flying OpenPKG..."
   253   echo "                                        Ralf S. Engelschall"
   254   echo "                                        OpenPKG GmbH"
   255   echo "                                        openpkg.com"
   256 ) | $l_prefix/lib/openpkg/rpmtool msg -b -t info
   258 #   die explicitly just before the shell would discover
   259 #   that we carry megabytes of data with us... ;-)
   260 exit 0
   262 #   the distribution tarball is appended in raw format directly to the
   263 #   end of this script, just leaded by padding whitespaces which make
   264 #   sure that the tarball data starts at the predefined offset of 80KB.
   265 #   This allows us to unpack the tarball by just skipping the leading
   266 #   80KB (= 8192*10, see above).

mercurial