#!/bin/bash # This file is 'sourced' by other scripts, therefore the above line is of no # use, except when modifying the file in emacs to have syntax highlighting. # Constants for return codes EXIT_SUCCESS=0 EXIT_WARNING=1 EXIT_FAILURE=2 # Position the cursor at the start of the line SET_CURSOR_START_LINE="\033[1G" # NORMAL prints text in normal color NORMAL="\033[0;39m" # SUCCESS prints text in a green colour SUCCESS="\033[1;32m" # WARNING prints text in a yellow colour WARNING="\033[1;33m" # FAILURE prints text in a red colour FAILURE="\033[1;31m" # Brackets are blue BRACKET="\033[1;34m" TAR_OPTS="-b8" HV_FONTS_PATH="/usr/share/fonts" DEFAULT_EDITOR=nano # It seems that when compiling bash-4.0, using # "make -j 1" is causing problems... if [ "x${MAKEJOBS}" = "x1" ]; then HVMAKE="make" else HVMAKE="make -j ${MAKEJOBS}" fi case "${HVL_TARGET}" in arm*) CLFS_BUILDFLAGS="-mabi=aapcs-linux -mfloat-abi=soft" CLFS_TARGET="arm-linux-gnueabi" CLFS_ARCH=$(echo ${CLFS_TARGET} | sed -e 's/-.*//' -e 's/arm.*/arm/g') CLFS_ENDIAN=$(echo ${CLFS_ARCH} | sed -e 's/armeb/BIG/' -e 's/arm/LITTLE/') if [ "${CLFS_ENDIAN}" = "LITTLE" ]; then CLFS_NOT_ENDIAN="BIG" else CLFS_NOT_ENDIAN="LITTLE" fi ;; "x86_64") CLFS_BUILDFLAGS="-m64" CLFS_TARGET="${HVL_TARGET}-unknown-linux-gnu" CLFS_ARCH=${HVL_TARGET} ;; "x86") # No special flags CLFS_BUILDFLAGS="" CLFS_TARGET="i686-unknown-linux-gnu" CLFS_ARCH=${HVL_TARGET} ;; *) echo "Unsupported target architecture: ${HVL_TARGET}" return ${EXIT_FAILURE} ;; esac CLFS_HOST="$(echo $MACHTYPE | \ sed "s/$(echo $MACHTYPE | cut -d- -f2)/cross/")" export CLFS_BUILDFLAGS CLFS_TARGET CLFS_ARCH CLFS_HOST CLFS_ENDIAN CLFS_NOT_ENDIAN CLFS=${LFS} # Extracting the version number from a complete package name. # Arg. #1: Complete package name with version (ex: firefox-3.5.5.source will output 3.5.5) get_pkg_ver() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi echo ${1} | sed "s!.*-\([0-9].*\)!\1!g" } # Extracting the base version number from a complete package name. # Arg. #1: Complete package name with version (ex: gcc-3.4.4 will output 3.4) get_pkg_ver_base() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi echo ${1} | sed "s!^.*-\([0-9]*\.[0-9]*\).*!\1!g" } # Extracting the base version number from a complete package name. # Arg. #1: Complete package name with version (ex: gcc-3.4.4 will output 3) get_pkg_ver_major() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi echo ${1} | sed "s!^.*-\([0-9]*\)\..*!\1!g" } # Extracting the name from a complete package name. # Arg. #1: Complete package name with version (ex: gcc-3.4.4 will output gcc) get_pkg_name() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi echo ${1} | sed "s!^\(.*\)-[0-9]*\.[0-9]*.*!\1!g" } # Saves the content of CFLAGS and CXXFLAGS environment variables. save_flags() { export OLDCFLAGS=${CFLAGS} export OLDCXXFLAGS=${CXXFLAGS} return ${EXIT_SUCCESS} } # Saves the content of CFLAGS and CXXFLAGS environment variables, and then # set them to empty strings. save_flags_no_optimizations() { export OLDCFLAGS=${CFLAGS} export OLDCXXFLAGS=${CXXFLAGS} export CFLAGS="" export CXXFLAGS="" return ${EXIT_SUCCESS} } # Restores the previous values of CFLAGS and CXXFLAGS environment variables. These # must have been saved first using save_flags(). restore_flags() { export CFLAGS=${OLDCFLAGS} export CXXFLAGS=${OLDCXXFLAGS} return ${EXIT_SUCCESS} } # Create log directory and log file for current stage if necessary # This should be done automatically... init_log_file() { mkdir -p ${LFS_LOG_DIR} && if [ ! -f ${LFS_LOG_FILE} ]; then touch ${LFS_LOG_FILE} || exit 1 fi } # Create symbolic links for start/stop scripts in /etc/rc.d # # Arg #1: script name # Arg #2: start number # Arg #3: stop number # Arg #4: level static_bootscript_add() { local SCRIPTNAME=${1} local START=${2} local STOP=${3} local RCLEVEL=${4} local START_LEVELS="" local STOP_LEVELS="" if [ "x${RCLEVEL}" = "xsysinit" ]; then START_LEVELS="sysinit" STOP_LEVELS="0 6" elif [ "x${RCLEVEL}" = "x3" ]; then START_LEVELS="3 4 5" STOP_LEVELS="0 1 2 6" else echo "${FUNCNAME}(), invalid level argument : ${*}" return 1 fi # Making sure bootscript has correct permissions chmod -v 740 ${LFS}/etc/rc.d/init.d/${SCRIPTNAME} && # Removing any old links for level in sysinit 0 1 2 3 4 5 6; do cd ${LFS}/etc/rc.d/rc${level}.d && rm -v -f $(find . -name "???${SCRIPTNAME}") || exit 1 done && if [ ${START} -ne 0 ]; then # Creating new start links for level in ${START_LEVELS}; do cd ${LFS}/etc/rc.d/rc${level}.d && ln -v -s ../init.d/${SCRIPTNAME} S${START}${SCRIPTNAME} || exit 1 done fi && if [ ${STOP} -ne 0 ]; then # Creating new stop links for level in ${STOP_LEVELS}; do cd ${LFS}/etc/rc.d/rc${level}.d && ln -v -s ../init.d/${SCRIPTNAME} K${STOP}${SCRIPTNAME} || exit 1 done fi } # Create symbolic links for start/stop scripts in /etc/rc.d # # Arg #1: level # Arg #2: script name # Arg #3: start number # Arg #4: stop number bootscript_add_manual() { local RCLEVEL=${1} local SCRIPTNAME=${2} local START=${3} local STOP=${4} # Making sure bootscript has correct permissions chmod 740 ${LFS}/etc/rc.d/init.d/${SCRIPTNAME} && # Removing any old links cd ${LFS}/etc/rc.d/rc${RCLEVEL}.d && rm -v -f $(find . -name "???${SCRIPTNAME}") && if [ ${START} -ne 0 ]; then # Creating new start link cd ${LFS}/etc/rc.d/rc${RCLEVEL}.d && ln -v -s ../init.d/${SCRIPTNAME} S${START}${SCRIPTNAME} || exit 1 fi && if [ ${STOP} -ne 0 ]; then # Creating new stop link cd ${LFS}/etc/rc.d/rc${RCLEVEL}.d && ln -v -s ../init.d/${SCRIPTNAME} K${STOP}${SCRIPTNAME} || exit 1 fi } # Create symbolic links for start/stop scripts in /etc/rc.d # # Arg #1: script name # Arg #2: start number # Arg #3: stop number bootscript_add_rc3() { static_bootscript_add ${*} 3 } # Create symbolic links for start/stop scripts in /etc/rc.d # # Arg #1: script name # Arg #2: start number # Arg #3: stop number bootscript_add_rcsysinit() { static_bootscript_add ${*} sysinit } # Add "export" before the variable name # # Arg #1: variable name # Arg #2: filename where the variable is located var_export() { VARIABLE=${1} FILE=${2} # Checking for correct number of arguments if [ $# -ne 2 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi # Checking if file exists if [ ! -f ${FILE} ]; then echo "${FUNCNAME}(), file not found: ${FILE}" return 1 fi # Checking if variable exists if ! grep "${VARIABLE}=" ${FILE} 1> /dev/null 2>&1; then echo "${FUNCNAME}(), variable not found: ${VARIABLE}" return 1 fi # Checking if variable is already exported when it is defined if grep "${VARIABLE}=" ${FILE} | grep "export " 1> /dev/null 2>&1; then echo "${FUNCNAME}(), variable already exported in definition: ${VARIABLE}" return 0 fi # Checking if variable is already exported, in # a "export VARIABLE1 VARIABLE2..." statement if grep "export " ${FILE} | grep " ${VARIABLE}" 1> /dev/null 2>&1; then echo "${FUNCNAME}(), variable already exported in export list: ${VARIABLE}" return 0 fi sed -i "s!\(^${VARIABLE}=.*\)!export \1!" ${FILE} } # Adding a new environment variable to a file. # # Arg #1: variable name # Arg #2: filename where the variable is located # Arg #3: new variable value # Arg #4: separator string (usually a " " or ":") var_add() { VARIABLE=${1} FILE=${2} VALUE="${3}" SEP="${4}" # Checking for correct number of arguments if [ $# -ne 4 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi # Checking if file exists if [ ! -f ${FILE} ]; then echo "${FUNCNAME}(), file not found: ${FILE}" return 1 fi # Checking if variable exists if ! grep "${VARIABLE}=" ${FILE} 1> /dev/null 2>&1; then echo "${VARIABLE}=\"${VALUE}\"" >> ${FILE} return $? fi # Checking if variable contains the new value if grep "${VARIABLE}=" ${FILE} | grep "${VALUE}" 1> /dev/null 2>&1; then echo "${FUNCNAME}(), variable ${VARIABLE} already contains value: ${VALUE}" return 0 fi # Adding new value to variable (case where no export before) # We search for the variable name starting at the beginning of the line # For example, this ensures that if the variable name is PATH, then # PATH=... matches but not MANPATH=... if grep "^${VARIABLE}=\"" ${FILE} 1> /dev/null 2>&1; then # Variable value is enclosed by double-quotes sed -i "s!\(^${VARIABLE}=\".*\)\(\"\)!\1${SEP}${VALUE}\"!" ${FILE} || exit 1 else # Variable value is NOT enclosed by double-quotes sed -i "s!\(^${VARIABLE}=.*\)!\1${SEP}${VALUE}!" ${FILE} || exit 1 fi # Adding new value to variable (case with export before) # We search for the variable name starting after an export statement, # at the beginning of a line. # For example, this ensures that if the variable name is PATH, then # PATH=... matches but not MANPATH=... if grep "^export ${VARIABLE}=\"" ${FILE} 1> /dev/null 2>&1; then # Variable value is enclosed by double-quotes sed -i "s!\(^export ${VARIABLE}=\".*\)\(\"\)!\1${SEP}${VALUE}\"!" ${FILE} else # Variable value is NOT enclosed by double-quotes sed -i "s!\(^export ${VARIABLE}=.*\)!\1${SEP}${VALUE}!" ${FILE} fi return $? } # Adding a new path environment variable to a file. # # Arg #1: variable name # Arg #2: filename where the variable is located # Arg #3: new variable value var_add_path() { var_add ${1} ${2} "${3}" ":" 1>> ${LFS_LOG_FILE} 2>&1 } # Adding a new string environment variable to a file. # # Arg #1: variable name # Arg #2: filename where the variable is located # Arg #3: new variable value var_add_str() { var_add ${1} ${2} "${3}" " " } # Adding a new string to a file. # # Arg #1: string # Arg #2: filename where the variable is located string_add() { STRING="${1}" FILE=${2} # Checking for correct number of arguments if [ $# -ne 2 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi # Checking if file exists if [ ! -f ${FILE} ]; then echo "${FUNCNAME}(), file not found: ${FILE}" return 1 fi # Checking if string exists if grep "${STRING}" ${FILE} 1> /dev/null 2>&1; then echo "${FUNCNAME}(), string already defined: ${STRING}" return 0 fi echo "${STRING}" >> ${FILE} } # Adding a new environment variable to a shadow password suite file (login.defs) # # Arg #1: variable name # Arg #2: filename where the variable is located # Arg #3: new variable value var_add_shadow() { VARIABLE=${1} FILE=${2} VALUE="${3}" # Checking for correct number of arguments if [ $# -ne 3 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi # Checking if file exists if [ ! -f ${FILE} ]; then echo "${FUNCNAME}(), file not found: ${FILE}" return 1 fi # Checking if variable contains the new value if egrep "^${VARIABLE}" ${FILE} | grep "${VALUE}" 1> /dev/null 2>&1; then echo "${FUNCNAME}(), variable ${VARIABLE} already contains value: ${VALUE}" return 0 fi # Adding new value to variable # We search for the variable name starting at the beginning of the line # For example, this ensures that if the variable name is PATH, then # PATH... matches but not MANPATH... sed -i "s!\(^${VARIABLE}.*\)!\1:${VALUE}!" ${FILE} } # Adding a new group, checking if it already exist before. # Arguments: same arguments as for standard groupadd command. hv_groupadd() { arguments=${*} set +e while [ $# -ne 0 ]; do last_argument=${*} shift done groupadd ${arguments} error=$? set -e if [ ${error} -eq 0 -o ${error} -eq 9 ]; then # 9 means the group already exists return ${EXIT_SUCCESS} else exit ${EXIT_FAILURE} fi } # Adding a new user, checking if it already exist before # Arguments: same arguments as for standard useradd command. hv_useradd() { arguments="${*}" set +e # The last argument is the username while [ $# -ne 0 ]; do last_argument=${*} shift done useradd ${arguments} error=$? set -e if [ ${error} -eq 0 -o ${error} -eq 9 ]; then # 9 means the user already exists return ${EXIT_SUCCESS} else exit ${EXIT_FAILURE} fi } # Evaluates the return value of the process that was run just before this # function was called. eval_retval() { if [ $? -ne 0 ]; then print_status failure exit ${EXIT_FAILURE} fi } # Obtain the name of the base directory for the decompressed package. # First argument: package name static_decompressed_dirname() { local PACKAGE=${1} # List of default archive extensions to try local MY_ARCH_EXT="tar.bz2 tar.gz tgz tar.Z zip" for arch_ext in ${MY_ARCH_EXT}; do if [ ! -f ${LFS_PKG_DIR}/${PACKAGE}.${arch_ext} ]; then # Try next archive extension. continue; fi case ${arch_ext} in tar.bz2|tar.gz|tgz|tar.Z) # Remove optional "./" leading component with sed # and extract base directory name with awk. # tar 1.23 reports an error when using pipes, so # remove error message with "2> /dev/null" local DIRNAME=$(tar ${TAR_OPTS} -tf ${LFS_PKG_DIR}/${PACKAGE}.tar.bz2 2> /dev/null | head -n1 | sed 's!^\./!!' | awk -F \/ '{print $1}') echo ${DIRNAME} ;; zip) # TODO echo ${PACKAGE} ;; esac return $? done # Failure or file not found echo "${FUNCNAME}(): Missing source package for \"${PACKAGE}\"" > /dev/stderr return ${EXIT_FAILURE} } # Applying patch # First argument is the name of the patch # Second argument is the package name (target dir in $LFS_TMP) apply_patch() { local PATCH_FILE=${1} local TARGET_DIR=${2} if [ -z "${PATCH_FILE}" ]; then echo echo "apply_patch(): no patch specified." return ${EXIT_FAILURE} fi if [ ! -f ${LFS_PKG_DIR}/${PATCH_FILE} ]; then echo echo "${FUNCNAME}(): patch file '${PATCH_FILE}' not found." return ${EXIT_FAILURE} fi echo "Applying patch: ${PATCH_FILE}" patch -Np1 -d ${LFS_TMP}/${TARGET_DIR} -i ${LFS_PKG_DIR}/${PATCH_FILE} } # Applying any patch(es) found for the current package. # Will work only if patches have the same base name as # the package, followed by a dash: # ${1}-*.patch # # 1st argument: Package name # 2: optional target directory. apply_patches() { PACKAGE=${1} local TARGET_DIR=${PACKAGE} # Checking for correct number of arguments if [ $# -gt 2 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" echo "Usage: ${FUNCNAME} PACKAGE-NAME" exit ${EXIT_FAILURE} fi if [ $# -eq 2 ]; then TARGET_DIR=${2} else TARGET_DIR=${PACKAGE} fi # Checking if we can find at least one patch. if ls ${LFS_PKG_DIR}/${1}-*.patch 1> /dev/null 2>&1; then cd ${LFS_PKG_DIR} for patch in ${1}-*.patch; do apply_patch ${patch} ${TARGET_DIR} || exit 1 done fi return $? } # Decompression of a package # First argument: package name # Second argument: directory where decompressing (optional) decompress_package() { # Checking for correct number of arguments if [ $# -eq 1 ]; then local TOPDIR=${LFS_TMP} elif [ $# -eq 2 ]; then local TOPDIR=${2} else echo "${FUNCNAME}(): Incorrect number of arguments (must be 1 or 2)" > /dev/stderr return ${EXIT_FAILURE} fi local PACKAGE=${1} # List of default archive extensions to try local MY_ARCH_EXT="tar.bz2 tar.gz tgz tar.Z zip" for arch_ext in ${MY_ARCH_EXT}; do if [ ! -f ${LFS_PKG_DIR}/${PACKAGE}.${arch_ext} ]; then # Try next archive extension. continue; fi if [ -d ${TOPDIR}/${PACKAGE} ]; then # Removing old source directory (if any) rm -v -rf ${TOPDIR}/${PACKAGE} || exit 1 fi cd ${TOPDIR} # Decompressing package case ${arch_ext} in tar.bz2) tar ${TAR_OPTS} -jxvf ${LFS_PKG_DIR}/${PACKAGE}.${arch_ext} || return 1 ;; tar.gz|tgz|tar.Z) tar ${TAR_OPTS} -zxvf ${LFS_PKG_DIR}/${PACKAGE}.${arch_ext} || return 1 ;; zip) echo ZIPZIPZIP unzip ${LFS_PKG_DIR}/${PACKAGE}.${arch_ext} || return 1 ;; esac cd - 1> /dev/null 2>&1 return $? done # Failure or file not found echo "${FUNCNAME}(): Missing source package for \"${PACKAGE}\"" > /dev/stderr return ${EXIT_FAILURE} } # Installation of a package # # First argument: Real package name # Second argument: Installation script name # Third argument: Unique identification label in 'install.log' # Remaining arguments: Additional configure options ipkg() { START_TIME=$(echo `date +%s`) local PACKAGE_NAME=${1} local SCRIPT=./${2} local LABEL=${3} # Checking for correct number of arguments if [ $# -lt 3 ]; then echo echo "${FUNCNAME}(): Missing argument" echo " command was: \"${FUNCNAME}() $*\"" exit ${EXIT_FAILURE} fi shift shift shift local CONFIGURE_OPTS=${*} # Checking if script is valid and executable if [ ! -x ${SCRIPT} ]; then echo echo "${FUNCNAME}(): script not found: ${SCRIPT}" exit ${EXIT_FAILURE} fi PACKAGE_LOG=${LFS_LOG_DIR}/${LABEL}.log # Checking if package was previously successfully installed if grep "^${LABEL} successfully installed" ${LFS_LOG_FILE} \ 1> /dev/null 2>&1; then return $EXIT_SUCCESS fi # Displaying label MSGSTRING="Installing ${LABEL}" display_checkbox_msg ${MSGSTRING} echo "------------------------" 1>> ${LFS_LOG_FILE} echo "${MSGSTRING}" 1>> ${LFS_LOG_FILE} echo "Decompressing package" 1>> ${PACKAGE_LOG} 2>&1 && decompress_package ${PACKAGE_NAME} 1> /dev/null 2>> ${PACKAGE_LOG} eval_retval local DECOMPRESSED_DIRNAME=$(static_decompressed_dirname ${PACKAGE_NAME}) # Rename the decompressed package as per the package name if necessary if [ "x${DECOMPRESSED_DIRNAME}" != "x${PACKAGE_NAME}" ]; then mv ${LFS_TMP}/${DECOMPRESSED_DIRNAME} ${LFS_TMP}/${PACKAGE_NAME} eval_retval fi # Displaying package source size in log file echo " Source size:" $(du -h -s ${LFS_TMP}/${PACKAGE_NAME} | awk '{ print $1 }') 1>> ${LFS_LOG_FILE} # Removing old build directory (if any) if [ -d ${LFS_TMP}/${PACKAGE_NAME}-build ]; then echo "Removing old build directory" 1>> ${PACKAGE_LOG} 2>&1 && rm -v -rf ${LFS_TMP}/${PACKAGE_NAME}-build 1> /dev/null 2>> ${PACKAGE_LOG} eval_retval fi # Creating build directory mkdir -v ${LFS_TMP}/${PACKAGE_NAME}-build 1>> ${PACKAGE_LOG} 2>&1 eval_retval # Executing script. ${SCRIPT} ${PACKAGE_NAME} ${CONFIGURE_OPTS} 1>> ${PACKAGE_LOG} 2>&1 eval_retval # Displaying package build size in log file BUILD_SIZE=$(du -h -s -c ${LFS_TMP}/${PACKAGE_NAME} ${LFS_TMP}/${PACKAGE_NAME}-build | grep total | awk '{ print $1 }') echo " Build size : ${BUILD_SIZE}" 1>> ${LFS_LOG_FILE} # Some scripts need to preserve the source or build directory. They can # do so by renaming them. if [ -d ${LFS_TMP}/${PACKAGE_NAME} ]; then # Removing source directory echo "Removing source directory" 1>> ${PACKAGE_LOG} 2>&1 && rm -v -rf ${LFS_TMP}/${PACKAGE_NAME} 1> /dev/null 2>> ${PACKAGE_LOG} eval_retval fi if [ -d ${LFS_TMP}/${PACKAGE_NAME}-build ]; then # Removing build directory echo "Removing build directory" 1>> ${PACKAGE_LOG} 2>&1 && rm -v -rf ${LFS_TMP}/${PACKAGE_NAME}-build 1> /dev/null 2>> ${PACKAGE_LOG} eval_retval fi # Writing success string to the end of the log file echo "${LABEL} successfully installed" 1>> ${LFS_LOG_FILE} # Displaying build time after the package name print_status success return $EXIT_SUCCESS } # Installation of a package, removing source and build directories after. # # First argument: package name # Second argument: script name # Remaining arguments: additional configure options ipkg_cust() { # Checking for correct number of arguments if [ $# -lt 2 ]; then echo echo "${FUNCNAME}(): Wrong number of arguments" echo " command was: \"${FUNCNAME}() $*\"" exit ${EXIT_FAILURE} fi local PACKAGE=${1} local CUSTOM_SCRIPT=${2} local LABEL=${PACKAGE} shift shift local CONFIGURE_OPTS=${*} ipkg ${PACKAGE} ${CUSTOM_SCRIPT} ${LABEL} ${CONFIGURE_OPTS} } # Installation of a package conforming to GNU autotools. # The package must be able to be built outside the # source directory. # # First argument: package name # Remaining arguments: additional configure options ipkg_ac() { # Checking for correct number of arguments if [ $# -lt 1 ]; then echo echo "${FUNCNAME}(): Wrong number of arguments" echo " command was: \"${FUNCNAME}() $*\"" exit ${EXIT_FAILURE} fi local PACKAGE=${1} local LABEL=${PACKAGE} shift local CONFIGURE_OPTS=${*} ipkg ${PACKAGE} cis-ac ${LABEL} ${CONFIGURE_OPTS} } # Installation of a package conforming to GNU autotools, # but that must be built inside the source directory. # # First argument: package name # Remaining arguments: additional configure options ipkg_ac_nb() { # Checking for correct number of arguments if [ $# -lt 1 ]; then echo echo "${FUNCNAME}(): Wrong number of arguments" echo " command was: \"${FUNCNAME}() $*\"" exit ${EXIT_FAILURE} fi local PACKAGE=${1} local LABEL=${PACKAGE} shift local CONFIGURE_OPTS=${*} ipkg ${PACKAGE} cis-ac-nobuild ${LABEL} ${CONFIGURE_OPTS} } # Installation of a GNOME package. # # First argument: package name # Remaining arguments: additional configure options ipkg_gnome() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo echo "${FUNCNAME}(): Wrong number of arguments" echo " command was: \"${FUNCNAME}() $*\"" exit ${EXIT_FAILURE} fi local PACKAGE=${1} local LABEL=${PACKAGE} shift local CONFIGURE_OPTS=${*} ipkg ${PACKAGE} cis-gnome ${LABEL} ${CONFIGURE_OPTS} } # Installation of a PERL module # # First argument: package name ipkg_pm() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo echo "${FUNCNAME}(): Wrong number of arguments" echo " command was: \"${FUNCNAME}() $*\"" exit ${EXIT_FAILURE} fi local PACKAGE=${1} local LABEL=${PACKAGE} ipkg ${PACKAGE} cis-pm ${LABEL} } # Run command, no log # First argument: Message to display during script # Second argument: command + arguments rcmd() { START_TIME=$(echo `date +%s`) # Checking for correct number of arguments if [ $# -lt 2 ]; then echo "${FUNCNAME}(): Missing argument(s)" exit ${EXIT_FAILURE} fi local MSGSTRING=${1} local CMD=${2} shift local CMD_WITH_ARGS=${*} display_checkbox_msg ${MSGSTRING} echo ${CMD_WITH_ARGS} 1>> ${LFS_LOG_FILE} # Executing command ${CMD_WITH_ARGS} 1>> ${LFS_LOG_FILE} 2>&1 if [ $? -ne 0 ]; then print_status failure if [ -n "${RCMD_NO_EXIT}" ]; then return ${EXIT_FAILURE} else exit ${EXIT_FAILURE} fi fi # Displaying build time after the package name print_status success return $EXIT_SUCCESS } # Display the action name, run a command, log its output and display it's # status. Write to install log file when successfully completed so it # won't be called again after a successfull installation. # First argument: "once" -> script is run only once # "mult" -> script can be run more than once # Second argument: Message to display during script # Third argument: script name (will be label) rscr() { START_TIME=$(echo `date +%s`) # Checking for correct number of arguments if [ $# -lt 3 ]; then echo "${FUNCNAME}(): Missing argument(s)" exit ${EXIT_FAILURE} fi SCRMODE=${1} MSGSTRING=${2} SCRIPT=${3} LABEL=${SCRIPT} shift shift SCRIPT_ARGS=${*} # Checking script mode: if [ "x${SCRMODE}" != "xonce" -a "x${SCRMODE}" != "xmult" ]; then echo "${FUNCNAME}(): First argument invalid, must be \"once\" or \"mult\" (\"${SCRMODE}\" given)" exit ${EXIT_FAILURE} fi # Checking if script is valid and executable if [ ! -x ./${SCRIPT} ]; then echo "${FUNCNAME}(): script not found: ${SCRIPT}" exit ${EXIT_FAILURE} fi PACKAGE_LOG=${LFS_LOG_DIR}/${LABEL}.log if [ "x${SCRMODE}" = "xonce" ]; then # Checking if package was previously successfully installed if grep "^${LABEL} successfully installed" ${LFS_LOG_FILE} 1> /dev/null 2>&1; then return $EXIT_SUCCESS fi fi display_checkbox_msg ${MSGSTRING} echo "------------------------" 1>> ${LFS_LOG_FILE} echo ${LABEL} 1>> ${LFS_LOG_FILE} # Executing script ./${SCRIPT} ${SCRIPT_ARGS} 1>> ${PACKAGE_LOG} 2>&1 eval_retval if [ "x${SCRMODE}" = "xonce" ]; then # Writing success string to the end of the log file echo "${LABEL} successfully installed" 1>> ${LFS_LOG_FILE} fi # Displaying build time after the package name print_status success return $EXIT_SUCCESS } get_total_build_time() { INFILE=${1} # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" echo "Usage: ${FUNCNAME} LOGFILE" exit ${EXIT_FAILURE} fi TIMES=$(cat ${INFILE} | grep "Build time:" | sed "s!Build time: \(.*\)h!\1!g") for time in ${TIMES}; do HOURS=$(( $HOURS + $(echo ${time} | sed "s!^\([0-9]*\)\..*!\1!g") )) # The first SED command extracts the minutes (fractions of an hour). # The second SED command removed the leading zero, if applicable. MIN=$(( $MIN + $(echo ${time} | sed "s!.*\.\([0-9][0-9]\)!\1!g" | sed "s!^0\([0-9]\)!\1!g" ) )) done HOURS=$(( ${HOURS} + ( ${MIN} / 100 ) )) MIN=$(( ${MIN} % 100 )) echo "${HOURS}.${MIN}" } # Global variable: START_TIME display_build_time() { END_TIME=$(echo `date +%s`) HOURS=$(( ( ${END_TIME} - ${START_TIME} ) / 3600 )) echo -n " ("${HOURS}"." echo -n " Build time: ${HOURS}." 1>> ${LFS_LOG_FILE} # Time is inferior to 1 hour... MINUTES=$(( ( ( ${END_TIME} - ${START_TIME} ) % 3600 ) / 36 )) if [ ${MINUTES} -lt 10 ]; then echo -n "0" echo -n "0" 1>> ${LFS_LOG_FILE} fi if [ ${MINUTES} -eq 0 ]; then echo -n "1" echo -n "1" 1>> ${LFS_LOG_FILE} else echo -n ${MINUTES} echo -n ${MINUTES} 1>> ${LFS_LOG_FILE} fi echo -n "h)" echo "h" 1>> ${LFS_LOG_FILE} } # Global variable: MSGSTRING display_checkbox_msg() { echo -en "${BRACKET}[ ${BRACKET}]${NORMAL} ${MSGSTRING}" } # The print_status prints a coloured "X" letter inside the checkbox to the left # of the screen (the checkbox is already displayed, and the cursor must have # been saved to where to put the mark). # 1st arg: status: success, warning or failure # Global variables: MSGSTRING and START_TIME print_status() { if [ $# -ne 1 ]; then # If no parameters are given to the print_status function, print usage # information. echo "Usage: ${FUNCNAME}() {success|warning|failure} [STRING] [TIME]" return ${EXIT_FAILURE} fi local COLOR="" case "${1}" in success) COLOR=$SUCCESS ;; warning) COLOR=$WARNING ;; failure) COLOR=$FAILURE ;; *) echo "Usage: ${FUNCNAME}() {success|warning|failure}" return ${EXIT_FAILURE} ;; esac # Reposition cursor at start of line echo -en "${SET_CURSOR_START_LINE}" echo -en "${BRACKET}[${COLOR}X${BRACKET}]${NORMAL} ${MSGSTRING}" if [ "x${MSGSTRING}" != "x" ]; then display_build_time fi echo }