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