#!/bin/bash

# $Id$

#----------------------------# Hardcoded URLs and MD5.
add_blfs_deps_urls() {       # No easy way to extract it.
#----------------------------# Some FTP mirrors may not work

  local BLFS_SERVER="${SERVER}/pub/blfs/conglomeration/"

  if [[ "${DEP_LIBXML}" = "y" ]] ; then
    echo "${LIBXML_URL} ${BLFS_SERVER}libxml2/${LIBXML_PKG} ${LIBXML_MD5}" >> urls.lst
  fi

  if [[ "${DEP_LIBXSLT}" = "y" ]] ; then
    echo "${LIBXSLT_URL} ${BLFS_SERVER}libxslt/${LIBXSLT_PKG} ${LIBXSLT_MD5}" >> urls.lst
  fi

  if [[ "${DEP_TIDY}" = "y" ]] ; then
    echo "${TIDY_URL} ${BLFS_SERVER}tidy/${TIDY_PKG} ${TIDY_MD5}" >> urls.lst
    echo "${TIDY_PATCH_URL} ${BLFS_SERVER}tidy/${TIDY_PATCH} ${TIDY_PATCH_MD5}" >> urls.lst
    echo "${TIDY_DOCS_URL} ${BLFS_SERVER}tidy/${TIDY_DOCS} ${TIDY_DOCS_MD5}" >> urls.lst
  fi

  if [[ "${DEP_UNZIP}" = "y" ]] ; then
    echo "${UNZIP_URL} ${BLFS_SERVER}unzip/${UNZIP_PKG} ${UNZIP_MD5}" >> urls.lst
  fi

  if [[ "${DEP_DBXML}" = "y" ]] ; then
    echo "${DBXML_URL} ${BLFS_SERVER}docbook-xml/${DBXML_PKG} ${DBXML_MD5}" >> urls.lst
  fi

  if [[ "${DEP_DBXSL}" = "y" ]] ; then
    echo "${DBXSL_URL} ${BLFS_SERVER}docbook-xsl/${DBXSL_PKG} ${DBXSL_MD5}" >> urls.lst
  fi

  if [[ "${DEP_LINKS}" = "y" ]] ; then
    echo "${LINKS_URL} ${BLFS_SERVER}links/${LINKS_PKG} ${LINKS_MD5}" >> urls.lst
  fi

  if [[ "${DEP_SUDO}" = "y" ]] ; then
    echo "${SUDO_URL} ${BLFS_SERVER}sudo/${SUDO_PKG} ${SUDO_MD5}" >> urls.lst
    echo "${SUDO_PATCH_URL} ${BLFS_SERVER}sudo/${SUDO_PATCH} ${SUDO_PATCH_MD5}" >> urls.lst
  fi

  if [[ "${DEP_WGET}" = "y" ]] ; then
    echo "${WGET_URL} ${BLFS_SERVER}wget/${WGET_PKG} ${WGET_MD5}" >> urls.lst
  fi

  if [[ "${DEP_SVN}" = "y" ]] ; then
    echo "${SVN_URL} ${BLFS_SERVER}subversion/${SVN_PKG} ${SVN_MD5}" >> urls.lst
  fi

  if [[ "${DEP_GPM}" = "y" ]] ; then
    echo "${GPM_URL} ${BLFS_SERVER}gpm/${GPM_PKG} ${GPM_MD5}" >> urls.lst
    echo "${GPM_PATCH_1_URL} ${BLFS_SERVER}gpm/${GPM_PATCH_1} ${GPM_PATCH_1_MD5}" >> urls.lst
    echo "${GPM_PATCH_2_URL} ${BLFS_SERVER}gpm/${GPM_PATCH_2} ${GPM_PATCH_2_MD5}" >> urls.lst
  fi

}

#----------------------------# Maybe there is a better way to do this, but this
copy_blfs_deps_scripts() {   # method avoid to place the test on all
#----------------------------# $PROGNAME/master.sh scripts.

  mkdir -p ${PROGNAME}-commands/blfs-tool-deps

  if [[ "${DEP_LIBXML}" = "y" ]] ; then
    mv blfs-tool-deps/901-libxml2 ${PROGNAME}-commands/blfs-tool-deps
  fi

  if [[ "${DEP_LIBXSLT}" = "y" ]] ; then
    mv blfs-tool-deps/902-libxslt ${PROGNAME}-commands/blfs-tool-deps
  fi

  if [[ "${DEP_TIDY}" = "y" ]] ; then
    mv blfs-tool-deps/903-tidy ${PROGNAME}-commands/blfs-tool-deps
  fi

  if [[ "${DEP_UNZIP}" = "y" ]] ; then
    mv blfs-tool-deps/904-unzip ${PROGNAME}-commands/blfs-tool-deps
  fi

  if [[ "${DEP_DBXML}" = "y" ]] ; then
    mv blfs-tool-deps/905-docbook-xml ${PROGNAME}-commands/blfs-tool-deps
  fi

  if [[ "${DEP_DBXSL}" = "y" ]] ; then
    mv blfs-tool-deps/906-docbook-xsl ${PROGNAME}-commands/blfs-tool-deps
  fi

  if [[ "${DEP_LINKS}" = "y" ]] ; then
    mv blfs-tool-deps/908-links ${PROGNAME}-commands/blfs-tool-deps
  fi

  if [[ "${DEP_SUDO}" = "y" ]] ; then
    mv blfs-tool-deps/909-sudo ${PROGNAME}-commands/blfs-tool-deps
  fi

  if [[ "${DEP_WGET}" = "y" ]] ; then
    mv blfs-tool-deps/910-wget ${PROGNAME}-commands/blfs-tool-deps
  fi

  if [[ "${DEP_SVN}" = "y" ]] ; then
    mv blfs-tool-deps/911-subversion ${PROGNAME}-commands/blfs-tool-deps
  fi

  if [[ "${DEP_GPM}" = "y" ]] ; then
    mv blfs-tool-deps/907-gpm ${PROGNAME}-commands/blfs-tool-deps
  fi

  rm -rf blfs-tool-deps

}


#----------------------------------#
wrt_blfs_tool_targets() {          #
#----------------------------------#

  echo "${tab_}${GREEN}Processing... ${L_arrow}BLFS_TOOL ${R_arrow}"

  for file in blfs-tool-deps/* ; do
    # Keep the script file name
    this_script=`basename $file`

    # Grab the name of the target
    name=`echo ${this_script} | sed -e 's@[0-9]\{3\}-@@'`

    # Find the package.
    case $name in
      tidy )  pkg_tarball=${TIDY_PKG} ;;
      unzip ) pkg_tarball=${UNZIP_PKG} ;;
      * )     pkg_tarball=$(get_package_tarball_name $name) ;;
    esac

    # Append each name of the script files to a list (this will become
    # the names of the targets in the Makefile)
    blfs_tool="$blfs_tool ${this_script}"

    #--------------------------------------------------------------------#
    #         >>>>>>>> START BUILDING A Makefile ENTRY <<<<<<<<          #
    #--------------------------------------------------------------------#
    #
    # Drop in the name of the target on a new line, and the previous target
    # as a dependency. Also call the echo_message function.
    CHROOT_wrt_target "${this_script}" "$PREV"

    # Insert instructions for unpacking the package and changing directories
    # DocBook-XML is a zip, the build script will handle that.
    [[ ! "$name" = "docbook-xml" ]] && CHROOT_Unpack "$pkg_tarball"

    # Run the script.
    CHROOT_wrt_RunAsRoot "$file"

    # Remove the build directory(ies) except if the package build fails.
    [[ ! "$name" = "docbook-xml" ]] && CHROOT_wrt_RemoveBuildDirs "$name"

    # Touch the tracking file.
    case $name in
      tidy )  pkg_ver=tidy-051026 ;;
      unzip ) pkg_ver=unzip-5.52 ;;
      * )     pkg_ver=$(echo $pkg_tarball | sed -e 's/.tar.*//;s/.tgz//;s/.zip//') ;;
    esac
    echo  "\$(tab_)@touch $TRACKING_DIR/$pkg_ver" >>  $MKFILE.tmp

    # Include a touch of the target name so make can check
    # if it's already been made.
    wrt_touch
    #
    #--------------------------------------------------------------------#
    #              >>>>>>>> END OF Makefile ENTRY <<<<<<<<               #
    #--------------------------------------------------------------------#

    # Keep the script file name for Makefile dependencies.
    PREV=${this_script}
  done
}