#!/bin/sh
#
#	Make Zaurus (etc?) .ipk files, from a simple
#	description.
#
#	Copyright Martin C. Atkins, 2003
#	Distributed under the terms of the GPL
#	see http://www.opensource.org/licenses/gpl-license.php
#
#Revision History:
#01/02/2003	Martin <martin@mca-ltd.com>
#		First version prepared for release
#

PROG=`basename $0`

get_one()
{
	lines="`grep \"$1\" $2`"
	if [ `echo $lines | wc -l` -ne 1 ]
	then
		echo "$PROG: more than one line matches $1" >&2
		exit 1
	fi
	set `echo $lines`
	if [ $# -ne 2 ]
	then
		echo "$PROG: bad line matching $1" >&2
		exit 1
	fi
	echo $2
}

#
# Change this to dirname, to run on the Zaurus, or anything
# lacking dirname(1)
#
mydirname()
{
	x="`echo $1 | sed 's:/[^/]*$::'`"
	if [ "$x" = "$1" ]
	then
		echo "."
	elif [ "$x" = "" ]
	then
		echo "/"
	else
		echo "$x"
	fi
}

case $# in
1)	;;
*)	echo "usage: $PROG control_file" >&2
	cat >&2 <<-\END
	Copyright Martin C. Atkins <martin@mca-ltd.com> (GPL licence terms)
	Version 0.1

	Create a Zaurus .ipk package file, as described by 'control_file'.
	Control_file contains lines of the form:
	  Control	<infile>	<name in package control section>
	or
	  File		<infile>	<path of file on target system>
	or
	  Link		<link target>	<path of link on target system>

	These lines can also have trailing comments, introduced by
	the '#' character.
	Other lines are ignored.

	There must be a Control line creating a file called 'control'.
	This file should be in the format of a package description file.
	The "Package", "Version", and "Architecture" fields are used
	to form the name of the resulting .ipk file.

	END
	exit 1
	;;
esac

CTRL=$1

BDIR="/tmp/mkipk.$$"
trap 'rm -rf $BDIR' 0 
mkdir $BDIR

echo "2.0" >$BDIR/debian-binary

mkdir $BDIR/control
grep '^Control[ 	]' $CTRL >$BDIR/tmp.control
GOT_CONTROL=N

while read l
do
	set `echo $l | sed 's/#.*$//'`
	if [ $# -ne 3 ]
	then
		echo "$PROG: line not of form: Control <from> <to> [# comment]" >&2
		exit 1
	fi
	cp $2 $BDIR/control/$3 || { echo "$PROG: can't copy $2" >&2; exit 1; }
	if [ "$3" = "control" ]
	then
		GOT_CONTROL=Y
	fi
done <$BDIR/tmp.control

if [ "$GOT_CONTROL" != "Y" ]
then
	echo "$PROG: there must be a control file" >&2
	exit 1
fi

tar cf $BDIR/control.tar -C $BDIR/control ./
gzip $BDIR/control.tar

mkdir $BDIR/data
grep '^File[ 	]' $CTRL >$BDIR/tmp.files

while read l
do
	set `echo $l | sed 's/#.*$//'`
	if [ $# -ne 3 ]
	then
		echo "$PROG: line not of form: File <from> <to> [# comment]" >&2
		exit 1
	fi
	DIR="`dirname $3`"
	if [ $DIR != '.' ]
	then
		mkdir -p $BDIR/data/$DIR
	fi
	cp $2 $BDIR/data/$3 || { echo "$PROG: can't copy $2" >&2; exit 1; }
done <$BDIR/tmp.files

grep '^Link[ 	]' $CTRL >$BDIR/tmp.links

while read l
do
	set `echo $l | sed 's/#.*$//'`
	if [ $# -ne 3 ]
	then
		echo "$PROG: line not of form: Link <from> <to> [# comment]" >&2
		exit 1
	fi
	DIR="`dirname $3`"
	if [ $DIR != '.' ]
	then
		mkdir -p $BDIR/data/$DIR
	fi
	ln -s $2 $BDIR/data/$3
done <$BDIR/tmp.links

tar cf $BDIR/data.tar -C $BDIR/data ./
gzip $BDIR/data.tar

PROJECT=`get_one "Package:[ 	]" $BDIR/control/control`
VERSION=`get_one "Version:[ 	]" $BDIR/control/control`
ARCH=`get_one "Architecture:[ 	]" $BDIR/control/control`

IPK="${PROJECT}_${VERSION}_${ARCH}.ipk"

tar cf $BDIR/$IPK -C $BDIR ./debian-binary ./data.tar.gz ./control.tar.gz
gzip <$BDIR/$IPK >$IPK

