#!/bin/sh
# Package Rebuild
# @1996,97,98,99,2000 by Antonio Gallo
# Commands to rebuild a package present into the port dir
# directly from the source


# Include BPP options
. package.conf


# Configure a source
package_configure() {
  # Go to sourcedir
  cd $1
  # Check if configure is there
  if [ ! -x ./configure ]; then
    echoc YELLOW "Tarball seems not a GNU source (no configure)"
    return 1
  fi
  # If thereis a makefile you should make a full clean up
  if [ -f Makefile ]; then
    echoc PURPLE "Cleaning Up your source distribution (make distclean)"
    make distclean
  fi
  # Really clean up makefile ?
  if [ -f Makefile ]; then
    echoc YELLOW "Makefile is already there"
    return 1
  fi
  # Configure or override configure from SOURCEINFO
  if [ "$CONFIGURE" = "" ]; then
    echoc PURPLE "Configuring the source basically with configure"
    ./configure
  else
    echoc PURPLE "Configuring the source with $CONFIGURE"
    $CONFIGURE
  fi
  REPLY=$?
  if [ $REPLY -ne 0 ]; then
    echoc RED "Configure has returned error nr. $REPLY"
    exit 2
  fi
}


# Compile the source
package_compile() {
  # Go to sourcedir
  cd $1
  # If thereis a makefile you should make a full clean up
  if [ ! -f [mM]akefile ]; then
    echoc RED "Sorry, no Makefile"
    exit 3
  fi
  # do the job
  echoc PURPLE "Compiling the program"
  make
  REPLY=$?
  if [ $REPLY -ne 0 ]; then
    echoc RED "make has returned error nr. $REPLY"
    exit 3
  fi
  # Make Check
  echoc PURPLE "Try to check the program"
  make check
  REPLY=$?
  if [ $REPLY -ne 0 ]; then
    echoc RED "make check has returned the error nr. $REPLY"
    exit 3
  fi
}


# Install the source
package_install() {
  # Go to sourcedir
  cd $1
  # Make Uninstall
  echoc PURPLE "Uninstalling the last built"
  make uninstall
  REPLY=$?
  if [ $REPLY -ne 0 ]; then
    echo RED "Uninstall has returned errore nr. $REPLY"
    exit 4
  fi
  # Installing
  echoc PURPLE "Installing the program"
  make install
  REPLY=$?
  if [ $REPLY -ne 0 ]; then
    echoc RED "make install has returned error nr. $REPLY"
    exit 4
  fi
  # Ldconfig
  echoc PURPLE "Running ldconfig ..."
  ldconfig
}


# Remove the source after the installation
package_implode() {
  # Go to sourcedir
  cd $1
  # If thereis a makefile you should make a full clean up
  if [ -f Makefile ]; then
    echoc PURPLE "Cleaning Up the source distribution (make distclean)"
    make distclean
  fi
  # Really clean up makefile ?
  cd ~
  echoc PURPLE "Removing source directory $1"
  rm -vfR $1
}


# ----- Init -----
if [ "$1" = "" ]; then
  echoc CYAN	"Bad Penguin - Package Rebuild $PKG_VERSION"
  echoc RED	"Sorry, you did'nt specified a package name"
  exit 1
fi


# Banner
echoc CYAN "Rebuilding $1 from source"


# Warn if already instaled
if [ -d $PKG_SPOOL/$1 ]; then
  echoc YELLOW "Package $1 is already installed in your system"
fi
# Exit if no port
if [ ! -d $PKG_PORTS/$1 ]; then
  echoc RED "Package $1 is not available as port"
  exit 1
fi
# Exit if there is no SOURCEINFO
if [ ! -f $PKG_PORTS/$1/SOURCEINFO ]; then
  echoc RED "Package $1 cannot be built from source (no SOURCEINFO)"
  exit 1
else
  . $PKG_PORTS/$1/SOURCEINFO 
fi
# Exit if there is no SOURCEINFO
if [ ! -f $PKG_PORTS/$1/SOURCEFILES ]; then
  echoc RED "Package $1 cannot be built from source (no SOURCEFILES)"
  exit 1
fi
# Exit if there is no MANIFEST
if [ ! -f $PKG_PORTS/$1/MANIFEST ]; then
  echoc RED "Package $1 cannot be built from source (no MANIFEST)"
  exit 1
fi


# get the name of the tarball
SOURCEFILE="`cut -c 35- < $PKG_PORTS/$1/SOURCEFILES`"
TARBALL="$PKG_SOURCE/$SOURCEFILE"
echo "Package $1 require the tarball $SOURCEFILE"


# See if the tarball is inside the package file
if [ -f $PKG_SOURCE/$SOURCEFILE ]; then
  # Tarball is already there
  echo "Tarball $SOURCEFILE is in $PKG_SOURCE"
else
  # Get it via ftp
  echo "Tarball $SOURCEFILE not found, trying via FTP ..."
  if [ -f $PKG_PORTS/$1/SOURCEMIRRORS ]; then
    ( cat $PKG_PORTS/$1/SOURCEMIRRORS ; echo ) | while read FTPSITE FTPDIR
    do
      [ "$FTPSITE" = "" ] && continue
      echo "Trying to getting $SOURCEFILE from $FTPSITE under $FTPDIR"
      ncftp "$FTPSITE/$FTPDIR/$SOURCEFILE"
      REPLY=$?
      if [ $REPLY -eq 0 ]; then
        echoc GREEN "Tarball $SOURCEFILE has been downloaded."
        break
      else
        echoc YELLOW "Tarball $SOURCEFILE has not been downloaded ($REPLY)."
      fi
    done
    if [ ! -f $PKG_SOURCE/$SOURCEFILE ]; then
      echo "Tarball $SOURCEFILE was not found in internet"
      exit 1
    fi
  else
    echoc RED "Package $1 does not have a tarball"
    exit 1
  fi
fi


# Ok, we have the source but check the md5sum now
echo "Tarball $TARBALL checking MD5 checksum"
cd $PKG_SOURCE
md5sum --check $PKG_PORTS/$1/SOURCEFILES
if [ $? -ne 0 ]; then
  echoc RED "Tarball $SOURCEFILE has and invalid checksum"
  exit 1
fi
# Exploding the tarball
echo "Tarball $TARBALL is being exploded ... "
mkdir -p /usr/src
( cd /usr/src && tar zxf $TARBALL )
if [ $? -ne 0 ]; then
  echoc RED "Tarball $SOURCEFILE has not been exploded"
  exit 1
fi

  
# Descenging directory
if [ "$SOURCEDIR" = "" ]; then
  SOURCEDIR="/usr/src/`basename $SOURCEFILE .tar.gz`"
  echo "tring to guess SOURCEDIR with $SOURCEDIR"
fi
if [ ! -d $SOURCEDIR ]; then
  echoc RED "Directory $SOURCEDIR does not exists"
  exit 1
fi
echo "Entering into $SOURCEDIR"
cd $SOURCEDIR

  
# Configuring the tarball
echo "Tarball $TARBALL is being configured ... "
package_configure $SOURCEDIR
# Compiling
echo "Tarball $TARBALL is being compiled ... "  
package_compile $SOURCEDIR
# Installed
echo "Tarball $TARBALL is being installed ... "  
package_install $SOURCEDIR
# Cleanup source
echo "Tarball $TARBALL is being installed ... "  
package_implode $SOURCEDIR

# Done for now ...
echoc GREEN "Package $1 compiled ed installed successfully"

  
# Repack anything
exec package-repack $1
exit 1
