#!/bin/bash

# $Id$

set -e


no_empty_builddir() {
  'clear'
cat <<- -EOF-
${DD_BORDER}

${tab_}${tab_}${BOLD}${RED}W A R N I N G${OFF}
    Looks like the \$BUILDDIR directory contains subdirectories
    from a previous HLFS build.

    Please format the partition mounted on \$BUILDDIR or set
    a different build directory before running jhalfs.
${OFF}
${DD_BORDER}
-EOF-
  exit
}


#----------------------------#
run_make() {                 #
#----------------------------#
  # Test if make must be run.
  if [ "$RUNMAKE" = "y" ] ; then
    # Test to make sure we're not running the build as root
    if [ "$UID" = "0" ] ; then
      echo "You must not be logged in as root to build the system."
      exit 1
    fi
    # Build the system
    if [ -e $MKFILE ] ; then
      echo -ne "Building the system...\n"
      cd $JHALFSDIR && make
      echo -ne "done\n"
    fi
  fi
}


#----------------------------#
clean_builddir() {           #
#----------------------------#
  # Test if the clean must be done.
  if [ "${CLEAN}" = "y" ]; then
    # Test to make sure that the build directory was populated by jhalfs
    if [ ! -d $JHALFSDIR ] || [ ! -d $BUILDDIR/sources ] ; then
      echo "Looks like $BUILDDIR was not populated by a previous jhalfs run."
      exit 1
    else
      # Clean the build directory
      echo -n "Cleaning $BUILDDIR ..."
      sudo rm -rf $BUILDDIR/{bin,boot,dev,etc,home,lib,media,mnt,opt,proc,root,sbin,srv,sys,tmp,tools,cross-tools,usr,var}
      echo "done"
      echo -n "Cleaning $JHALFSDIR ..."
      sudo rm -rf $JHALFSDIR
      echo "done"
      echo -n "Cleaning remainig extracted sources in $BUILDDIR/sources ..."
      sudo rm -rf `find $BUILDDIR/sources/* -maxdepth 0 -type d`
      echo "done"
    fi
  fi
}

VERBOSITY2=$VERBOSITY

[[ $VERBOSITY2 > 0 ]] && echo ""

[[ $VERBOSITY2 > 0 ]] && echo -n "Loading <func_book_parser>..."
source $COMMON_DIR/libs/func_book_parser
[[ $? > 0 ]] && echo "file libs/func_book_parser did not load.." && exit 1
[[ $VERBOSITY2 > 0 ]] && echo "OK"


[[ $VERBOSITY2 > 0 ]] && echo -n "Loading <func_download_pkgs>..."
source $COMMON_DIR/libs/func_download_pkgs
[[ $? > 0 ]] && echo "file libs/func_download_pkgs did not load.." && exit 1
[[ $VERBOSITY2 > 0 ]] && echo "OK"


[[ $VERBOSITY2 > 0 ]] && echo -n "Loading <func_wrt_Makefile>..."
source $COMMON_DIR/libs/func_wrt_Makefile
[[ $? > 0 ]] && echo "file libs/func_wrt_Makefile did not load.." && exit 1
[[ $VERBOSITY2 > 0 ]] && echo "OK"


[[ $VERBOSITY2 > 0 ]] && echo -n "Loading <func_blfs_deps>..."
source $COMMON_DIR/libs/func_blfs_deps
[[ $? > 0 ]] && echo "file libs/func_blfs_deps did not load.." && exit 1
[[ $VERBOSITY2 > 0 ]] && echo "OK"

[[ $VERBOSITY2 > 0 ]] && echo -n "    ..."