xref: /aoo41x/main/fetch_tarballs.sh (revision 0b0f4a2b)
1#!/usr/bin/env bash
2#**************************************************************
3#
4#  Licensed to the Apache Software Foundation (ASF) under one
5#  or more contributor license agreements.  See the NOTICE file
6#  distributed with this work for additional information
7#  regarding copyright ownership.  The ASF licenses this file
8#  to you under the Apache License, Version 2.0 (the
9#  "License"); you may not use this file except in compliance
10#  with the License.  You may obtain a copy of the License at
11#
12#    http://www.apache.org/licenses/LICENSE-2.0
13#
14#  Unless required by applicable law or agreed to in writing,
15#  software distributed under the License is distributed on an
16#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17#  KIND, either express or implied.  See the License for the
18#  specific language governing permissions and limitations
19#  under the License.
20#
21#**************************************************************
22
23file_list_name=$1
24
25if [ -z "$TARFILE_LOCATION" ]; then
26    echo "ERROR: no destination defined! please set TARFILE_LOCATION!"
27    exit
28fi
29
30if [ ! -d "$TARFILE_LOCATION" ]; then
31    mkdir $TARFILE_LOCATION
32fi
33if [ ! -d "$TARFILE_LOCATION" ]; then
34    echo "ERROR: can't create"
35    exit
36fi
37
38if [ -z "$1" ]; then
39    echo "ERROR: parameter missing!"
40    echo "usage: $0 <fetch list>"
41    echo "first line must define the base url."
42    exit
43fi
44
45# Downloader method selection
46fetch_bin=
47fetch_args=
48
49#Look for FreeBSD's fetch(1) first
50if [ -x /usr/bin/fetch ]; then
51    fetch_bin=/usr/bin/fetch
52    fetch_args="-Fpr"
53    echo found FreeBSD fetch: $fetch_bin
54else
55  for wg in wget /usr/bin/wget /usr/local/bin/wget /usr/sfw/bin/wget /opt/sfw/bin/wget /opt/local/bin/wget; do
56    eval "$wg --version" > /dev/null 2>&1
57    ret=$?
58    if [ $ret -eq 0 ]; then
59        fetch_bin=$wg
60	    fetch_args="-nv -N"
61        echo found wget at `which $fetch_bin`
62        break 2
63    fi
64  done
65  if [ -z "$fetch_bin" ]; then
66    for c in curl /usr/bin/curl /usr/local/bin/curl /usr/sfw/bin/curl /opt/sfw/bin/curl /opt/local/bin/curl; do
67    # mac curl returns "2" on --version
68    #    eval "$i --version" > /dev/null 2>&1
69    #    ret=$?
70    #    if [ $ret -eq 0 ]; then
71        if [ -x $c ]; then
72            fetch_bin=$c
73	        fetch_args="$file_date_check -O"
74            echo found curl at `which $fetch_bin`
75            break 2
76        fi
77    done
78  fi
79  if [ -z "$fetch_bin" ]; then
80    echo "ERROR: neither wget nor curl found!"
81    exit
82  fi
83fi
84
85#Checksummer selection
86md5sum=
87
88for i in md5 md5sum /usr/local/bin/md5sum gmd5sum /usr/sfw/bin/md5sum /opt/sfw/bin/gmd5sum /opt/local/bin/md5sum; do
89    if [ "$i" = "md5" ]; then
90        eval "$i -x" > /dev/null 2>&1
91    else
92        eval "$i --version" > /dev/null 2>&1
93    fi
94    ret=$?
95    if [ $ret -eq 0 ]; then
96        md5sum=$i
97        echo found md5sum at `which $md5sum`
98        break 2
99    fi
100done
101
102if [ "$md5sum" = "md5" ]; then
103    md5special=-r
104fi
105
106if [ -z "$md5sum" ]; then
107    echo "Warning: no md5sum: found!"
108fi
109
110start_dir=`pwd`
111logfile=$TARFILE_LOCATION/fetch.log
112date >> $logfile
113
114# Create and go to a temporary directory under the tar file destination.
115mkdir -p $TARFILE_LOCATION/tmp
116cd $TARFILE_LOCATION/tmp
117
118
119basename ()
120{
121    echo $1 | sed "s/^\(.*\/\)//"
122}
123
124
125#
126# Download a file from a URL and add its md5 checksum to its name.
127#
128download ()
129{
130    local URL=$1
131
132    if [ -n "$URL" ]; then
133        local basename=$(basename $URL)
134        local candidate=$(find "$TARFILE_LOCATION" -type f -name "*-$basename")
135        if [ -n "$candidate" ]; then
136            echo "$basename is already present ($candidate)"
137        else
138		    echo fetching $basename
139	        $fetch_bin $fetch_args $URL 2>&1 | tee -a $logfile
140
141            if [ $? -ne 0 ]; then
142                echo "download failed"
143                mv $basename ${basename}_broken
144                failed="$failed $i"
145            elif [ -f "$basename" -a -n "$md5sum" ]; then
146                local sum=`$md5sum $md5special $basename | sed "s/ .*//"`
147                mv $basename "$TARFILE_LOCATION/$sum-$basename"
148                echo "added md5 sum $sum"
149            fi
150        fi
151    fi
152}
153
154#
155# Download a file from a URL and check its md5 sum to the one that is part of its name.
156#
157download_and_check ()
158{
159    local URL=$1
160
161    if [ -n "$URL" ]; then
162        local basename=$(basename $URL)
163        if [ -f "$TARFILE_LOCATION/$basename" ]; then
164            echo "$basename is already present"
165        else
166		    echo "fetching $basename"
167	        $fetch_bin $fetch_args $URL 2>&1 | tee -a $logfile
168
169            if [ $? -ne 0 ]; then
170                echo "download failed"
171                mv $basename ${basename}_broken
172                failed="$failed $i"
173            elif [ -f "$basename" -a -n "$md5sum" ]; then
174                local sum=`$md5sum $md5special $basename | sed "s/ .*//"`
175                local sum_in_name=`echo $basename | sed "s/-.*//"`
176                if [ "$sum" != "$sum_in_name" ]; then
177                    echo checksum failure for $basename 2>&1 | tee -a $logfile
178                    failed="$failed $basename"
179                    mv $basename ${basename}_broken
180                fi
181                mv $basename "$TARFILE_LOCATION/$basename"
182            fi
183        fi
184    fi
185}
186
187echo "downloading tar balls to $TARFILE_LOCATION"
188
189while read line ; do
190    # Remove leading and trailing space and comments
191    line=`echo $line | sed 's/^\s*//;s/\s*$//;s/\s*#.*$//'`
192    case $line in
193        # Ignore empty lines.
194        '')
195            ;;
196
197        # When a URL ends in a / then it is taken as a partial URL
198        # to which the following lines will be appended.
199        ftp:\/\/*\/ | http:\/\/*\/)
200            UrlHead=$line
201            echo $UrlHead
202            ;;
203
204        # A full URL represents a single file which is downloaded.
205        ftp:\/\/* | http:\/\/*)
206            download $line
207            ;;
208
209        # If the line starts with the name of an environment variable than the file is
210        # downloaded only when the variable evaluates to YES.
211        [A-Z0-9_]*:*)
212            prefix=`echo $line | sed 's/:.*$//'`
213            if [ -n "$prefix" ]; then
214                eval value=\$$prefix
215                if [ "x$value" = "xYES" ]; then
216                    line=`echo $line | sed 's/^.*://'`
217                    download_and_check $UrlHead$line
218                fi
219            fi
220            ;;
221
222        # Any other line is interpreted as the second part of a partial URL.
223        # It is appended to UrlHead and then downloaded.
224        *)
225            download_and_check $UrlHead$line
226            ;;
227    esac
228done < "$file_list_name"
229
230
231# Special handling of dmake
232if [ -n "$DMAKE_URL" -a ! -x "$SOLARENV/$OUTPATH/bin/dmake$EXEEXT" ]; then
233    download $DMAKE_URL
234fi
235
236# Special handling of epm-3.7
237# Basically just a download of the epm archive.
238# When its name contains "-source" than that part is removed.
239epm_archive_tail=`echo $(basename $EPM_URL) | sed 's/-source//'`
240epm_archive_name=$(find "$TARFILE_LOCATION" -type f -name "*-$epm_archive_tail")
241if [ -n "$EPM_URL" -a ! -x "$SOLARENV/$OUTPATH/bin/epm$EXEEXT" -a -z "$epm_archive_name" ]; then
242    download $EPM_URL
243    archive_name=$(find "$TARFILE_LOCATION" -type f -name "*-epm-3.7-source*")
244    if [ -n "$archive_name" ]; then
245        epm_archive_name=`echo $archive_name | sed 's/-source//'`
246        mv "$archive_name" "$epm_archive_name"
247    fi
248fi
249
250if [ ! -z "$failed" ]; then
251    echo
252    echo ERROR: failed on:
253    for i in $failed ; do
254        echo $i
255    done
256    exit 1
257fi
258
259