-Removed all ipkg_* functions and replaced them with a single ipkg function accepting...
[hvlinux.git] / functions
index 4f58faf..9c863ea 100644 (file)
--- a/functions
+++ b/functions
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/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.
 
@@ -24,6 +24,8 @@ 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
@@ -33,14 +35,27 @@ else
 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}"
@@ -48,8 +63,24 @@ case "${HVL_TARGET}" in
        ;;
 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}
 
+function function_exists
+{
+    local FUNCTION_NAME=$1
+
+    [ -z "$FUNCTION_NAME" ] && return 1
+
+    declare -F "$FUNCTION_NAME" > /dev/null 2>&1
+
+    return $?
+}
+
 # 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()
@@ -99,7 +130,10 @@ get_pkg_name()
        return 1
     fi
 
-    echo ${1} | sed "s!^\(.*\)-.*!\1!g"
+    # SED do not support non-greedy regexp:
+    # We want to match any characters preceeding a dash followed by a number
+    # (shortest match -> non-greedy)
+    echo ${1} | sed "s!\([^-][^0-9]*\)-[0-9].*!\1!"
 }
 
 # Saves the content of CFLAGS and CXXFLAGS environment variables.
@@ -512,11 +546,36 @@ static_decompressed_dirname()
 {
     local PACKAGE=${1}
 
-    # Remove optional "./" leading component with sed
-    # and extract base directory name with awk.
-    local DIRNAME=$(tar ${TAR_OPTS} -tf ${LFS_PKG_DIR}/${PACKAGE}.tar.bz2 | head -n1 | sed 's!^\./!!' | awk -F \/ '{print $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
 
-    echo ${DIRNAME}
+        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
@@ -596,66 +655,138 @@ decompress_package()
 
     local PACKAGE=${1}
 
-    if [ ! -f ${LFS_PKG_DIR}/${PACKAGE}.tar.bz2 ]; then
-       echo "${FUNCNAME}(): Missing source package: \"${PACKAGE}.tar.bz2\"" > /dev/stderr
-       return ${EXIT_FAILURE}
-    fi
-    
-    if [ -d ${TOPDIR}/${PACKAGE} ]; then
-       # Removing old source directory (if any)
-       rm -v -rf ${TOPDIR}/${PACKAGE} || exit 1
-    fi
+    # 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
 
-    # Decompressing package
-    # Option 'U' of tar is to remove each file prior to extracting over it
-    cd ${TOPDIR} &&
-    tar ${TAR_OPTS} -jxvf ${LFS_PKG_DIR}/${PACKAGE}.tar.bz2 &&
-    cd - 1> /dev/null 2>&1
+        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'
+# Arg. #1: Package name and version (ex: gcc-4.5.1)
 # Remaining arguments: Additional configure options
+# Options:
+#   -h     Display this help and returns
+#   -l     Unique identification label in 'install.log'
+#          (default is package name and version)
+#   -m     Installation mode:
+#            ac   Standard autoconf package, build in separate dir
+#            acnb Standard autoconf package, building in source dir
+#            nb   No autoconf (configure)
+#            gnome
+#            xorg
+#            pm
+#   -s     Name of script to execute (default is ipkg.sh)
 ipkg()
 {
     START_TIME=$(echo `date +%s`)
+
+    export IPKG_MODE="ac"
+    export HVLABEL="" # Global variable
+    local SCRIPT=./ipkg.sh
+
+    while getopts "hl:m:" flag ;do
+        case ${flag} in
+           l)
+                # Alternate label
+                HVLABEL=${OPTARG}
+               ;;
+           m)
+                # Installation mode
+                case ${OPTARG} in
+                   ac|acnb|noac|gnome|xorg|pm)
+                        IPKG_MODE=${OPTARG}
+                       ;;
+                    *)
+                       echo "${FUNCNAME}(): Unsupported mode: ${OPTARG}."
+                       return 1
+                        ;;
+                esac
+                ;;
+           s)
+                # Alternate script name
+                SCRIPT=${OPTARG}
+               ;;
+           ?)
+               echo "${FUNCNAME}(): Invalid option: ${OPTARG}."
+               return 1
+               ;;
+        esac
+    done
+    shift `expr "${OPTIND}" - 1`
+
+    unset OPTSTRING
+    unset OPTIND
+    unset OPTARG
+
     local PACKAGE_NAME=${1}
-    local SCRIPT=./${2}
-    local LABEL=${3}
+
     # Checking for correct number of arguments
-    if [ $# -lt 3 ]; then
+    if [ $# -lt 1 ]; then
         echo
        echo "${FUNCNAME}(): Missing argument"
        echo "  command was: \"${FUNCNAME}() $*\""
        exit ${EXIT_FAILURE}
     fi
 
-    shift
-    shift
     shift
     local CONFIGURE_OPTS=${*}
 
+    if [ -z "${HVLABEL}" ]; then
+        # Default label = package name and version
+        HVLABEL=${PACKAGE_NAME}
+    fi
+
     # Checking if script is valid and executable
     if [ ! -x ${SCRIPT} ]; then
         echo
-       echo "${FUNCNAME}(): script not found: ${SCRIPT}"
+       echo "${FUNCNAME}(): cannot execute script: ${SCRIPT}"
        exit ${EXIT_FAILURE}
     fi
 
-    PACKAGE_LOG=${LFS_LOG_DIR}/${LABEL}.log
+    PACKAGE_LOG=${LFS_LOG_DIR}/${HVLABEL}.log
 
     # Checking if package was previously successfully installed
-    if grep "^${LABEL} successfully installed" ${LFS_LOG_FILE} \
+    if grep "^${HVLABEL} successfully installed" ${LFS_LOG_FILE} \
       1> /dev/null 2>&1; then
        return $EXIT_SUCCESS
     fi
     
     # Displaying label
-    MSGSTRING="Installing ${LABEL}"
+    MSGSTRING="Installing ${HVLABEL}"
     display_checkbox_msg ${MSGSTRING}
 
     echo "------------------------" 1>> ${LFS_LOG_FILE}
@@ -711,7 +842,7 @@ ipkg()
     fi
 
     # Writing success string to the end of the log file
-    echo "${LABEL} successfully installed" 1>> ${LFS_LOG_FILE}
+    echo "${HVLABEL} successfully installed" 1>> ${LFS_LOG_FILE}
 
     # Displaying build time after the package name
     print_status success
@@ -719,119 +850,6 @@ ipkg()
     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
@@ -891,7 +909,7 @@ rscr()
     SCRMODE=${1}
     MSGSTRING=${2}
     SCRIPT=${3}
-    LABEL=${SCRIPT}
+    HVLABEL=${SCRIPT}
     shift
     shift
     SCRIPT_ARGS=${*}
@@ -908,18 +926,18 @@ rscr()
        exit ${EXIT_FAILURE}
     fi
 
-    PACKAGE_LOG=${LFS_LOG_DIR}/${LABEL}.log
+    PACKAGE_LOG=${LFS_LOG_DIR}/${HVLABEL}.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
+        if grep "^${HVLABEL} 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}
+    echo ${HVLABEL} 1>> ${LFS_LOG_FILE}
 
     # Executing script
     ./${SCRIPT} ${SCRIPT_ARGS} 1>> ${PACKAGE_LOG} 2>&1
@@ -927,7 +945,7 @@ rscr()
 
     if [ "x${SCRMODE}" = "xonce" ]; then
         # Writing success string to the end of the log file
-        echo "${LABEL} successfully installed" 1>> ${LFS_LOG_FILE}
+        echo "${HVLABEL} successfully installed" 1>> ${LFS_LOG_FILE}
     fi
 
     # Displaying build time after the package name