#!/bin/bash -e

CONFFILE=/etc/pkgbinarymangler/sanitychecks.conf

. /usr/share/pkgbinarymangler/common

while [ $# -gt 0 ]; do
  if [ -d "$1" -a -f "$1"/DEBIAN/control ]; then
    DIR=$1
    break
  else
    shift
  fi
done

if [ -z "$DIR" ]; then
  echo "pkgsanitychecks: Error: Unable to locate DEBIAN/control" >&2
  exit 0
fi

if grep -q '^Section: debian-installer$' "$DIR/DEBIAN/control"; then
  echo "INFO: Disabling pkgsanitychecks for udeb" >&2
  exit 0
fi

# locations of python modules for 2.6 and newer python versions
PKG=$(basename $DIR)
case $PKG in
  python2.6|python2.7|python3.0|python3.1) : ;;
  *)
  for pv in 2.6 2.7 3.0 3.1; do
    if [ -d $DIR/usr/lib/python$pv/site-packages ]; then
      echo "Found files in /usr/lib/python$pv/site-packages (must be in dist-packages for python$pv)." >&2
      find $DIR/usr/lib/python$pv/site-packages >&2
      pysite_found=y
    fi
    if [ -d $DIR/usr/local/lib/python$pv ]; then
      echo "Found files in /usr/local/lib/python$pv (must be in /usr/lib for python$pv)." >&2
      find $DIR/usr/local/lib/python$pv >&2
      pysite_found=y
    fi
  done
  if [ -n "$pysite_found" ]; then
      break_build=y
  fi
esac

# check for files in /usr/local
if [ -d $DIR/usr/local ]; then
      echo "Found files in /usr/local (must be in /usr)." >&2
      find $DIR/usr/local >&2
      break_build=y
    fi
# more sanity checks ...

# break the build if necessary
if [ -n "$break_build" ]; then
  exit 1
fi

exit 0
