#!/bin/sh
#
# md5tok: script that will build a file suitable for
#         processing by md5sum. The input files are
#         from /lfs-book/chapter03/{packages,patches}.html
#
# usage:  md5tok <lfs-book-dir> > /lfs-sources/md5sums
#         cd /lfs-sources; md5sum -c md5sums
#


# init
dir="$1"
if [ ! -d "$dir" ]; then
        echo "Invalid directory: $dir"
        exit 1
fi

# cat two files together
cat "$dir/packages.html" "$dir/patches.html" > .tempmd5

# open file on fd8
exec 8<".tempmd5"

tokp="<p>"
tokpend="</p>"
md5str=""
filestr=""

# downloadToken: function processes the "Download:" token
#                until the end token (tokpend). Then strips
#                the resulting string to the filename only.
downloadToken()
{
        while read -u8
         do
                if [ $REPLY == $tokpend ]; then
                        break
                fi
                filestr+=$REPLY
         done
        filestr=${filestr%<*>}
        filestr=${filestr%<*>}
        filestr=${filestr##*/}
}

# md5Token: function processes the "MD5" token until the end
#           token (tokpend). Then strips the resulting string
#           to the md5sum only.
md5Token()
{
        while read -u8
         do
                if [ $REPLY == $tokpend ]; then
                        break
                fi
                md5str+=$REPLY
         done
        md5str=${md5str%%<*>}
        md5str=${md5str#*>}
}

# pToken: function processes a <p> token from the input file.
#         Subtokens "Download:" and "MD5" are further processed
#         and all other data is ignored.
pToken()
{
        while read -u8
         do
                for token in $REPLY
                 do
                        if [ $token == $tokpend ]; then
                                return
                        fi
                        if [ $token == "Download:" ]; then
                                downloadToken;
                                return
                        fi
                        if [ $token == "MD5" ]; then
                                md5Token;
                                return
                        fi
                 done
         done
}

# main loop
while read -u8
  do
        for token in $REPLY
         do
                if [ $token == $tokp ]; then
                        pToken;
                        if [ -n "$md5str" ]; then
                                echo "$md5str $filestr"
                                md5str=""
                                filestr=""
                        fi
                        break
                fi
         done
  done

# close fd8
exec 8<&-

# clean up
rm ".tempmd5"

Anyhow, I retyped, so hopefully there are no typos.
Could use more error checking.

Shawn

-- 
http://linuxfromscratch.org/mailman/listinfo/lfs-support
FAQ: http://www.linuxfromscratch.org/lfs/faq.html
Unsubscribe: See the above information page

Reply via email to