#!/bin/bash SUDO= if [ $UID -ne 0 ]; then SUDO=sudo fi function fail { echo -e "\n\e[31m---> Failed on step $*\e[0m" >&2 exit 1 } function log { echo -en "\033]0;bootstrap-chroot - $*\a" echo -e "\e[92m---> $*\e[0m" } function builddir { if [ -z "${1}" ]; then echo "Missing argument 'dir'" return 1 fi if [ -d "${BUILD_DIR}/${1}" ] && [ $# -lt 2 ] || [ "${2}" != "--keep" ]; then log "Removing previous build directory for ${1}" rm -rf "${BUILD_DIR}/${1}" || $SUDO rm -r "${BUILD_DIR}/${1}" || fail removing previous build dir for "${1}" fi [ -d "${BUILD_DIR}/${1}" ] || mkdir -p "${BUILD_DIR}/${1}" cd "${BUILD_DIR}/${1}" log "Preparing to build ${1}" } function get_pkg_src { local PKG="${1^^}" local PKG_DIR="${1,,}" local VAR_PKG_VERSION="${PKG}_VERSION" local VAR_PKG_URL="${PKG}_URL" # Replace underscores with dashes in directory PKG_DIR="${PKG_DIR//_/-}" local CACHE_DIR="${SOURCE_DIR}/.cache" [ -d "${CACHE_DIR}" ] || mkdir -p "${CACHE_DIR}" || fail create source file caching directory if [ ! -d "${SOURCE_DIR}/${PKG_DIR}-${!VAR_PKG_VERSION}" ]; then log "Downloading ${1} source ..." SRC_FILE=$(curl --output-dir "${CACHE_DIR}" -C- -LOsw "%{filename_effective}" "${!VAR_PKG_URL}") || fail download "${1}" source file log "Extracting ${1} source ..." tar -xf "${SRC_FILE}" -C "${SOURCE_DIR}/" || fail extract "${1}" source log "Got source for ${1}" fi }