
fn_check_sourcedir() {
  if [ "$SOURCEDIR" = "" ]; then
    echoc YELLOW "- Variable SOURCEDIR in SOURCEINFO has not been set"
    SOURCEDIR="/usr/local/src/`basename $SOURCEFILE .tar.gz`"
    echoc YELLOW "  tring to use $SOURCEDIR ..."
  fi
  if [ ! -d $SOURCEDIR ]; then
    echoc RED "Directory $SOURCEDIR does not exists"
    exit 1
  fi
  echo "- Entering into $SOURCEDIR"
  cd $SOURCEDIR
}


fn_configure() {
  # PRECONFIG
  if [ "$PRECONFIG" != "" ]; then
    if [ -f $SOURCEDIR/[Mm]akefile ]; then
      echoc GREEN "- Cleaning Up your source: $PRECONFIG"
      cd $SOURCEDIR
      sh -x -c "$PRECONFIG"
      REPLY=$?
      if [ $REPLY -ne 0 ]; then
        echoc RED "PRECONFIG has returned error nr. $REPLY"
        exit $REPLY
      fi
    fi
  fi
  # CONFIG
  if [ "$CONFIGURE" != "" ]; then
    echoc GREEN "- Configuring source: $CONFIGURE"
    cd $SOURCEDIR
    sh -x -c "$CONFIGURE"
    REPLY=$?
    if [ $REPLY -ne 0 ]; then
      echoc RED "CONFIGURE has returned error nr. $REPLY"
      exit $REPLY
    fi
  else
    echoc YELLOW "- Nothing to configure"  
  fi
  # POSTCONFIG
  if [ "$POSTCONFIG" != "" ]; then
    echoc GREEN "- Preparing to compile: $POSTCONFIG"
    cd $SOURCEDIR 
    sh -x -c "$POSTCONFIG"
    REPLY=$?
    if [ $REPLY -ne 0 ]; then
      echoc RED "POSTCONFIG has returned error nr. $REPLY"
      exit $REPLY
    fi
  fi
}


fn_compile() {
  # Go to sourcedir
  cd $SOURCEDIR
  # If thereis a makefile you should make a full clean up
  if [ ! -f [mM]akefile ]; then
    echoc RED "Sorry, no Makefile in $SOURCEDIR"
    exit 3
  fi
  # Make
  if [ "$COMPILE" != "" ]; then
    echoc GREEN "- Compiling the program: $COMPILE"
    sh -x -c "$COMPILE"
    REPLY=$?
    if [ $REPLY -ne 0 ]; then
      echoc RED "compilation has returned error nr. $REPLY"
      exit 3
    fi
  else
    echoc YELLOW "- Nothing to compile"
  fi
  # Make test/check
  if [ "$MAKETEST" != "" ]; then
    echoc GREEN "- Testing the program: $MAKETEST"
    sh -x -c "$MAKETEST"
    REPLY=$?
    if [ $REPLY -ne 0 ]; then
      echoc RED "test has returned error nr. $REPLY"
      exit 3
    fi
  fi  
}


fn_install() {
  cd $SOURCEDIR
  # Make Uninstall
  if [ "$PREINSTALL" != "" ]; then
    echoc GREEN "- Pre-installing the program: $PREINSTALL"
    sh -x -c "$PREINSTALL"
    REPLY=$?
    if [ $REPLY -ne 0 ]; then
      echoc RED "pre-installation has returned error nr. $REPLY"
      exit 3
    fi
  fi
  # Make install
  if [ "$INSTALL" != "" ]; then
    echoc GREEN "- Installing the program: $INSTALL"
    sh -x -c "$INSTALL"
    REPLY=$?
    if [ $REPLY -ne 0 ]; then
      echoc RED "installation has returned error nr. $REPLY"
      exit 3
    fi
  else
    echoc YELLOW "- Nothing to install"
  fi
  # Ldconfig
  if [ "$POSTINSTALL" != "" ]; then
    echoc GREEN "- Post-installing the program: $POSTINSTALL"
    sh -x -c "$POSTINSTALL"
    REPLY=$?
    if [ $REPLY -ne 0 ]; then
      echoc RED "post-installation has returned error nr. $REPLY"
      exit 3
    fi
  fi
}


fn_remove_source() {
  if [ "$REMOVESOURCE" = "1" ]; then
    cd ~
    echoc GREEN "- Removing source directory $SOURCEDIR"
    rm -vfR $SOURCEDIR
  fi
}

# :-)  :-)  :-)  :-)  :-)  :-)  :-)  :-)  :-)  :-)  :-)  :-)  :-)  :-)  :-) 



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

PACKAGENAME="$1"
fn_sanity_check
fn_source_request
fn_source_files
fn_check_sourcedir
fn_configure
fn_compile
fn_install
fn_remove_source
echoc GREEN "Package $PACKAGENAME compiled and installed successfully"
# Repack anything
exec package-repack $PACKAGENAME
exit 1
