]> git.sesse.net Git - casparcg/blob - dependencies64/ffmpeg/ffmpeg-build-linux/fetchurl
[build] Added the modified build script based on an old version of https://github...
[casparcg] / dependencies64 / ffmpeg / ffmpeg-build-linux / fetchurl
1 #!/bin/sh
2 #
3 # Small utility to fetch and unpack archives on the web (with cache)
4 #
5 # Depends on : curl, tar
6 #
7
8 set -e
9 set +u
10
11 # ENV vars, inherited from external
12 CACHE=${CACHE:-1}
13 UNPACK=${UNPACK:-1}
14 VERBOSE=${VERBOSE:-0}
15
16 EXTRACT_DIR=${EXTRACT_DIR:-`pwd`}
17 if [ -n "$HOME" ]; then
18   CACHE_DIR=${CACHE_DIR:-$HOME/.cache/fetchurl}
19 else
20   CACHE_DIR=${CACHE_DIR:-}
21 fi
22 TMP_DIR=${TMP_DIR:-/tmp}
23
24 URL=$1
25
26 set -u
27
28 stderr () {
29   echo $@ 1>&2
30 }
31
32 sh () {
33   echo $ $@
34   if [ "$VERBOSE" -ne 0 ]; then
35     $@
36   else
37     $@ >/dev/null 2>&1
38   fi
39 }
40
41 expand_path() {
42         here=`pwd`
43         cd $1
44         echo `pwd -P`
45         cd "$here"
46 }
47
48 usage() {
49   echo "Usage: fetchurl url"
50   echo "CACHE=${CACHE}"
51   echo "UNPACK=${UNPACK}"
52   echo "VERBOSE=${VERBOSE}"
53
54   echo "EXTRACT_DIR=${EXTRACT_DIR}"
55   echo "CACHE_DIR=${CACHE_DIR}"
56   echo "TMP_DIR=${TMP_DIR}"
57
58   echo "URL=${URL}"
59   exit 1
60 }
61
62 if [ -z "$URL" ]; then
63   stderr "ERROR: missing url"
64   usage
65 fi
66
67 if [ -z "$CACHE_DIR" ] && [ "$CACHE" -ne 0 ]; then
68   stderr "ERROR: missing cache dir"
69   usage
70 fi
71
72 filename=`basename "$URL" | sed 's/\?.*//'`
73 tmp_file="$TMP_DIR/$filename"
74 cache_file="$CACHE_DIR/$filename"
75
76 mkdir -p "$CACHE_DIR"
77
78 # Fetch
79 if [ "$CACHE" -eq 0 ] || [ ! -f "$cache_file" ]; then
80   rm -rf "$tmp_file"
81   sh curl -L -o "$tmp_file" "$URL"
82   sh mv "$tmp_file" "$cache_file"
83 fi
84
85 # TODO: checksums
86
87 # Unpack
88 if [ "$UNPACK" -ne 0 ]; then
89
90   if [ "$filename" != "${filename%.tar.gz}" ]; then
91     extname=.tar.gz
92   elif [ "$filename" != "${filename%.tgz}" ]; then
93     extname=.tgz
94   elif [ "$filename" != "${filename%.tar.bz2}" ]; then
95     extname=.tar.bz2
96   elif [ "$filename" != "${filename%.tar.xz}" ]; then
97     extname=.tar.xz
98   else
99     stderr extension of $filename is not supported
100     exit 1
101   fi
102
103   target_dir=`expand_path "$EXTRACT_DIR"`
104   mkdir -p "$target_dir"
105   sh cd "$target_dir"
106   
107   case "$extname" in
108     .tar.gz|.tgz)    
109       sh tar xzvf "$cache_file"
110     ;;
111     .tar.bz2)
112       sh tar xjvf "$cache_file"
113     ;;
114     .tar.xz)
115       sh tar xJvf "$cache_file"
116     ;;
117     *)
118       stderr BUG, this should not happen
119       exit 1
120     ;;
121   esac
122 fi
123