#!/bin/sh

# usage: $0 DIRNAME PHASE
#   PHASE is 1 (checksum) or 2 (package)

cleanup() {
  status=$1

  if [ -e ${dir}/.keep ]; then
    cd ${dir}
    objdir=$(make -V WRKDIR)
    tar cfjC /tmp/work.tbz ${objdir}/.. work
  fi

  cat /tmp/make.log${status}
  echo 1 > /tmp/status
  echo "================================================================"
  echo -n "build ended at "
  date

  exit 0
}

add_pkg() {
  pkgs=$*

  echo add_pkg $pkgs
  cd /tmp/depends
  export PKG_PATH=/tmp/depends
  if [ ! -z "${pkgs}" ]; then
    echo "adding dependencies"
    for i in $pkgs; do
      echo "pkg_add $i"
      base=$(basename $i .tgz)
      base=$(basename $base .tbz)
      if pkg_info -q -e $base; then
        echo "skipping $base, already added"
      else
        if ! pkg_add $i; then
          echo "error in dependency $i, exiting"
          echo "1" > /tmp/status
          exit 1
        fi
      fi
    done
  fi
}

del_pkg() {
  pkgs=$*

  cd /tmp/depends
  export PKG_PATH=/tmp/depends
  if [ ! -z "${pkgs}" ]; then
    unset delpkg
    for i in $pkgs; do
      base=$(basename $i .tgz)
      base=$(basename $base .tbz)
      if ! pkg_info -q -e $base; then
        echo "skipping $base, already deleted"
      else
	delpkg="${base} ${delpkg}"
      fi
    done
    echo "Deleting $delpkg"
    if ! (echo ${delpkg} | xargs pkg_delete -f); then
      echo "error in pkg_delete, exiting"
      echo "1" > /tmp/status
      exit 1
    fi
  fi
}

dir=$1
phase=$2

ED=$3
PD=$4
FD=$5
BD=$6
RD=$7

if [ $phase = 1 ]; then

  cd $dir || exit 1
  echo "maintained by: $(make maintainer)"
  echo "port directory: ${dir}"
  echo "build started at $(date)"

  echo "FETCH_DEPENDS=${FD}"
  echo "PATCH_DEPENDS=${PD}"
  echo "EXTRACT_DEPENDS=${ED}"
  echo "BUILD_DEPENDS=${BD}"
  echo "RUN_DEPENDS=${RD}"

  add_pkg $FD

  #Allow ports to notice they're being run on bento
  export PACKAGE_BUILDING=1

  cd $dir || exit 1
  pkgname=$(make package-name)
  echo "================================================================"
  echo "====================<phase 1: make checksum>===================="

  if /pnohang $TIMEOUT /tmp/make.log1 ${pkgname} make checksum; then
    cat /tmp/make.log1
    echo "0" > /tmp/status
  else
    cleanup 1
  fi

else

  cd $dir || exit 1
  pkgname=$(make package-name)

  echo "================================================================"
  echo "====================<phase 2: make extract>===================="
  add_pkg ${ED}
  cd $dir
  /pnohang $TIMEOUT /tmp/make.log2 ${pkgname} make extract || cleanup 2
  cat /tmp/make.log2
  del_pkg ${ED}

  echo "================================================================"
  echo "====================<phase 3: make patch>===================="
  add_pkg ${PD}
  cd $dir
  /pnohang $TIMEOUT /tmp/make.log3 ${pkgname} make patch || cleanup 3
  cat /tmp/make.log3
  del_pkg ${PD}

  echo "================================================================"
  echo "====================<phase 4: make build>===================="
  add_pkg ${BD}

  xvfb=0
  if which -s Xvfb; then
    xvfb=1
    pid=$(echo $$ % 32768 | bc)
    X11BASE=$(which Xvfb | sed -e 's./bin/Xvfb..')
    Xvfb :${pid} -fp ${X11BASE}/lib/X11/fonts/misc &
    DISPLAY=:${pid}
    export DISPLAY
  fi

  cd $dir
  /pnohang $TIMEOUT /tmp/make.log4 ${pkgname} make build || cleanup 4
  cat /tmp/make.log4

  echo "================================================================"
  echo "====================<phase 5: make package>===================="
  add_pkg ${RD}
  cat > /tmp/mtree.exclude <<EOF
./root/*
./var/*
./tmp/*
./etc/make.conf.bak
./etc/make.conf
./work/*
./compat/linux/proc
EOF
  mtree -X /tmp/mtree.exclude -xcn -k uid,gid,mode -p / > /tmp/mtree
  cd $dir
  if /pnohang $TIMEOUT /tmp/make.log5 ${pkgname} make package; then
    cat /tmp/make.log5
    echo "0" > /tmp/status
    prefix=$(make -V PREFIX)
    del_pkg ${pkgname}

    mtree -X /tmp/mtree.exclude -x -f /tmp/mtree -p / | egrep -v '^(usr/local/var|usr/local/www/|usr/X11R6/lib/X11/xserver/SecurityPolicy|usr/local/share/nls/POSIX|usr/local/share/nls/en_US.US-ASCII|etc/services|compat |usr/X11R6 |etc/manpath.config|etc/.*.bak|usr/local/info/dir|usr/X11R6/lib/X11/fonts/.*/fonts\.|usr/X11R6/(include|share)/pixmaps|usr/local/lib/xemacs|usr/(X11R6|local)/man/..( |/man. )|usr/X11R6/lib/X11/fonts/TrueType )' > /tmp/list3

    if [ -s /tmp/list3 ]; then
      cd /
      grep ' extra$' /tmp/list3 | awk '{print $1}' | xargs -J % find % -ls > /tmp/list4
      grep ' missing$' /tmp/list3 > /tmp/list5
      grep -vE ' (extra|missing)$' /tmp/list3 > /tmp/list6
      if [ "x${PLISTCHECK}" != "x" ]; then
	if grep -qE 'usr/(local|X11R6)/(bin/|include/|man/|sbin/|share/|share/|lib/|libdata/|libexec/)' /tmp/list4; then
          echo "1" > /tmp/status
	fi
	if [ -s /tmp/list5 ]; then
	  echo "1" > /tmp/status
	fi
      fi
      echo "================================================================"
    fi

    # BUILD_DEPENDS need to be present at install-time, e.g. gmake
    # Concatenate and remove duplicates
    BRD=$(echo $BD $RD | tr ' ' '\n' | sort -u | tr '\n' ' ')
    del_pkg ${BRD}
    cd /var/db/pkg
    if [ $(echo $(echo * | wc -c)) != 2 ]; then
      echo "leftover packages:" *
      del_pkg *
    fi

    echo
    echo "=== Checking filesystem state"

    if [ -s /tmp/list4 ]; then
      echo "list of extra files and directories in / (not present before this port was installed but present after it was deinstalled)"
      cat /tmp/list4
    fi
    if [ -s /tmp/list5 ]; then
      echo "list of files present before this port was installed but missing after it was deinstalled)"
      cat /tmp/list5
    fi
    if [ -s /tmp/list6 ]; then
      echo "list of filesystem changes from before and after port installation and deinstallation"
      cat /tmp/list6
      if [ "x${PLISTCHECK}" != "x" ]; then
        echo "1" > /tmp/status
      fi
    fi
  else
    cleanup 5
  fi

  if [ ${xvfb} = 1 ]; then
    kill $(jobid %1)
  fi

  if [ -e ${dir}/.keep ]; then
    cd ${dir}
    objdir=$(make -V WRKDIR)
    tar cfjC /tmp/work.tbz ${objdir}/.. work
  fi

fi

exit 0
