#!/bin/sh
#functions loader file for getXX.sh downloads

get_file()
{
        URL=$1
        OUT=$2
        #MD5_URL=$3
        OUT_MD5=$OUT.md5

        #first get the MD5 so we can check if a download is even needed.
        if [ "$MD5_URL" != "" ]; then
                wget $WGET_OPTION -O $OUT_MD5 $MD5_URL

                if [ ! -s $OUT_MD5 ]; then
                        echo "";
                        echo "*** MD5 DOWNLOAD FAILURE from $MD5_URL!! ***";
                        echo "";
                        exit 1;
                fi

                if [ -s $OUT ]; then

                        md5sum -c $OUT_MD5
                        if [ "$?" -eq 0 ]; then
                                echo "MD5 on $OUT passes.";
                                return;
                        fi
                fi
        elif [ -s $OUT ] && [ -s $OUT_MD5 ]; then
                md5sum -c $OUT_MD5
                if [ "$?" -eq 0 ]; then
                        echo "MD5 on $OUT passes.";
                        return;
                fi
        fi

        #either there was no md5, no file yet, or it failed.
        #grab the file.
        wget $WGET_OPTION -O $OUT $URL

        if [ "$MD5_URL" != "" ] && [ -s $OUT ]; then
                md5sum -c $OUT_MD5
                if [ "$?" -ne 0 ]; then
                        echo "";
                        echo "*** MD5 FAIL ON $OUT ***";
                        echo "       ABORT!!          ";
                        echo "";
                        exit 1;
                fi
        fi

        if [ "$MD5_URL" = "" ] && [ -s $OUT ]; then
                md5sum $OUT > $OUT_MD5
        fi

}

