#! /bin/sh
# Package Check - @2001 by Antonio Gallo
# agx@linux.it - www.badpenguin.org - www.linux.it/~agx
# Sun Jun 24 05:14:11 2001 by AGX

##### =====================      tools    =========================  #####
. /usr/local/libexec/package-library
MYVERSION="2.2.0"

##### =====================   fn_package_check         =========================  #####

fn_package_check() {
  #echoc CYAN "(*) check package's files"
  
  # Squeeze
  echo "> squeezing $PKGROOT ..."
  find  $PKGROOT \( -name \*~ -or -name \*\# \) -exec rm -f {} \;

  # Deprecated dirs
  if [ -e $PKGROOT/usr/doc ]; then
    if [ ! -L $PKGROOT/usr/doc ]; then
      echoc YELLOW "> moveing /usr/doc to /usr/share/doc"
      mkdir -p $PKGROOT/usr/share/
      mv -v $PKGROOT/usr/doc/ $PKGROOT/usr/share/doc/
    fi
  fi
  if [ -e $PKGROOT/usr/man ]; then
    if [ ! -L $PKGROOT/usr/man ]; then
      echoc YELLOW "> moveing /usr/man to /usr/share/man"
      mkdir -p $PKGROOT/usr/share/
      mv -v $PKGROOT/usr/man/ $PKGROOT/usr/share/man/
    fi
  fi
  if [ -e $PKGROOT/usr/info ]; then
    if [ ! -L $PKGROOT/usr/info ]; then
      echoc YELLOW "> moveing /usr/info to /usr/share/info"
      mkdir -p $PKGROOT/usr/share/    
      mv -v $PKGROOT/usr/info/ $PKGROOT/usr/share/info/
    fi
  fi
  
  # Check obsolete files  
  for i in PRE-DELETE DELETE POST-DELETE PRUNE CONFIGURE doinst.sh \
           PRE-INSTALL INSTALL POST-INSTALL X-POST-INSTALL PRE-REMOVE REMOVE \
           POST-REMOVE CLEANUP PURGE PRUNE 
  do
    if [ -f $PKGINST/$i ]; then
      echoc YELLOW "! file $PKGINST/$i is obsolete !"
      exit 1
    fi
  done

  # Check install scripts
  for i in PRE-INSTALL  INSTALL  POST-INSTALL X-POST-INSTALL \
	    PRE-REMOVE REMOVE POST-REMOVE CLEANUP
  do
    TEMPSCRIPT="$PKGSCRIPT/$i"
    if [ -f $TEMPSCRIPT ]; then
      #echo "- fixing $i permissions"
      chmod 700 $TEMPSCRIPT
      chown root.root $TEMPSCRIPT

      grep -q "^set -e" $TEMPSCRIPT
      if [ $? -ne 0 ]; then
        echoc YELLOW "! you are not using 'set -e' into this script."
        echo "  Press ENTER to continue"
        read x
      fi

      grep -q "^set -x" $TEMPSCRIPT
      if [ $? -eq 0 ]; then
        echoc YELLOW "! you are using 'set -x' into this script."
        echo "  Press ENTER to continue"
        read x
      fi
    fi
  done

  # Se c'e' incoming ci deve essere anche CLEANUP
  if [ -d $PKGROOT/incoming ]; then
    if [ ! -f $PKGSCRIPT/CLEANUP ]; then
      echoc YELLOW "! directoy 'incoming' is present, you should write a CLEANUP script !"
      echoc YELLOW "  Press ENTER to continue"
      read x
    fi
  fi

  # Icons
  #if [ -d $PKGROOT/usr/bin -o -d $PKGROOT/usr/local/bin ]; then
    #if [ ! -d $PKGROOT/usr/share/gnome/apps ]; then
     # echoc YELLOW "! There are programs but no icons under /usr/share/gnome/apps"
      #echoc YELLOW "  Press ENTER to continue"
      #read x
    #fi
  #fi

  # DOCDIR
  DOCDIR="$PKGROOT/usr/share/doc/"
  if [ -d $DOCDIR ]; then
    echoc GREEN "> changeing mode to docs ..."
    find $DOCDIR -type f -exec chmod 644 {} \;
    #rmdir $DOCDIR          2>/dev/null
  fi

  # INFO
  if [ -d "$PKGROOT/usr/share/info" ]; then
    #TODO echoc GREEN "- Adding texinfo to the deps"
    #TODO echo $DEPS_INFO >> $PKG_INSTALL/REQUEST
    echo "> compressing infoes ..."
    find "$PKGROOT/usr/share/info" -type f -exec gzip -9f {} 2>/dev/null \;
    echo "> changing mode to infoes ..."
    find "$PKGROOT/usr/share/info" -type f -exec chmod 644 {} \;
    # Scripts
    mkdir -m 700 -p $PKGSCRIPT
    touch $PKGSCRIPT/POST-INSTALL
    touch $PKGSCRIPT/PRE-REMOVE
    grep -q "install-info" $PKGSCRIPT/PRE-REMOVE
    if [ $? -ne 0 ]; then
      echoc YELLOW "! you are not handling info files in PRE-REMOVE."
      echo "  Press ENTER to continue"
      read x
    fi
    grep -q "install-info" $PKGSCRIPT/POST-INSTALL
    if [ $? -ne 0 ]; then
      echoc YELLOW "! you are not handling info files in POST-INSTALL."
      echo "  Press ENTER to continue"
      read x
    fi
  fi

  # MAN
  if [ -d "$PKGROOT/usr/share/man" ]; then
    #echoc GREEN "[MAN] Adding man to the deps"
    #echo $DEPS_MAN >> $PKG_INSTALL/REQUEST
    echo "> compressing manpages ..."
    find $PKGROOT/usr/share/man -type f -exec gzip -v9f {} 2>/dev/null \;
    echo "> changing mode to manpages ..."
    find $PKGROOT/usr/share/man -type f -exec chmod 644 {} \;
  fi

  # Stripping Binaries
  echo -n "> stripping  binaries:"
  for i in  "bin"  "sbin"  "usr/bin"  "usr/X11R6/bin"  "usr/sbin"  "usr/local/bin"  "usr/local/sbin" \
		"opt/bin" "opt/sbin"
  do
    if [ -d $PKGROOT/$i ]; then
      echo -n " $i"
      find "$PKGROOT/$i"  -type f -exec strip --strip-unneeded {} 2>/dev/null \;
    fi
    if [ -d $PKGROOT/incoming/$i ]; then
      echo -n "incoming/$i"
      find "$PKGROOT/incoming/$i" -type f -exec strip --strip-unneeded {} 2>/dev/null \;
    fi
  done
  echo "  done."

  # Stripping Libraries
  echo -n "> stripping libraries:"
  for i in  "lib"  "sbin"  "usr/lib"  "usr/X11R6/lib"  "usr/local/lib" "opt/lib" ; do
    if [ -d $PKGROOT/$i ]; then
      echo -n " $i"
      find "$PKGROOT/$i" -type f -iname "lib*so*"  -not -name "*.a" -exec strip --strip-unneeded {} \;
    fi
    if [ -d $PKGROOT/incoming/$i ]; then
      echo "incoming/$i"
      find "$PKGROOT/incoming/$i"  -type f  -iname "lib*so*"  -not -name "*.a" -exec strip --strip-unneeded {} \;
    fi
  done
  echo "  done."

  # Permissions
  install-permissions $PKGROOT 2>/dev/null
  if [ -d $PKGROOT/incoming ]; then
    install-permissions $PKGROOT/incoming 2>/dev/null
  fi
}


##### =====================   M  A  I  N   =========================  #####

# Banner
echo "======================================================================"
echoc CYAN  "Bad Penguin - Package Check $MYVERSION"
echo "======================================================================"

# Check Parameters
if [ "$1" = "" ]; then
  echoc RED "package-check: need at least 1 argument !"
  exit 1
fi

# Define package subdirs
fn_read_settings $1
fn_read_pkginfo
fn_check_pkgdir

# Repack first ?
if [ "$2" != "norepack" -a ! -f "$PKGCOMPILE/NOREPACK" ]; then
  if [ -f "$PKGINST/MANIFEST" ]; then
    set -e
    package-repack "$1" "$PKGINST/MANIFEST" "nobuild"
    set +e
    echo "======================================================================"
  else
    echoc RED "! package-check: cannot repack because MANIFEST is missing"
    exit 1
  fi
fi

fn_run_customize
fn_package_check
package-request "$1"
package-build   "$1"
# end of the story
echoc GREEN "- package-check ok"
exit 0
