Merge topics 'compute_count_fix', 'build_rewrite'
[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
11 CACHE_DIR="$(pwd)/cache"
12
13 # Make sure the cache dir exists
14 function cache_dir {
15     if [ ! -d $CACHE_DIR/ ]; then mkdir $CACHE_DIR/; fi
16     if [ ! -f $CACHE_DIR/.cache ]; then touch $CACHE_DIR/.cache; fi
17     echo "Cache Dir: $CACHE_DIR"
18 }
19
20 function cache_git_tar {
21     echo "cache_git_tar git ls-remote"
22 }
23
24 # $1 = download url
25 # $2 = filename to write to
26 function curl_file {
27     echo "Downloading $1"
28     echo "Cache location: $CACHE_DIR/$2"
29     curl -L $1 > $CACHE_DIR/$2
30     sed -i "/$2/d" $CACHE_DIR/.cache
31     echo "$(md5sum $CACHE_DIR/$2) $2" >> $CACHE_DIR/.cache
32 }
33
34 # $1 =  download url
35 function populate_cache {
36     cache_dir
37
38     # get the file name
39     filename="${1##*/}"
40
41     # check if the cache file exists
42     # and if it has an md5 compare that
43     echo "Checking cache file: $1"
44     if [ ! -f $CACHE_DIR/${filename} ]; then
45         curl_file $1 $filename
46     else
47         remote_md5="$(curl -L ${1}.md5 | awk {'print $1'})"
48         if [ -z "$remote_md5" ]; then
49             echo "Got empty MD5 from remote for $filename, skipping MD5 check"
50         elif [ "$remote_md5" != "$(grep ${filename} $CACHE_DIR/.cache | awk {'print $1'})" ]; then
51             curl_file $1 $filename
52         fi
53     fi
54 }
55
56 # $1 = filename to get from cache
57 function get_cached_file {
58   cp -f $CACHE_DIR/$1 .
59 }