Generalization of recursive function
[apex.git] / build / cache.sh
1 #!/bin/sh
2 ##############################################################################
3 # Copyright (c) 2016 Red Hat Inc.
4 # Dan Radez <dradez@redhat.com>
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 source ./variables.sh
11
12 # Make sure the cache dir exists
13 function cache_dir {
14     if [ -f $CACHE_DIR ]; then rm -rf $CACHE_DIR; fi
15     if [ ! -d $CACHE_DIR/ ]; then mkdir $CACHE_DIR/; fi
16     if [ ! -f $CACHE_DIR/$CACHE_HISTORY ]; then touch $CACHE_DIR/$CACHE_HISTORY; fi
17     echo "Cache Dir: $CACHE_DIR"
18 }
19
20 # $1 = download url
21 # $2 = filename to write to
22 function curl_file {
23     if [ -f $CACHE_DIR/$2 ]; then
24         echo "Removing stale $2"
25         rm -f $CACHE_DIR/$2
26     fi
27     echo "Downloading $1"
28     echo "Cache download location: $CACHE_DIR/$2"
29     until curl -C- -L -o $CACHE_DIR/$2 $1  || (( count++ >= 20 )); do
30         echo -n '' #do nothing, we just want to loop
31     done
32     sed -i "/$2/d" $CACHE_DIR/$CACHE_HISTORY
33     echo "$(md5sum $CACHE_DIR/$2) $2" >> $CACHE_DIR/$CACHE_HISTORY
34 }
35
36 # $1 =  download url
37 # $2 =  remote md5
38 function populate_cache {
39     local my_md5
40     cache_dir
41
42     # get the file name
43     filename="${1##*/}"
44     # copy passed in md5
45     remote_md5=$2
46
47     # check if the cache file exists
48     # and if it has an md5 compare that
49     echo "Checking if cache file exists: ${filename}"
50     if [ ! -f $CACHE_DIR/${filename} ]; then
51         echo "Cache file: ${CACHE_DIR}/${filename} missing...will download..."
52         curl_file $1 $filename
53     else
54         echo "Cache file exists...comparing MD5 checksum"
55         if [ -z "$remote_md5" ]; then
56             remote_md5="$(curl -sf -L ${1}.md5 | awk {'print $1'})"
57         fi
58         if [ -z "$remote_md5" ]; then
59             echo "Got empty MD5 from remote for $filename, skipping MD5 check"
60             curl_file $1 $filename
61         else
62             my_md5=$(grep ${filename} ${CACHE_DIR}/${CACHE_HISTORY} | awk {'print $1'})
63             if [ -z "$my_md5" ]; then
64                 echo "${filename} missing in ${CACHE_HISTORY} file. Caculating md5..."
65                 my_md5=$(md5sum ${CACHE_DIR}/${filename} | awk {'print $1'})
66             fi
67             if [ "$remote_md5" != "$my_md5" ]; then
68                 echo "MD5 mismatch, local cache file MD5 is ${my_md5}"
69                 echo "              remote cache file MD5 is ${remote_md5}"
70                 echo "Downloading $filename"
71                 curl_file $1 $filename
72             else
73               echo "Will use cache for ${filename}"
74             fi
75         fi
76     fi
77 }