#!/bin/sh
##
##  Shell-based package for OpenPKG SOURCE bootstrap procedure
##  Copyright (c) 2000-2002 Cable & Wireless Deutschland GmbH
##  Copyright (c) 2000-2002 The OpenPKG Project <http://www.openpkg.org/>
##  Copyright (c) 2000-2002 Ralf S. Engelschall <rse@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.
##

#   program parameters
me="$0"
dir='@DIR@'
tgz='@TGZ@'

#   establish standard environment
LC_CTYPE=C
export LC_CTYPE
umask 022

#   pre-parse command line options
help=0
prefix=''
for opt
do
    case $opt in
        -*=*) arg=`echo "$opt" | sed 's/^[-_a-zA-Z0-9]*=//'` ;;
           *) arg='' ;;
    esac
    case $opt in
        -h | --help          ) help=1      ;;
        --prefix=*           ) prefix=$arg ;;
    esac
done
if [ ".$prefix" = . ]; then
    help=1
fi
if [ ".$help" = .1 ]; then
    echo "Usage: sh $me" 2>&1
    echo "       --prefix=<prefix> [--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 "       [-h|--help] [-v|--verbose]" 2>&1
    exit 1
fi

#   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`
cgrp=`(cat /etc/passwd; ypcat passwd) 2>/dev/null |\
      grep "^${cusr}:" | awk -F: '{ print $4; }'`
cgrp=`(cat /etc/group; ypcat group) 2>/dev/null |\
      grep ":${cgrp}:" | awk -F: '{ print $1; }'`
if [ ".$cgrp" = . ]; then
    cgrp="$cusr"
fi

#   extract the tarball
echo "$me: extracting to $dir..."
uudecode $me
rm -rf $dir >/dev/null 2>&1
mkdir $dir || exit 1
uncompress <$tgz |\
(cd $dir; tar x${v}f - 2>/dev/null)
if [ ".$cusr" = .root ]; then
    ( cd $dir 
      chown -R -h $cusr . >/dev/null 2>&1 || true
      chgrp -R -h $cgrp . >/dev/null 2>&1 || true
    )
fi
echo "$me: extraction done."

#   perform bootstrap procedure
echo "$me: building for $prefix..."
cd $dir || exit 1
/bin/sh ./openpkg.boot "$@" || exit 1
echo "$me: building done."

#   cleanup
echo "$me: cleaning up..."
cd ..
rm -rf $dir >/dev/null 2>&1
rm -f  $tgz >/dev/null 2>&1
echo "$me: cleaned up."

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

#   the distribution tarball
