#!/bin/sh
#
# ipktools - tools to create, extract, package and install ipk files on pdaXrom
# version 0.2
#
# Copyright (C) 2006 hdluc@yahoo.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
######################################################################
VSTRING=`grep "\# version" $0|grep -v "VSTRING"|sed -e "s/#//g"`
APPNAME="ipktools"
APP=`basename $0`
NAME="$1"
OPTION=$2

# functions

unpackipk ()
{
  # trim the package name
  NAME=`basename $NAME`
  if [ -f $NAME ]; then
    FILENAME=$NAME
    NAME=`echo $NAME|cut -d _ -f0`
  else
    if [ "`/bin/ls $NAME*.ipk|wc -l|awk '{print $1}'`" != "1" ]; then
      echo "error: package name ambiguity" >&2
      exit 1
    else
      FILENAME=`/bin/ls $NAME*.ipk`
    fi
  fi
  # extract package
  echo "Extracting $NAME ($FILENAME)"
  if [ ! -d $NAME ]; then
    mkdir $NAME
    zcat $FILENAME|tar xvf - -C $NAME
    cd $NAME
    echo "... extracting control"
    mkdir control
    zcat control.tar.gz|tar -xvf - -C control
    rm control.tar.gz
    echo "... extracting data"
    mkdir data
    zcat data.tar.gz|tar xvf - -C data
    rm data.tar.gz
    echo "done."
  else
    echo "error: cannot extract to $NAME (already exist)" >&2
  fi
}

getfield()
{
  FIELD=$1
  VALUE=`grep "^$FIELD:" < ${NAME}/control/control | sed -e "s/^[^:]*:[[:space:]]*//"`
  if [ -z "$VALUE" ]; then
    echo "error: missing control field $FIELD" >&2
    exit 1
  fi
  echo $VALUE
  return 0
}

makeipk ()
{
  # make sure package exist
  NAME=`basename $NAME`
  if [ ! -x "$NAME" ]; then
    echo "error: package ${NAME} not found!" >&2
    exit 1
  fi 
  if [ ! -f ${NAME}/control/control ]; then
    echo "error: control file not found!" >&2
    exit 1
  fi
  if [ ! -d ${NAME}/data ]; then
    echo "error: no data found!" >&2
    exit 1
  fi
  # get package info
  PKG=`getfield Package`
  if [ "$PKG" != "$NAME" ]; then
    echo "error: package name does not match" >&2
    exit 1
  fi
  VERSION=`getfield Version | sed 's/.*://;'`
  ARCH=`getfield Architecture`
  if [ "$ARCH" != "arm" ] && [ "$ARCH" != "armv5tel" ] && [ "$ARCH" != "all" ]; then
    echo "error: invalid architecture" >&2
    exit 1 
  fi
  getfield Maintainer >/dev/null
  getfield Description >/dev/null
  SECTION=`getfield Section`
  if [ -z "$SECTION" ]; then
    echo "error: The Section field should have one of the following values:" >&2
    echo "Games, Multimedia, Communications, Settings, Utilies, Applications, Console, Misc" >&2
  fi
  PRIORITY=`getfield Priority`
  if [ -z "$PRIORITY" ]; then
    echo "error: The Priority field should have one of the following values:" >&2
    echo "required, important, standard, optional, extra." >&2
  fi

  # cleanup
  if [ -f ${NAME}_${VERSION}_${ARCH}.ipk ]; then 
    rm ${NAME}_${VERSION}_${ARCH}.ipk
  fi

  # build package
  echo "Building $NAME $VERSION"
  cd $NAME
  for script in control/preinst control/postinst control/prerm control/postrm; do
    if [ -f $script -a ! -x $script ]; then
      chmod 755 $script
    fi
    if [ -f $script ] && [ "`tail -1 $script|grep echo`" = "" ]; then
        echo "echo done" >> $script
    fi
  done
  cd control
  echo "... archiving control"
  tar cf - ./* | gzip - > ../control.tar.gz
  cd ..
  cd data
  echo "... archiving data"
  tar cf - ./* | gzip - > ../data.tar.gz
  cd ..
  echo "... creating ${NAME}_${VERSION}_${ARCH}.ipk"
  if [ ! -f debian-binary ]; then
    echo "2.0" > debian-binary
  fi
  tar cf - ./data.tar.gz ./control.tar.gz ./debian-binary | gzip - > ../${NAME}_${VERSION}_${ARCH}.ipk
  echo "... cleaning up"
  rm ./data.tar.gz
  rm ./control.tar.gz
  echo "built ${NAME}_${VERSION}_${ARCH}.ipk"
  cd ..
}

createcontrols ()
{
PKG=$1
cat > $PKG/control/control << EOF
Package: $PKG
Version: 0.0.1
Maintainer: yourname@domain.com
Architecture: armv5tel
Priority: optional
Section: Applications
Depends: 
Description: short description.
EOF
cat > $PKG/control/preinst << EOF
#!/bin/sh
### put pre install commands here ###
echo done.
EOF
cat > $PKG/control/postinst << EOF
#!/bin/sh
### put post install commands here ###
echo done.
EOF
cat > $PKG/control/prerm << EOF
#!/bin/sh
### put pre remove commands here ###
echo done.
EOF
cat > $PKG/control/postrm << EOF
#!/bin/sh
### put post remove commands here ###
echo done.
EOF
}

newipk ()
{
  # make sure package does not exist
  NAME=`basename $NAME`
  if [ -d "$NAME" ]; then
    echo "error: package ${NAME} already exists!" >&2
    exit 1
  fi 
  # create package skeleton
  mkdir -p ${NAME}/control
  createcontrols ${NAME}
  echo "2.0" > ${NAME}/debian-binary
  mkdir -p ${NAME}/data/usr/share/applications
  mkdir -p ${NAME}/data/usr/share/pixmaps
  mkdir -p ${NAME}/data/home/zaurus
  mkdir -p ${NAME}/data/home/root
  chown zaurus:qpe ${NAME}/data/home/zaurus 2>/dev/null
  echo "created package structure for ${NAME}_0.0.1_armv5tel.ipk"
}

zipipk ()
{
  if [ "$NAME" != "" ] && [ -f $NAME ]; then
    ZIPFILE=`basename $NAME|sed -e "s/.ipk/.zip/g"`
    echo "zipping up $NAME"
    zip -1 $ZIPFILE $NAME
    if [ "$?" = "0" ]; then
      /bin/rm $NAME
    fi
  fi
}

updatepkginfo ()
{
  IPKSTATUS=$1
  APPNAME=$2
  APPVERSION=$3
cat >> $IPKSTATUS << EOF
Package: $APPNAME
Status: install ok installed
Root: /
Version: $APPVERSION

EOF
}

# start here
isValid=FALSE
for i in makeipk unpackipk newipk zipipk
do
  if [ "$APP" = "$i" ]; then
    isValid=TRUE
  fi
done
if [ $isValid = "TRUE" ]; then
  echo "Hd IPK Tools$VSTRING - $APP for pdaXrom"
  if [ "$NAME" = "" ]; then
    echo "syntax: $APP [package]"
    exit 1
  fi
  if [ "`dirname $NAME`" != "." ]; then
    cd `dirname $NAME`
  fi
  if [ "$APP" = "zipipk" ]; then
    zipipk $NAME
    exit 0
  fi
  if [ "$APP" = "unpackipk" ]; then
    unpackipk $NAME
    exit 0
  fi
  if [ "$APP" = "makeipk" ]; then
    makeipk $NAME
    exit 0
  fi
  if [ "$APP" = "newipk" ]; then
    newipk $NAME
    exit 0
  fi
else
  echo "Hd IPK Tools$VSTRING"
  echo "please run correct program alias"
  exit 1
fi
