#!/bin/sh
##
##  OpenPKG Source Bootstrap Package (self-extracting shell script)
##  Copyright (c) 2000-2007 OpenPKG Foundation e.V. <http://openpkg.net/>
##  Copyright (c) 2000-2007 Ralf S. Engelschall <http://engelschall.com/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   configuration
l_me="$0"
o_help=no
o_version=no
o_tar=no
l_prefix='/openpkg'
l_dir='@l_dir@'
l_release="@l_release@"
l_version="@l_version@"

#   establish standard environment
PATH="$PATH:/bin:/sbin:/usr/bin:/usr/sbin"
LC_CTYPE=C
export LC_CTYPE
umask 022

#   pre-parse command line options
for opt
do
    case $opt in
        -*=*) arg=`echo "$opt" | sed 's/^[-_a-zA-Z0-9]*=//'` ;;
           *) arg='' ;;
    esac
    case $opt in
        -h | --help     ) o_help=yes    ;;
        -v | --version  ) o_version=yes ;;
        -t | --tar      ) o_tar=yes     ;;
        --prefix=*      ) l_prefix=$arg ;;
    esac
done
if [ ".$o_help" = .yes ]; then
    echo "Usage: sh $l_me" 2>&1
    echo "       [--prefix=<prefix>] [--tag=<str>]" 2>&1
    echo "       [--user=<usr>] [--group=<grp>]" 2>&1
    echo "       [--{s,m,r,n}usr=<usr>] [--{s,m,r,n}grp=<grp>]" 2>&1
    echo "       [--{s,m,r,n}uid=<uid>] [--{s,m,r,n}gid=<gid>]" 2>&1
    echo "       [--use_tar=<tar>] [--use_make=<make>] [--use_cc=<cc>]" 2>&1
    echo "       [--use_ar=<ar>] [--use_ld=<ld>] [--use_as=<as>] [--use_strip=<strip>]" 2>&1
    echo "       [-t|--tar] [-h|--help] [-v|--version]" 2>&1
    exit 1
fi

#   make sure all essential unpacking tools are available
#   (the build tools are checked later from within openpkg.spec)
for tool in /bin/sh mkdir cat tar rm chown chgrp sed dd; do
    found=no
    case $tool in
        /* )
            if [ -f $tool ]; then
                found=yes
            fi
            ;;
        * )
            for p in `IFS=:; echo $PATH`; do
                if [ -f "$p/$tool" ]; then
                    found=yes
                    break
                fi
            done
            ;;
    esac
    if [ ".$found" = .no ]; then
        echo "$l_me:ERROR: unable to find bootstrap tool \"$tool\"" 1>&2
        exit 1
    fi
done

#   optionally extract the embedded tarball only
if [ ".$o_tar" = .yes ]; then
    dd if=$l_me bs=8192 skip=8 2>/dev/null
    exit 0
fi

#   display version and copyright header
echo "OpenPKG ${l_release} Source Bootstrap Package, version ${l_version}"
if [ ".$o_version" = .yes ]; then
    exit 0
fi
echo "Building for prefix ${l_prefix} on current platform"

#   determine current user/group
cusr=`(id -un) 2>/dev/null ||\
      (id | sed -e 's;^[^(]*(\([^)]*\)).*;\1;') 2>/dev/null ||\
      (whoami) 2>/dev/null ||\
      (who am i | cut "-d " -f1) 2>/dev/null ||\
      echo $LOGNAME`
cgid=`(id -g $cusr) 2>/dev/null ||\
      ((getent passwd "${cusr}"; grep "^${cusr}:" /etc/passwd; ypmatch "${cusr}" passwd; nismatch "${cusr}" passwd; nidump passwd . | grep "^${cusr}:") 2>/dev/null |\
      sed -e 'q' | awk -F: '{ print $4; }')`
cgrp=`(id -gn $cusr) 2>/dev/null ||\
      ((getent group; cat /etc/group; ypcat group; niscat group; nidump group .) 2>/dev/null | grep "^[^:]*:[^:]*:${cgid}:" |\
      sed -e 'q' | awk -F: '{ print $1; }')`
if [ ".$cgrp" = . ]; then
    cgrp="$cusr"
fi

#   extract the source distribution files
echo "++ extracting OpenPKG source distribution"
rm -rf $l_dir >/dev/null 2>&1
mkdir $l_dir || exit 1
dd if=$l_me bs=8192 skip=8 2>/dev/null | (cd $l_dir; tar xf - 2>/dev/null)
if [ ".$cusr" = .root ]; then
    ( cd $l_dir || exit 1
      chown -R -h $cusr . >/dev/null 2>&1 || true
      chgrp -R -h $cgrp . >/dev/null 2>&1 || true
    ) || exit 1
fi
if [ ! -f $l_dir/openpkg.boot ]; then
    echo "$l_me:ERROR: failed to unpack into directory \"$l_dir\"" 1>&2
    exit 1
fi

#   perform bootstrap procedure
echo "++ building OpenPKG binary distribution"
( cd $l_dir || exit 1
  ./openpkg.boot ${1+"$@"} || exit 1
) || exit 1

#   cleanup
rm -rf $l_dir >/dev/null 2>&1

#   die explicitly just before the shell would discover
#   that we carry mega-bytes of data with us...
exit 0

#   the distribution tarball is appended in raw format directly to the
#   end of this script, just leaded by padding whitespaces which make
#   sure that the tarball data starts at the pre-defined offset of 64KB.
#   This allows us to unpack the tarball by just skipping the leading
#   64KB (= 8192*8, see above).

