#!/bin/sh

# 28/Dec/2004 reed

# todo: need to get +PRESERVE file to and add that attribute
# todo: PRESERVE should be recorded in +BUILD_INFO

# using /ftp/pub/NetBSD/packages/1.6/i386/All/ which appears to have updates
# using /ftp/pub/NetBSD/packages/2.0/i386/All/ doesn't have updates now
# later should use a directory that has updates such as 2004Q4

#PKG_DIR=/ftp/pub/NetBSD/packages/1.6/i386/All/
PKG_DIR=/ftp/pub/NetBSD/packages/2.0/i386/All/

PKG_SUFX=.tgz

#for f in `ls *${PKG_SUFX}` ; do
#for f in `ls gzip-base-1.2.4anb1.tgz zoo-2.10.1.tgz` ; do
#for f in `ls coreutils-5.0nb3.tgz` ; do

if [ -z "$1" ] ; then
  echo need a package file as the argument
  exit 1
fi

f=$1

if [ ! -e "${f}" ] ; then
  echo "pkg_admin lsbest of ${f}"
  f2=`pkg_admin lsbest ${PKG_DIR}/${f}\*`
  [ -z "{$f2}" ] || f=${f2}
fi

if [ ! -e "${f}" ] ; then
  echo File \"${f}\" does not exist. 1>&2
  exit 1
fi

f2=`basename ${f} | sed -e "s,${PKG_SUFX}$,,"`
f=${f2}
echo $f

### XXXX check if directory and files exist
### XXX and compare timestamps to know if to skip or not
  mkdir -p ${HOME}/pkgsrc-specs/${f}
  cd ${HOME}/pkgsrc-specs/${f}
  
  tar xzf ${PKG_DIR}/${f}${PKG_SUFX} +BUILD_INFO +COMMENT +CONTENTS +DESC +SIZE_ALL +SIZE_PKG

  # later maybe consider doing this in XML instead
  echo "-------" > ${f}.spec

  pkgname=`awk '{ if ($1 == "@name") print $2 }' < +CONTENTS`
  prefix=`awk '{ if ($1 == "@cwd" && $2 != ".") print $2 }' < +CONTENTS`

  echo Name: ${pkgname} >> ${f}.spec
  echo Prefix: ${prefix} >> ${f}.spec

  awk '{ if ($1 == "@blddep") { blddep=$2 ; } if ($1 == "@pkgdep") {
	 print "Requires:",$2,blddep ; } }' +CONTENTS >> ${f}.spec
  awk '{ if ($1 == "@pkgcfl") print "Conflicts:",$2 ; }' +CONTENTS >> ${f}.spec

  awk -F = '{ if ($1 == "REQUIRES") print "LibRequires:",$2 ; }' \
	+BUILD_INFO >> ${f}.spec
  awk -F = '{ if ($1 == "PROVIDES") print "LibProvides:",$2 ; }' \
	+BUILD_INFO >> ${f}.spec

  pkgpath=`awk -F = '{ if ($1 == "PKGPATH") print $2 }' < +BUILD_INFO`
  opsys=`awk -F = '{ if ($1 == "OPSYS") print $2 }' < +BUILD_INFO`
  os_version=`awk -F = '{ if ($1 == "OS_VERSION") print $2 }' < +BUILD_INFO`
  architecture=`awk -F = '{ if ($1 == "MACHINE_ARCH") print $2 }' < +BUILD_INFO`
  object_fmt=`awk -F = '{ if ($1 == "OBJECT_FMT") print $2 }' < +BUILD_INFO`
  license=`awk -F = '{ if ($1 == "LICENSE") print $2 }' < +BUILD_INFO`
#  opsys=`awk -F = '{ if ($1 == "RESTRICTED") print $2 }' < +BUILD_INFO`

  echo Pkgsrc: ${pkgpath} >> ${f}.spec

  # doesn't look like CATEGORIES is recorded
  # so use pkgpath for now
  echo -n "Category: " >> ${f}.spec
  echo $pkgpath | sed 's,^\(.*\)\/.*,\1,' >> ${f}.spec

  echo -n 'Comment: ' >> ${f}.spec
  cat +COMMENT >> ${f}.spec
  echo >> ${f}.spec

  echo -n "Size: " >> ${f}.spec
  cat +SIZE_PKG >> ${f}.spec
  #echo

  echo "Filename: ${f}" >> ${f}.spec

  echo -n "Filesize: " >> ${f}.spec
  # redo this to be more portable
  stat -L -f %z ${PKG_DIR}/${f}${PKG_SUFX} >> ${f}.spec

  # could use sha1, sha1sum (different format), openssl sha1, or digest sha1
  echo -n "Digest: " >> ${f}.spec
  sha1 ${PKG_DIR}/${f}${PKG_SUFX} | awk '{ print $1, $4 }' >> ${f}.spec

  echo "OpSys: ${opsys}" >> ${f}.spec
  echo "OsVersion: ${os_version}" >> ${f}.spec
  echo "Architecture: ${architecture}" >> ${f}.spec
  echo "Format: ${object_fmt}" >> ${f}.spec
  echo "License: ${license}" >> ${f}.spec

  # probably a better sed way to do this:
  # need to add a space after Homepage:
#  sed -n -e '/^Homepage:/,/./p' ~/tmp-pkg_info | tr -d '\n' && echo
  homepage=`sed -n -e '/^Homepage:/{n;p;}' +DESC`
  if [ -n "${homepage}" ] ; then
    echo "Homepage: ${homepage}" >> ${f}.spec
  fi

  # on line by itself, so formatting doesn't look bad
  echo "Description: " >> ${f}.spec
  sed -n -e '1,/^Homepage:/p' +DESC | sed -e '$d' -e '$d' >> ${f}.spec

#done