Improved fpkg and now chwcking integrity of bz2 archives
[hvlinux.git] / functions-update
index 742d9f1..2f2359f 100644 (file)
@@ -22,8 +22,10 @@ SOURCEFORGE_URL="http://cdnetworks-us-1.dl.sourceforge.net"
 GNOME_URL="http://ftp.gnome.org/pub/gnome/sources"
 GNU_URL="http://ftp.gnu.org/pub/gnu"
 TETEX_URL="http://www.tug.org/ftp/tex-archive/systems/unix/teTeX"
+BLFS_XORG_URL="http://anduin.linuxfromscratch.org/files/BLFS/svn/xorg"
+XORG_URL="http://xorg.freedesktop.org/releases/individual"
 
-WGETCMD="wget --directory-prefix=${LFS_PKG_DIR} --timeout=15 --tries=3 -nc -c -o ${WGET_LOG_FILE}"
+WGETCMD="wget --directory-prefix=${LFS_PKG_DIR} --timeout=15 --tries=3 -nc --continue --no-check-certificate -o ${WGET_LOG_FILE}"
 
 LFS_PATCHES_LIST=${LFS_PKG_DIR}/patches-list-lfs.html
 BLFS_PATCHES_LIST=${LFS_PKG_DIR}/patches-list-blfs.html
@@ -186,7 +188,7 @@ detect_file_not_found()
     # HTTP: will return error code 404.
     # FTP: will say "File not found"
     if grep "404" ${WGET_LOG_FILE} 1> /dev/null 2>&1; then
-        echo NOTFOUND
+        echo "404 NOTFOUND"
         return 0
     elif grep "No such file" ${WGET_LOG_FILE} 1> /dev/null 2>&1; then
         return 0
@@ -211,7 +213,7 @@ static_getpkg()
        return 1
     fi
 
-    if [ -z "${ARCH_EXT}" ]; then
+    if [ -z "${MY_ARCH_EXT}" ]; then
         # List of default archive extensions to try
         MY_ARCH_EXT="tar.bz2 tar.gz tgz tar.Z"
     fi
@@ -219,6 +221,11 @@ static_getpkg()
     echo "  MY_ARCH_EXT=${MY_ARCH_EXT}"
 
     for arch_ext in ${MY_ARCH_EXT}; do
+        # Don't take any chance: remove any partially downloaded file.
+        # If we arrive here, it means the final destination file was not found
+        # so we can safely remove any file prior to trying to download it.
+        rm -f ${LFS_PKG_DIR}/${PACK}.${arch_ext}
+
         ${WGETCMD} ${URL}/${PACK}.${arch_ext}
 
         # Failure: if it was a connection timeout, don't try for other file extensions.
@@ -246,10 +253,47 @@ static_getpkg()
 # Get package if it is not in the repository
 # Arg. #1: Package name and version
 # Arg. #2: URL
+# Options:
+#   -e     File extension (default is tar.bz2)
+#   -d     Fetch directory (default is LFS_PKG_DIR)
+#   -f     Filename (default is PACKAGE)
+#   -h     Display this help and returns
+#   -s     Subdirectory on server
 fpkg()
 {
-    PACK=${1}
-    URL=${2}
+    local FILE_EXT=""
+    local DEST_DIR=""
+    local SRC_FILENAME=""
+    local SRC_DIR=""
+
+    while getopts "e:d:f:hs:" flag ;do
+        case ${flag} in
+           e)
+                # File extension
+                FILE_EXT=${OPTARG}
+               ;;
+           d)
+                # Fetch directory (where to put file)
+                DEST_DIR=${OPTARG}
+               ;;
+           f)
+                # Filename if different than package name
+                SRC_FILENAME=${OPTARG}
+               ;;
+           s)
+               SRC_DIR=${OPTARG}
+                ;;
+           ?)
+               echo "${FUNCNAME}(): Invalid option: ${OPTARG}."
+               return 1
+               ;;
+        esac
+    done
+    shift `expr "${OPTIND}" - 1`
+
+    unset OPTSTRING
+    unset OPTIND
+    unset OPTARG
 
     # Checking for correct number of arguments
     if [ $# -ne 2 ]; then
@@ -257,83 +301,73 @@ fpkg()
        return 1
     fi
 
-    # Default extension
-    local FINAL_EXT=tar.bz2
-
+    local PACK=${1}
+    local URL=${2}
     local FETCH_STRING="Fetching ${PACK}"
 
-    if [ -n "${ARCH_EXT}" ]; then
-        FINAL_EXT=${ARCH_EXT}
-        FETCH_STRING="${FETCH_STRING}.${ARCH_EXT}"
+    if [ -z "${SRC_FILENAME}" ]; then
+        # Default source filename = name of package
+        SRC_FILENAME=${PACK}
     fi
 
-    # Default destination directory
-    local MY_FETCH_DIR=${LFS_PKG_DIR}
+    if [ -n "${FILE_EXT}" ]; then
+        FETCH_STRING="${FETCH_STRING}.${FILE_EXT}"
+    fi
 
-    if [ -n "${FETCH_DIR}" ]; then
-        local MY_FETCH_DIR=${FETCH_DIR}
-        local MOVE_FILE=1
+    if [ -z "${FILE_EXT}" -o \
+        "x${FILE_EXT}" = "xtar.gz" -o \
+        "x${FILE_EXT}" = "xtgz" -o \
+        "x${FILE_EXT}" = "xtar.Z" -o \
+        "x${FILE_EXT}" = "xtar.bz2" \
+        ]; then
+        FINAL_EXT=tar.bz2
+    else
+        FINAL_EXT=${FILE_EXT}
     fi
 
-    DEST_FILE=${MY_FETCH_DIR}/${PACK}.${FINAL_EXT}
+    if [ -z "${DEST_DIR}" ]; then
+        # Default destination directory
+        DEST_DIR=${LFS_PKG_DIR}
+    fi
 
-    ##########echo "DEST_FILE=${DEST_FILE}"
-    if [ ! -f ${DEST_FILE} ]; then
-       rcmd "${FETCH_STRING}" static_getpkg ${PACK} ${URL} ${ARCH_EXT}
+    mkdir -v -p ${DEST_DIR} >> ${LFS_LOG_FILE} 2>&1
 
-        # Move to final directory if alternate directory specified
-       if [ -f ${LFS_PKG_DIR}/${PACK}.${FINAL_EXT} -a -n "${MOVE_FILE}" ]; then
-           mv ${LFS_PKG_DIR}/${PACK}.${FINAL_EXT} ${DEST_FILE}
-       fi
+    if [ -z "${SRC_DIR}" ]; then
+        # Default source subdirectory on server
+        SRC_DIR=""
+    else
+        URL=${URL}/${SRC_DIR}
     fi
 
-    # Check if a patch is available.
-    static_getpatch ${PACK}
-}
+    DEST_FILE=${DEST_DIR}/${PACK}.${FINAL_EXT}
 
-# Get package if it is not in the repository.
-# This function is used if the source and target names for the
-# package are different (name mismatch).
-# Arg. #1: Target package name and version (on disk)
-# Arg. #2: Source package name and version (on internet)
-# Arg. #3: URL
-fpkg_mis()
-{
-    TARGET=${1}
-    SOURCE=${2}
-    URL=${3}
+    if [ ! -f ${DEST_FILE} ]; then
+        rcmd "${FETCH_STRING}" static_getpkg ${SRC_FILENAME} ${URL} ${FILE_EXT}
 
-    # Checking for correct number of arguments
-    if [ $# -ne 3 ]; then
-       echo "${FUNCNAME}(), wrong number of arguments: ${*}"
-       reurn 1
-    fi
+        # Move file if source filename is not equal to package name and/or destination
+        # directory is not the default:
+        DOWNLOADED_FILE=${LFS_PKG_DIR}/${SRC_FILENAME}.${FINAL_EXT}
 
-    if [ -z "${ARCH_EXT}" ]; then
-        # Default extension
-        FINAL_EXT=tar.bz2
-    else
-        FINAL_EXT=${ARCH_EXT}
+        if [ "${DEST_FILE}" != "${DOWNLOADED_FILE}" ]; then
+           mv ${DOWNLOADED_FILE} ${DEST_FILE}
+       fi
     fi
 
-    if [ ! -f ${LFS_PKG_DIR}/${TARGET}.${FINAL_EXT} ]; then
-       fpkg ${SOURCE} ${URL}
-       
-       if [ -f ${LFS_PKG_DIR}/${SOURCE}.${FINAL_EXT} ]; then
-           mv ${LFS_PKG_DIR}/${SOURCE}.${FINAL_EXT} ${LFS_PKG_DIR}/${TARGET}.${FINAL_EXT}
-       fi
+    # Test integrity of archive if requested
+    if [ -f ${DEST_FILE} -a -n "${TEST_INTEGRITY}" -a "x${FINAL_EXT}" = "xtar.bz2" ]; then
+        rcmd "Testing integrity of ${PACK}.${FINAL_EXT}" bzip2 -t ${DEST_FILE}
     fi
 
-    # Check for available patches with TARGET name.
-    static_getpatch ${TARGET}
+    # Check for available patches with PACKAGE (TARGET) name.
+    static_getpatch ${PACK}
 
-    # Check for available patches with SOURCE name.
-    static_getpatch ${SOURCE}
+    # Check for available patches with SRC_FILENAME name.
+    static_getpatch ${SRC_FILENAME}
 
     # Rename any patch fetched (in fpkg call) and replace SOURCE by TARGET in
     # patch name.
-    if ls ${LFS_PKG_DIR}/${SOURCE}-*.patch 1> /dev/null 2>&1; then
-       rename ${SOURCE} ${TARGET} ${LFS_PKG_DIR}/${SOURCE}-*.patch
+    if ls ${LFS_PKG_DIR}/${SRC_FILENAME}-*.patch 1> /dev/null 2>&1; then
+       rename ${SRC_FILENAME} ${PACK} ${LFS_PKG_DIR}/${SRC_FILENAME}-*.patch
     fi
 }
 
@@ -342,7 +376,7 @@ fpkg_mis()
 # Arg. #2: Directory name (optional)
 fpkg_gnu()
 {
-    PACK=${1}
+    local PACK=${1}
 
     if [ $# -eq 2 ]; then
        NAME=${2}
@@ -443,6 +477,23 @@ fpkg_lfs()
     fi
 }
 
+# Get package if it is not in the repository
+# Arg. #1: Package name and version
+# Arg. #2: Subdirectory name (module name)
+fpkg_xorg()
+{
+    local PACK=${1}
+    local SUBDIR=${2}
+
+    # Checking for correct number of arguments
+    if [ $# -ne 2 ]; then
+       echo "${FUNCNAME}(), wrong number of arguments: ${*}"
+       return 1
+    fi
+
+    fpkg -d ${LFS_PKG_DIR}/${SUBDIR} -s ${SUBDIR} ${PACK} ${XORG_URL}
+}
+
 # Create a symbolic link to a package that is located in another stage
 # repository (to not have the same file twice).
 # Arg. #1: Source stage number (1, 2, 3, etc)