#!/bin/sh
# Install all required packages
#
# XXX: Welcome to port hell, if /usr/ports has been updated, you actually want
# to delete all installed packages and start over again, hence why people used
# to run this stuff in jails, etc.
#
# Rick van der Zwet <rick@wirelessleiden.nl>
#

. `dirname $0`/package-build.inc.sh

DEBUG=${DEBUG:-'0'}
DEP_CHECK_ONLY=${DEP_CHECK_ONLY:-'0'}

# make options, for package building
LOGDIR=`mktemp -d /tmp/$(basename $0 .sh)-$(date '+%Y%m%d-%H:%M:%S')X`

p_info Log directory ${LOGDIR}
p_info Make options are:
p_info `p_list ${PKG_MAKE_ARGS}`

# HACK: install our own ports _inside_ the normal ports dir
cp -fR $WL_PORTSDIR/* $PORTSDIR || exit 1

# Pre-req dependency fixing
p_info Checking whether there are currently unmet dependencies
RETVAL=0
PKGS=`pkg_info | awk '{print $1}'`
for PKG in ${PKGS} ; do
  PKG_DEP_FAIL=""
  for PKGDEP in `pkg_info -qr ${PKG} | awk '{print $2}'`; do
    pkg_info -e ${PKGDEP} 
    if [ $? -eq 1 ]; then
      PKG_DEP_FAIL="${PKG_DEP_FAIL} ${PKGDEP}"
    fi
  done
  if [ -n "${PKG_DEP_FAIL}" ]; then
      p_err Unmet dependencies found at $PKG, please fix manually:
      p_err `p_list ${PKG_DEP_FAIL}`
      RETVAL=1
  fi
done
if [ $RETVAL -eq 1 ]; then
  exit 1
fi
p_info Dependecy check ok

if [ ${DEP_CHECK_ONLY} -eq 1 ]; then
  exit 0
fi
echo ""


# Cleanup of old packages
if [ -d "${NANO_PACKAGE_DIR}" ]; then
	p_warn "Deleted all (old) packages at ${NANO_PACKAGE_DIR}"
	rm -R ${NANO_PACKAGE_DIR}/*
fi
p_info Created target dir ${NANO_PACKAGE_DIR}
mkdir -p ${NANO_PACKAGE_DIR}


# Build required packages
MAKE="make ${PKG_MAKE_ARGS}"
BUILD_LIST=''
set `echo ${PACKAGE_LIST} | xargs -n1 -I% echo $PORTSDIR/%`
while [ "$1" ]; do
  PACKAGE=$1; shift
  BUILD_LIST="${BUILD_LIST} ${PACKAGE}"

  # Build new one
  LOGFILE="$LOGDIR/build_`echo $PACKAGE | tr '/' '_'`.log"
  echo ""
  p_info Packaging $PACKAGE
  cd $PACKAGE || exit 1

  # Check for build dependencies
  BUILDDEP_PKG=`${MAKE} build-depends-list | awk -F: '{print $2}'`
  if [ -n "${BUILDDEP_PKG}" ]; then
	  p_info Build dependencies needed are:
          p_info `p_list ${BUILDDEP_PKG}`
  fi

  # Check for run dependencies
  PKGDEP_PKGS=`${MAKE} package-depends-list | awk '{print $2}'`
  if [ -n "${PKGDEP_PKGS}" ]; then
	  p_info Package dependencies also packaged are:
	  p_info `p_list ${PKGDEP_PKGS}`
  fi

  # Check if package is already installed
  VERSION=`${MAKE} clean generate-plist check-already-installed | awk '/is already/ {print $2}'`
   if [ -n "$FORCE_REBUILD" ]; then
     p_warn "${PACKAGE} forcefullly rebuilding..."
     MAKE_OPTION='deinstall reinstall'
   elif [ "${VERSION}" = "An" ]; then
     p_warn "${PACKAGE} outdated, please update"
     #XXX: Better way to detect whether a package needs updating
     # An older version of lang/python26 is already installed (python26-2.6.2)
     #XXX: Broken updatin, please fix
     #p_warn "re-run package-build.sh to check for unmet ones done using:"
     #p_warn "$0"
     #MAKE_OPTION='deinstall reinstall'
     MAKE_OPTION=''
   elif [ -n "${VERSION}" ]; then
     VERSION=`echo ${VERSION} | sed 's/^.*-//g'`
     p_info ${PACKAGE} already installed at ${VERSION}
     MAKE_OPTION=''
   else
     p_info ${PACKAGE} not installed, installing...
     MAKE_OPTION='install'
   fi

  if [ -n "${MAKE_OPTION}" ]; then
    p_info Buildlog: $LOGFILE
    ${MAKE} ${MAKE_OPTION} 2>>$LOGFILE 1>> $LOGFILE
    if [ $? -ne 0 ]; then
       p_err building ${PACKAGE} FAILED, logging at $LOGFILE
       exit 1
    fi
  fi
  ${MAKE} package-links
  PKGNAME=`${MAKE} extract-message | awk '{print $NF}'`
  pkg_create -b $PKGNAME ${NANO_PACKAGE_DIR}/${PKGNAME}.tbz || exit 1

  p_info Checking dependencies
  for PKG in $PKGDEP_PKGS; do
     echo "${BUILD_LIST}" | grep -q "$PKG"
     if [ $? -eq 0 ]; then
       # Already packaged
       p_info ${PKG} Already packaged
     else
       echo $@  | grep -q "${PKG}"
       if [ $? -eq 0 ]; then
          p_info ${PKG} Already planned
       else
          set $@ ${PKG}
       fi
     fi
  done
  p_info Packaging $PACKAGE succesfull
done
  
p_info `echo ${BUILD_LIST} | wc -w` packages build succesfully

if [ ${DEBUG} -eq 0 ]; then
  rm -R ${LOGDIR}
else
  p_info Debugging enabled ${LOGDIR} saved
fi

