#---
## @Synopsis BUILD module for quill
#---

##
## @Globals QUILL_FETCH_MODE BUILDISON
function query_spell_build() {
  if [[ "$QUILL_FETCH_MODE" != "cpan" ]]; then
     if query "Will you be adding a custom BUILD file:" "n"
     then
       BUILDISON="BUILD, "
     fi
  fi
}

##
## @Globals QUILL_FETCH_MODE QUILL_TMP_DIR QUILL_SPELL_DIR SPELL_NAME EDITOR
function add_build() {
  if [[ "$QUILL_FETCH_MODE" == "cpan" ]]; then
    cp ${QUILL_TMP_DIR}/${SPELL_NAME}-BUILD ${QUILL_SPELL_DIR}/${SPELL_NAME}/BUILD
  else
    if query "Do you want the default_build function dumped into the BUILD file" "n"
    then
      dump_default_function build | sed -e 's:\( --[^hb]\): \\\n \1:g' -e "s:&& make:\&\&\nmake:" >> BUILD
    else
      echo "default_build &&" >> BUILD
    fi
  fi
  $EDITOR BUILD
}

#---
## @Synopsis ask and try to detect the build system by checking for marker
## @Synopsis files in the tarball
##
## @Globals QUILL_TMP_DIR SPELL_NAME SPELL_SRC_FILE SPELL_SRC_DIR
#---
function detect_build_system() {
  local bs SPELL SOURCE_CACHE

  # we override some sorcery variables to make uncompress_unpack work
  # uncompress so more file formats are supported than what tar -t can handle
  #   although currently hunt_src_url doesn't support anything else
  SPELL=$SPELL_NAME
  SOURCE_CACHE="$QUILL_TMP_DIR"
  uncompress_unpack "$SPELL_SRC_FILE"
  cd "$SPELL_SRC_DIR"

  #check for markers
  if [[ -e setup.py ]]; then
    bs=python
  elif [[ -e Rakefile ]]; then
    bs=rake
  elif [[ -e Makefile.PL ]]; then
    bs=PerL
  elif [[ -e Makefile.pl ]]; then
    bs=perl
  elif [[ -e .wscript ]]; then
    bs=waf
  elif [[ -e SConstruct ]]; then
    bs=scons
  elif [[ -e Makefile ]]; then
    if [[ -e configure ]]; then
      bs=make,configure
    elif [[ -e autogen.sh ]]; then
      bs=make,autogen,configure
    else
      bs=make
    fi
  elif [[ -e Jamfile ]]; then
    if [[ -e configure ]]; then
      bs=jam,configure
    elif [[ -e autogen.sh ]]; then
      bs=jam,autogen,configure
    else
      bs=jam
    fi
  else
    error_msg "Couldn't determine the build system, please file a bug."
    cd ..
    rm -r "$SPELL_SRC_FILE"
    return 1
  fi

  cd ..
  rm -r "$SPELL_SRC_FILE"

  message "Detected build system: $bs"
  echo $bs
}

#---
## @Synopsis creates a BUILD file according to the build system used
## @Synopsis unless it is make,configure (default_build)
##
## @Globals none
#---
function implement_build_system() {
  local bs=$1
  [[ $bs == make,configure ]] && return 0

  if grep -q autogen <<< $bs; then
    echo "cd blabla ./autogen.sh &&" >> PRE_BUILD
  fi
  if grep -q configure <<< $bs; then
    cat <<"FEO" >> BUILD
./configure --prefix=$INSTALL_ROOT/usr  \
            --sysconfdir=$INSTALL_ROOT/etc  \
            --localstatedir=$INSTALL_ROOT/var  \
            --mandir=$INSTALL_ROOT/usr/share/man   \
            --infodir=$INSTALL_ROOT/usr/share/info  \
            $OPTS &&
FEO
  fi

  case $bs in
    rake) echo "rake" >> BUILD ;;
    perl) echo "perl Makefile.pl" >> BUILD ;;
    PerL) echo "perl Makefile.PL" >> BUILD ;;
  python) echo "python setup.py" >> BUILD ;;
     waf) echo "waf_build" >> BUILD ;;
   scons) echo 'scons PREFIX=$INSTALL_ROOT/usr' >> BUILD ;;
     jam) echo "jam" >> BUILD ;;
    make) echo "make" >> BUILD ;;
  esac

}

#---
##
## This software is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This software is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this software; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##
#---
