#!/bin/sh
# $Id: mkipkg,v 1.9 2001/06/29 23:49:00 hausmann Exp $

# TODO: srcdir!=builddir

usage()
{
    echo "usage: $0 --destdir=[destination installation directory] --control=[path to control.in]"
    exit 1
}

destdir=
strip=strip
control=

for option
do
    case "$option" in
        -*=*)
            arg=`echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'`
            ;;
    esac

    case "$option" in
        --destdir=*)
            destdir=$arg
            ;;
        --strip=*)
            strip=$arg
            ;;
       --control=*)
            control=$arg
            ;;
    esac
done

if [ -z "$destdir" -o -z "$control" ]; then
    usage
fi

if [ ! -r konq-embedrc ]; then
    echo "$0: cannot find konq-embedrc, exiting..."
    exit 1
fi

if [ ! -r $control ]; then
   echo "$0: cannot find $control, exiting..."
   exit 1
fi

confdir=`find $destdir -type d -name config`
if [ -z "$confdir" ]; then
    echo "$0: cannot find config dir in $destdir, exiting..."
    exit 1
fi

echo "*** preparing configuration"

cp konq-embedrc $confdir/

# hack ;-)
binary=`find $destdir -type f -name "konqueror"`
$strip --strip-all $binary

#buildsize=`du -h -s $destdir | awk '{print $1}'`

path="`echo "$PATH" | sed -e "s/\:/ /g"`"
mkfsjffs2=""
for i in $path; do
  if [ -x "$i/mkfs.jffs2" ]; then
    mkfsjffs2="$i/mkfs.jffs2"
    break
  fi
done
 
if [ -z "$mkfsjffs2" ]; then
    echo "$0: WARNING: no mkjfs.jffs2 found in path. Falling back to using du"
    echo "for size calculation. mkfs.jffs2 is recommended for size calculation"
    echo "as it calculates the real package size on the compressed file system,"
    echo "in contrast to du calculating the uncompressed size!"
    buildsize=`du -h -s $destdir | awk '{print $1}'`
else
    buildsize=`$mkfsjffs2 -r $destdir | wc -c`
fi

echo "*** package size: $buildsize"

buildversion=`cat ../VERSION`

ctrldir=$destdir/CONTROL

mkdir -p $ctrldir

cat > subst << EOT
s,@konq_ipkg_version@,$buildversion,g
s,@konq_ipkg_size@,$buildsize,g
s,@konq_ipkg_arch@,arm,g
EOT

sed -f subst < $control > $ctrldir/control

rm -f subst

echo "*** building package (ipkg-build)"
ipkg-build $destdir

