#!/bin/bash

# $Id$

#----------------------------#
get_book() {                 #
#----------------------------#
  cd $JHALFSDIR

  if [ -z $WORKING_COPY ] ; then
# Check for Subversion or git instead of just letting the script fail.
    case $PROGNAME in
      lfs | hlfs)
        test `type -p svn` || eval "echo \"This feature requires Subversion.\"
        exit 1"
      ;;
      clfs*)
        test `type -p git` || eval "echo \"This feature requires Git.\"
        exit 1"
      ;;
    esac

    echo -n "Downloading the $PROGNAME document, $LFSVRS version... "

    case $PROGNAME in
      lfs) svn_root="LFS" ;;
      hlfs) svn_root="HLFS" ;;
      clfs*) ;;
      *) echo "BOOK not defined in function <get_book>"
          exit 1 ;;
    esac
    # Grab a fresh book if it's missing, otherwise, update it from the
    # repo. If we've already extracted the commands, move on to getting the
    # sources.
    if [ $PROGNAME == "lfs" ] || [ $PROGNAME == "hlfs" ] &&
                                 [ ! -d ${PROGNAME}-${LFSVRS}/.svn ]; then
        svn co $SVN/${svn_root}/${TREE} ${PROGNAME}-$LFSVRS >>$LOGDIR/$LOG 2>&1
    elif [ $PROGNAME == "clfs" ]  || [ $PROGNAME == "clfs2" ] ||
         [ $PROGNAME == "clfs3" ] && [ ! -d ${PROGNAME}-${LFSVRS}/.git ]; then
        echo $TREE
        git clone $GIT ${PROGNAME}-$LFSVRS >>$LOGDIR/$LOG 2>&1
        if [ ! $TREE == "development" ]; then
           pushd ${PROGNAME}-$LFSVRS > /dev/null
           echo "Checking out $LFSVRS at $PWD in $TREE"
           git checkout ${TREE} >>$LOGDIR/$LOG 2>&1
           popd > /dev/null
        fi
    else
      cd ${PROGNAME}-$LFSVRS
      case $PROGNAME in
        clfs*)
          # If the repo is in "detached head" state, git pull fails, so get
          # back first to master:
          git checkout master >>$LOGDIR/$LOG 2>&1
          git pull >>$LOGDIR/$LOG 2>&1
              if [ ! $TREE == "development" ]; then
                 git checkout ${TREE} >>$LOGDIR/$LOG 2>&1
              fi
        ;;
        lfs | hlfs) svn up >>$LOGDIR/$LOG 2>&1 ;;
        *) ;;
      esac
    fi
    echo -ne "done\n"

  else # Working copy
    echo -ne "Using $BOOK as book's sources ...\n"
  fi
}

#----------------------------#
extract_commands() {         #
#----------------------------#

  cd $JHALFSDIR
  case $PROGNAME in
    clfs*)
      VERSION=$(xmllint --noent $BOOK/BOOK/prologue/$ARCH/bookinfo.xml 2>/dev/null | grep subtitle | sed -e 's/^.*ion //'  -e 's/<\/.*//') ;;
    lfs)
      if [ "$INITSYS" = "sysv" ] ; then
        VERSION=$(grep 'ENTITY version ' $BOOK/general.ent| cut -d\" -f2)
      else
        VERSION=$(grep 'ENTITY versiond' $BOOK/general.ent| cut -d\" -f2)
      fi
      ;;
    *)
      VERSION=$(xmllint --noent $BOOK/prologue/bookinfo.xml 2>/dev/null | grep subtitle | sed -e 's/^.*ion //'  -e 's/<\/.*//')  ;;
  esac

  # Clean
  rm -rf ${PROGNAME}-commands

  # Extract the commands
  echo -n "Extracting commands for"
  case ${PROGNAME} in
    clfs)
        echo -n " ${L_arrow}${BOLD}$ARCH${R_arrow} target architecture... "
        xsltproc --nonet                                   \
                 --xinclude                                \
                 --stringparam method "$METHOD"            \
                 --stringparam testsuite "$TEST"           \
                 --stringparam bomb-testsuite "$BOMB_TEST" \
                 --stringparam vim-lang "$VIMLANG"         \
                 --stringparam timezone "$TIMEZONE"        \
                 --stringparam page "$PAGE"                \
                 --stringparam lang "$LANG"                \
                 --stringparam sparc "$SPARC64_PROC"       \
                 --stringparam x86 "$TARGET"               \
                 --stringparam mips "$TARGET"              \
                 -o ./${PROGNAME}-commands/                \
                 $XSL                                      \
                 $BOOK/BOOK/$ARCH-index.xml >>$LOGDIR/$LOG 2>&1
      ;;

    clfs2)
        echo -n " ${L_arrow}${BOLD}$ARCH${R_arrow} target architecture... "
        xsltproc --nonet                            \
                 --xinclude                         \
                 --stringparam vim-lang "$VIMLANG"  \
                 --stringparam timezone "$TIMEZONE" \
                 --stringparam page "$PAGE"         \
                 --stringparam lang "$LANG"         \
                 --output ./${PROGNAME}-commands/   \
                 $XSL                               \
                 $BOOK/BOOK/$ARCH-index.xml >>$LOGDIR/$LOG 2>&1
      ;;

    clfs3)
        echo -n " ${L_arrow}${BOLD}$ARCH${R_arrow} target architecture... "
        xsltproc --nonet                            \
                 --xinclude                         \
                 --stringparam endian x$ENDIAN      \
                 --stringparam timezone "$TIMEZONE" \
                 --stringparam page "$PAGE"         \
                 --stringparam lang "$LANG"         \
                 --output ./${PROGNAME}-commands/   \
                 $XSL                               \
                 $BOOK/BOOK/$ARCH-index.xml >>$LOGDIR/$LOG 2>&1
      ;;

    hlfs)
        echo -n " ${L_arrow}${BOLD}$MODEL + $KERNEL${R_arrow} HLFS flavour... "
        xsltproc --nonet                                                \
                 --xinclude                                             \
                 --stringparam model "$MODEL"                           \
                 --stringparam kernel "$KERNEL"                         \
                 --stringparam testsuite "$TEST"                        \
                 --stringparam bomb-testsuite "$BOMB_TEST"              \
                 --stringparam features                                 \
                     x$SSP$ASLR$PAX$HARDENED_TMP$WARNINGS$MISC$BLOWFISH \
                 --stringparam timezone "$TIMEZONE"                     \
                 --stringparam page "$PAGE"                             \
                 --stringparam lang "$LANG"                             \
                 --stringparam grsecurity_host "$GRSECURITY_HOST"       \
                 --output ./${PROGNAME}-commands/                       \
                 $XSL                                                   \
                 $BOOK/index.xml >>$LOGDIR/$LOG 2>&1
      ;;
    lfs)
        echo -n " ${L_arrow}${BOLD}LFS${R_arrow} build... "
        pushd $BOOK > /dev/null
        if [ -f process-scripts.sh ]; then
          bash process-scripts.sh >> $LOGDIR/$LOG 2>&1
        fi
        popd > /dev/null

        xsltproc --nonet                                      \
                 --xinclude                                   \
                 --stringparam revision       "$INITSYS"      \
                 --stringparam testsuite      "$TEST"         \
                 --stringparam bomb-testsuite "$BOMB_TEST"    \
                 --stringparam vim-lang       "$VIMLANG"      \
                 --stringparam strip          "$STRIP"        \
                 --stringparam del-la-files   "$DEL_LA_FILES" \
                 --stringparam full-locale    "$FULL_LOCALE"  \
                 --stringparam timezone       "$TIMEZONE"     \
                 --stringparam page           "$PAGE"         \
                 --stringparam lang           "$LANG"         \
                 --stringparam pkgmngt        "$PKGMNGT"      \
                 --stringparam wrap-install   "$WRAP_INSTALL" \
                 --stringparam hostname       "$HOSTNAME"     \
                 --stringparam interface      "$INTERFACE"    \
                 --stringparam ip             "$IP_ADDR"      \
                 --stringparam gateway        "$GATEWAY"      \
                 --stringparam prefix         "$PREFIX"       \
                 --stringparam broadcast      "$BROADCAST"    \
                 --stringparam domain         "$DOMAIN"       \
                 --stringparam nameserver1    "$DNS1"         \
                 --stringparam nameserver2    "$DNS2"         \
                 --output ./${PROGNAME}-commands/             \
                 $XSL                                         \
                 $BOOK/index.xml >> $LOGDIR/$LOG 2>&1
      ;;
    *)  echo -n " ${L_arrow}${PROGNAME}${R_arrow} book invalid, terminate build... "
        exit 1 ;;
  esac

  echo "done"

  # Make the scripts executable.
  chmod -R +x $JHALFSDIR/${PROGNAME}-commands

  # Create the packages file. We need it for proper Makefile creation
  create_package_list
  create_chroot_scripts

  # Done. Moving on...
  get_sources

}

#----------------------------#
create_package_list() {      #
#----------------------------#

  # Create the packages file. We need it for proper Makefile creation
  rm -f pkg_tarball_list
  echo -n "Creating <${PROGNAME}> list of tarball names for $BOOK"
  if [ ! -z $ARCH ] ; then echo -n " $ARCH" ; fi
  echo -n "... "
  case ${PROGNAME} in
    clfs*)
        xsltproc --nonet --xinclude  \
                 -o pkg_tarball_list \
                 packages.xsl        \
                 $BOOK/BOOK/materials/${ARCH}-chapter.xml >>$LOGDIR/$LOG 2>&1
      ;;
    hlfs)
        xsltproc --nonet --xinclude             \
                 --stringparam model "$MODEL"   \
                 --stringparam kernel "$KERNEL" \
                 --output pkg_tarball_list      \
                 packages.xsl                   \
                 $BOOK/chapter04/chapter04.xml >>$LOGDIR/$LOG 2>&1
      ;;
    lfs)
        xsltproc --nonet --xinclude                \
                 --stringparam pkgmngt "$PKGMNGT"  \
                 --stringparam revision "$INITSYS" \
                 --output pkg_tarball_list         \
                 packages.xsl                      \
                 $BOOK/chapter03/chapter03.xml >>$LOGDIR/$LOG 2>&1
      ;;
    *)
  esac

  echo "done"

}

#----------------------------#
create_chroot_scripts() {      #
#----------------------------#

  rm -rf chroot_scripts
  echo -n "Creating chroot commands scripts from $BOOK"
  if [ ! -z $ARCH ] ; then echo -n " $ARCH" ; fi
  echo -n "... "
  case ${PROGNAME} in
    clfs*)
      xsltproc --nonet --xinclude \
               -o chroot-scripts/ chroot.xsl \
               $BOOK/BOOK/${ARCH}-index.xml >> $LOGDIR/$LOG 2>&1
      ;;
    hlfs)
      xsltproc --nonet --xinclude \
               -o chroot-scripts/ chroot.xsl \
               $BOOK/index.xml >> $LOGDIR/$LOG 2>&1
      ;;
    lfs)
      xsltproc --nonet --xinclude \
               -o chroot-scripts/ chroot.xsl \
               $BOOK/chapter06/chapter06.xml >> $LOGDIR/$LOG 2>&1
      ;;
    *)
  esac
  echo "done"

}