Script "prepare-mx-chroot.sh"
#!/bin/sh

# ----------------------------
# prepare-mx-chroot.sh
# version 2.1.6.9
# ----------------------------

script_call_name=`basename ${0}`
usage_str=`echo "Usage:" ./"${script_call_name}" "--normal | --fresh | --without-bin-packages [ --copygits ]"`

# --------------------------------------------------------------------------------

NORMAL_OPT=""
FRESH_OPT=""
COPYGITS_OPT=""
WITHOUT_BIN_PACKAGES_OPT=""
for arg in $@
do
  [ $arg = "--normal" ] && NORMAL_OPT="yes"
  [ $arg = "--fresh" ] && FRESH_OPT="yes"
  [ $arg = "--copygits" ] && COPYGITS_OPT="yes"
  [ $arg = "--without-bin-packages" ] && WITHOUT_BIN_PACKAGES_OPT="yes"
  if [ $arg = "--help" ] || [ $arg = "--usage" ]; then
    echo "${usage_str}"
    exit 0
  fi
done

if [ -z ${NORMAL_OPT} ] && [ -z ${FRESH_OPT} ] && [ -z ${WITHOUT_BIN_PACKAGES_OPT} ]; then
  echo "${usage_str}"
  exit 0
fi
if [ ! -z ${FRESH_OPT} ] && [ ! -z ${NORMAL_OPT} ]; then
  echo "--normal and --fresh options are mutually exclusive!"
  echo "please, specify only one of them"
  exit 1
fi
if [ ! -z ${FRESH_OPT} ] && [ ! -z ${WITHOUT_BIN_PACKAGES_OPT} ]; then
  echo "--without-bin-packages and --fresh options are mutually exclusive!"
  echo "please, specify only one of them"
  exit 1
fi
if [ ! -z ${WITHOUT_BIN_PACKAGES_OPT} ] && [ ! -z ${NORMAL_OPT} ]; then
  echo "--normal and --without-bin-packages options are mutually exclusive!"
  echo "please, specify only one of them"
  exit 1
fi

# --------------------------------------------------------------------------------

[ -z "${USERNAME}" ] && USERNAME=`logname`
# sed 's/^[ \t]*//;s/[ \t]*$//' is used to delete both leading and trailing whitespace
USERHOME=`finger -l "${USERNAME}" 2>/dev/null | grep "Directory:" | cut -f1 | cut -d':' -f2 | sed 's/^[ \t]*//;s/[ \t]*$//'`
[ -z "${USERHOME}" ] && USERHOME="/home/`logname`"
[ -z "${CHROOT_DIR}" ] && CHROOT_DIR="${HOME}/mx-chroot"
[ -z "${BUILDFARM_DIR}" ] && BUILDFARM_DIR="${USERHOME}/build-farm"

if [ ! -z ${FRESH_OPT} ]; then
  if [ -d "${CHROOT_DIR}" ]; then
    proc_sys_dev_okay=""
    mkdir -p "${CHROOT_DIR}"/proc && rmdir "${CHROOT_DIR}"/proc 2>/dev/null && \
      mkdir -p "${CHROOT_DIR}"/sys && rmdir "${CHROOT_DIR}"/sys 2>/dev/null && \
      mkdir -p "${CHROOT_DIR}"/dev && rmdir "${CHROOT_DIR}"/dev 2>/dev/null && \
      proc_sys_dev_okay="yes"
    if [ -z ${proc_sys_dev_okay} ]; then
      echo "/proc, /sys and/or /dev are \"real\"; can't make totally fresh chroot"
      [ ! -z "${CHROOT_DIR}" ] && rm -rf "${CHROOT_DIR}"/bin
      [ ! -z "${CHROOT_DIR}" ] && rm -rf "${CHROOT_DIR}"/sbin
      [ ! -z "${CHROOT_DIR}" ] && rm -rf "${CHROOT_DIR}"/lib
      [ ! -z "${CHROOT_DIR}" ] && rm -rf "${CHROOT_DIR}"/usr
      [ ! -z "${CHROOT_DIR}" ] && rm -rf "${CHROOT_DIR}"/root
      [ ! -z "${CHROOT_DIR}" ] && rm -rf "${CHROOT_DIR}"/etc
      [ ! -z "${CHROOT_DIR}" ] && rm -rf "${CHROOT_DIR}"/var
      [ ! -z "${CHROOT_DIR}" ] && rm -rf "${CHROOT_DIR}"/tmp
    else
      echo "removing anything from previous mx-chroot!"
      rm -rf "${CHROOT_DIR}" && mkdir "${CHROOT_DIR}"
    fi
  else
    echo "directory '${CHROOT_DIR}' doesn't exist; creating it"
    mkdir -p "${CHROOT_DIR}"
  fi
else
  if [ ! -d "${CHROOT_DIR}" ]; then
    printf "%s '%s'\n" "creating chroot directory" "${CHROOT_DIR}"
    mkdir -p "${CHROOT_DIR}"
  fi
fi

if [ -z ${WITHOUT_BIN_PACKAGES_OPT} ]; then
  [ -z "${PACKAGES_DIR}" ] && PACKAGES_DIR="PACKAGES"
  if [ -d "${BUILDFARM_DIR}/${PACKAGES_DIR}" ]; then
    cp "${BUILDFARM_DIR}/${PACKAGES_DIR}"/binpkg/* "${CHROOT_DIR}"/
    cp "${BUILDFARM_DIR}/${PACKAGES_DIR}"/* "${CHROOT_DIR}"/ 2>/dev/null
  else
    echo "packages not found"
  fi
fi

if [ -d "${BUILDFARM_DIR}" ]; then
  rm -f "${CHROOT_DIR}"/*glibc.binpkg*
  rm -f "${CHROOT_DIR}"/*gnu.binpkg*
  rm -f "${CHROOT_DIR}"/*unknown.binpkg*
  cp "${BUILDFARM_DIR}"/SERVICE-SCRIPTS/*.sh "${CHROOT_DIR}"/ 1>/dev/null 2>&1
  rm -f "${CHROOT_DIR}"/prepare-mx-chroot.sh
  rm -f "${CHROOT_DIR}"/collect_new_packages.sh
else
  echo "build-farm ('${BUILDFARM_DIR}') doesn't exist"
fi

cd "${CHROOT_DIR}"
if [ -f unpack-them.sh ]; then
  ./unpack-them.sh 
  rm -f *bz2
fi

if [ -d "${BUILDFARM_DIR}"/BUILD-SCRIPTS ]; then
  CHROOT_BUILDFARM_DIR="${CHROOT_DIR}/root/build-farm"
  re_or_not_re=""
  if [ -d "${CHROOT_BUILDFARM_DIR}"/BUILD-SCRIPTS ]; then
    rm -rf "${CHROOT_BUILDFARM_DIR}"/BUILD-SCRIPTS
    re_or_not_re="re-"
  fi
  echo -n "${re_or_not_re}generating build farm..."
  mkdir -p "${CHROOT_BUILDFARM_DIR}"
  cp -r "${BUILDFARM_DIR}"/BUILD-SCRIPTS "${CHROOT_BUILDFARM_DIR}"/
  if [ $? -eq 0 ]; then echo " ok"; else echo " fail"; fi
  rm -rf "${CHROOT_BUILDFARM_DIR}"/BUILD-SCRIPTS/_BIGSYSTEM

  if [ ! -z ${COPYGITS_OPT} ]; then
    echo "copying local git repos:"
    cd "${BUILDFARM_DIR}"
    for curr_entry in *
    do
      if [ -d "${BUILDFARM_DIR}/${curr_entry}" ]; then
        if [ -z `echo "${curr_entry}" | grep -- "-build"` ] && 
              [ -z `echo "${curr_entry}" | grep -- "-disabled"` ]; then
          if [ ! -z `echo "${curr_entry}" | grep -- "-git"` ]
          then
            curr_git_repo_dir=`echo "${curr_entry}" | sed 's/"${BUILDFARM_DIR}"//'`
            printf "\t%s..." "${curr_git_repo_dir}"
            cp -dR --preserve=all ./"${curr_git_repo_dir}" "${CHROOT_BUILDFARM_DIR}"/
            if [ $? -eq 0 ]; then echo " ok"; else echo " fail"; fi
          fi
        fi
      fi
    done
  fi
else
  echo "build farm scripts ('${BUILDFARM_DIR}/BUILD-SCRIPTS') not found!"
fi

# self-update
if [ -d "${BUILDFARM_DIR}"/SERVICE-SCRIPTS ]; then
  script_name=`basename "${0}"`
  rm -f "${HOME}"/"${script_name}".NEW
  cp "${BUILDFARM_DIR}"/SERVICE-SCRIPTS/"${script_name}" "${HOME}"/"${script_name}".NEW
  diff -u "${HOME}"/"${script_name}" "${HOME}"/"${script_name}".NEW 1>/dev/null 2>&1 && \
    rm -f "${HOME}"/"${script_name}".NEW
  [ -f "${HOME}"/"${script_name}".NEW ] && printf "found new version of this script, see '%s'\n" "${script_name}".NEW
fi

current_date=`date`
echo "${current_date}" >"${CHROOT_DIR}"/.timestamp
echo "timestamp:" "${current_date}"

echo "-done-"
Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.