#!/bin/sh

QTE_VERSION=2.3.4
DEB_VERSION=2.0

# Have to do this here, since CVS can't store symlinks
mkdir -p etc/rc2.d
rm -f etc/rc2.d/S98opie
ln -s ../init.d/opie etc/rc2.d/S98opie

mkdir -p etc/rcS.d
rm -f etc/rcS.d/S01bootsplash
ln -s ../init.d/bootsplash etc/rcS.d/S01bootsplash

rm -f ipaq/etc/rc2.d/S96opieatd
ln -s ../init.d/opieatd ipaq/etc/rc2.d/S96opieatd



VERSION_MAJ=$(sed -n -e 's/.*QPE_VERSION "\([0-9]*\)\..*\..*".*/\1/p' <$OPIEDIR/include/qpe/version.h)
VERSION_MIN=$(sed -n -e 's/.*QPE_VERSION ".*\.\([0-9]*\)\..*".*/\1/p' <$OPIEDIR/include/qpe/version.h)
VERSION_PAT=$(sed -n -e 's/.*QPE_VERSION ".*\..*\.\([0-9]*\).*/\1/p' <$OPIEDIR/include/qpe/version.h)
SUB_VERSION=$(sed -n -e 's,.*SUB_VERSION \"\(.*\)\".*,\1,p' <$OPIEDIR/include/qpe/version.h)
if grep -q 'QPE_VERSION .*snapshot' $OPIEDIR/include/qpe/version.h
then
    VERSION_CVS="$(date +%Y%m%d)"
    SUB_VERSION=$VERSION_CVS
else
    VERSION_CVS=""
fi

QPE_VERSION=$VERSION_MAJ.$VERSION_MIN.$VERSION_PAT

ARCH=arm
STRIP=arm-linux-strip
STRIP_FILES="*ARM*not stripped"

TDIR=/tmp/ipk$$
DATADIR=$TDIR
CTRLDIR=$TDIR/CONTROL

IMAGEDIR=
VERB=
LIST=
RPM=

while [ $# -ne 0 ]
do
    case "$1" in
     -v)
	VERB=1
	echo >&2 "Packaging for version $VERSION_MAJ.$VERSION_MIN.$VERSION_PAT$VERSION_CVS"
    ;; -l)
	LIST=1
    ;; -i)
	shift
	IMAGEDIR=$1
	DATADIR=$IMAGEDIR
	mkdir -p $IMAGEDIR
    ;; -classic)
        CLASSIC=1
    ;; -rpm)
	if [ "$OPIEDIR" != "/opt/Qtopia" ]
	then
	    echo >&2 '$OPIEDIR is not the standard /usr/Qtopia directory.'
	    sleep 1
	    #exit 1
	fi
	RPM=1
    ;; -arch)
	shift
	ARCH=$1
	STRIP=
	STRIP_FILES=
    ;; /*)
	FILES="$FILES $1"
    ;; *)
	FILES="$FILES $PWD/$1"
    esac
    shift
done

if [ -n "$CLASSIC" ]
then
    DATADIR=$TDIR/data
    CTRLDIR=$TDIR/control
fi

if [ -z "$FILES" ]
then
    FILES=`find $OPIEDIR -name "*.control" -print`
fi

RDIR=$PWD
cd $OPIEDIR

for i in $FILES
do
    rm -rf $TDIR

    mkdir -p $DATADIR
    mkdir -p $CTRLDIR

    packagename=${i##*/}; packagename=${packagename%.control}
    version=$(eval echo '"'$(sed -n -e "s/^Version: *//p" $i)'"')
    depends=$(eval echo '"'$(sed -n -e "s/^Depends: *//p" $i)'"')
    files=$(eval echo $(sed -n -e "s/^Files://p" $i))
    arch=$(eval echo $(sed -n -e "s/^Arch://p" $i))
    section=$(sed -n -e "s/^Section: *//p" $i)
    provides=$(sed -n -e "s/^Provides: *//p" $i)
    conflicts=$(sed -n -e "s/^Conflicts: *//p" $i)
    license=$(sed -n -e "s/^License: *//p" $i)
    summary=$(sed -n -e "s/^Description: *//p" $i)
    package=${packagename}_${version}_$ARCH
    ERROR=
    if [ -z "$files" ]
    then
	mkdir -p $DATADIR/usr/share/doc/$packagename
    else
	for f in $files
	do
	    if [ -d $f ]
	    then
		ffiles=$(find $f -type f -o -type b -o -type c -o -type l)
	    else
		ffiles=$f
	    fi
	    for ff in $ffiles
	    do
		case $ff in
		    */CVS/*)
		    continue
		;;	*~)
		    continue
		;;	*.control)
		    continue
		;; $QTDIR/*)
		    BASE=$(dirname /opt/QtPalmtop/${ff#$QTDIR/})
		;; etc/*.d/*)
		    BASE=$(dirname /$ff)
		;; ipaq/*)
		    BASE=$(dirname ${ff#ipaq})
		;; *)
		    # For SHARP ROM compatibility. Should change to Qtopia.
		    BASE=/opt/QtPalmtop/$(dirname $ff)
		esac

		if [ -f $ff -o -b $ff -o -c $ff ]
		then
		    D=$DATADIR$BASE
		    if [ -x $ff -a -n "$STRIP" ]
		    then
			case $(file $ff) in
			 $STRIP_FILES)
			    $STRIP $ff
			 ;; *)
			esac
		    fi
		    if [ -n "$RPM" ]
		    then
			case "$ff" in
			 /*) RPMFILES="$RPMFILES $ff"
			;; *) RPMFILES="$RPMFILES $OPIEDIR/$ff"
			esac
		    else
			mkdir -p $D
			if cp -a $ff $D
			then
			    true
			else
			    ERROR=1
			fi
		    fi
		else
		    echo >&2 "$0: $i: No such file: $ff"
		    ERROR=1
		fi
	    done
	done
    fi
    if [ -z "$ERROR" ]
    then
	if [ -n "$RPM" ]
	then
	    SPEC=/tmp/mkipks-rpm-$$.spec
	    echo >$SPEC "Summary: $summary"
	    echo >>$SPEC "Name: $packagename"
	    echo >>$SPEC "Group: $section" ##########
	    echo >>$SPEC "License: $license"
	    echo >>$SPEC "Version: ${version%-*}"
	    echo >>$SPEC "Release: ${version#*-}"
 
	    echo >>$SPEC "%description"
	    sed -n -e '/^Description:/,$ p' $i | tail +2 >>$SPEC
	    echo >>$SPEC "%files"
	    echo >>$SPEC "%defattr(-,root,root)"
	    /bin/ls $RPMFILES >>$SPEC
	    rpm -bb --target $ARCH-unknown-linux $SPEC
	    # rm $SPEC
	elif [ -z "$IMAGEDIR" ]
	then
	    #size=$(du -h -s $DATADIR | sed -e 's/[	 ].*//')
	    if (which mkfs.jffs2 >/dev/null 2>&1); then
		size=$(mkfs.jffs2 -r $DATADIR | wc -c)
	    fi
	    echo "Package: $packagename" >$CTRLDIR/control
	    if [ ! -z "$size" ]; then
	    echo "Installed-Size: $size" >>$CTRLDIR/control
	    else
		    if [ -z "$SIZEWARNED" ]
		    then
	    echo "Warning, no mkfs.jffs2 found, unable to set Installed-Size."
	    SIZEWARNED=1
		    fi
	    fi
	    echo "Architecture: $ARCH" >>$CTRLDIR/control 
	    echo "Filename: ./$package.ipk" >>$CTRLDIR/control
	    echo "Version: $version" >>$CTRLDIR/control
	    [ ! -z "$depends" ] && echo "Depends: $depends" >>$CTRLDIR/control
	    [ ! -z "$provides" ] && echo "Provides: $provides" >>$CTRLDIR/control
	    [ ! -z "$conflicts" ] && echo "Conflicts: $conflicts" >>$CTRLDIR/control
	    egrep -v "^(Files|Version|Depends|Provides|Conflicts|Architecture):" >>$CTRLDIR/control $i
	    [ -n "$CLASSIC" ] && echo "$DEB_VERSION" >$TDIR/debian-binary
	    base=${i%.control}
	    scripts="preinst postinst prerm postrm"
	    for pf in $scripts
	    do
		if [ -x ${base}.$pf ]
		then
		    cp ${base}.$pf $CTRLDIR/$pf
		fi
	    done
	    if [ -z "$CLASSIC" ]
	    then
		$OPIEDIR/scripts/ipkg-build -o root -g root $TDIR
	    else
	    	if [ "`id -u`" != "0" ]
		then
		    if [ -z "$ROOTWARNED" ]
		    then
		        echo Warning. You are not root. Please become root or use fakeroot.
		        ROOTWARNED=1
		    fi
		else
		    chown -R root.root $TDIR/*
		fi
		( cd $DATADIR && tar -czf $TDIR/data.tar.gz * )
		( cd $CTRLDIR && tar -czf $TDIR/control.tar.gz * )
		( cd $TDIR && tar -czf $OPIEDIR/$package.ipk ./control.tar.gz ./data.tar.gz ./debian-binary )
	    fi
	    RESULT=$package.ipk
	    if [ -n "$VERB" ]
	    then
		echo >&2 "Built $RESULT ($size)"
	    fi
	    if [ -n "$LIST" ]
	    then
		echo $RESULT
	    fi
	fi
    else
	echo >&2 "Not building $package package"
    fi
done
# clean up 
rm -rf $TDIR