xref: /AOO41X/main/fetch_tarballs.sh (revision 0b0f4a2b1c442cef10a0cbba8b506291c1802637)
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
54bd677bc4SPedro Giffunielse
55bd677bc4SPedro 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
569d275db6SAndre Fischer    eval "$wg --version" > /dev/null 2>&1
57cdf0e10cSrcweir    ret=$?
58cdf0e10cSrcweir    if [ $ret -eq 0 ]; then
59bd677bc4SPedro Giffuni        fetch_bin=$wg
60bd677bc4SPedro Giffuni	    fetch_args="-nv -N"
61a6ff3988SAndre Fischer        echo found wget at `which $fetch_bin`
62cdf0e10cSrcweir        break 2
63cdf0e10cSrcweir    fi
64cdf0e10cSrcweir  done
65bd677bc4SPedro Giffuni  if [ -z "$fetch_bin" ]; then
66bd677bc4SPedro 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
67cdf0e10cSrcweir    # mac curl returns "2" on --version
68cdf0e10cSrcweir    #    eval "$i --version" > /dev/null 2>&1
69cdf0e10cSrcweir    #    ret=$?
70cdf0e10cSrcweir    #    if [ $ret -eq 0 ]; then
719d275db6SAndre Fischer        if [ -x $c ]; then
72bd677bc4SPedro Giffuni            fetch_bin=$c
73bd677bc4SPedro Giffuni	        fetch_args="$file_date_check -O"
74a6ff3988SAndre Fischer            echo found curl at `which $fetch_bin`
75cdf0e10cSrcweir            break 2
76cdf0e10cSrcweir        fi
77cdf0e10cSrcweir    done
78cdf0e10cSrcweir  fi
79bd677bc4SPedro Giffuni  if [ -z "$fetch_bin" ]; then
80cdf0e10cSrcweir    echo "ERROR: neither wget nor curl found!"
81cdf0e10cSrcweir    exit
82cdf0e10cSrcweir  fi
83bd677bc4SPedro Giffunifi
84bd677bc4SPedro Giffuni
85bd677bc4SPedro Giffuni#Checksummer selection
86bd677bc4SPedro Giffunimd5sum=
87cdf0e10cSrcweir
88cdf0e10cSrcweirfor i in md5 md5sum /usr/local/bin/md5sum gmd5sum /usr/sfw/bin/md5sum /opt/sfw/bin/gmd5sum /opt/local/bin/md5sum; do
89cdf0e10cSrcweir    if [ "$i" = "md5" ]; then
90cdf0e10cSrcweir        eval "$i -x" > /dev/null 2>&1
91cdf0e10cSrcweir    else
92cdf0e10cSrcweir        eval "$i --version" > /dev/null 2>&1
93cdf0e10cSrcweir    fi
94cdf0e10cSrcweir    ret=$?
95cdf0e10cSrcweir    if [ $ret -eq 0 ]; then
96cdf0e10cSrcweir        md5sum=$i
97a6ff3988SAndre Fischer        echo found md5sum at `which $md5sum`
98cdf0e10cSrcweir        break 2
99cdf0e10cSrcweir    fi
100cdf0e10cSrcweirdone
101cdf0e10cSrcweir
102cdf0e10cSrcweirif [ "$md5sum" = "md5" ]; then
103cdf0e10cSrcweir    md5special=-r
104cdf0e10cSrcweirfi
105cdf0e10cSrcweir
106cdf0e10cSrcweirif [ -z "$md5sum" ]; then
107cdf0e10cSrcweir    echo "Warning: no md5sum: found!"
108cdf0e10cSrcweirfi
109cdf0e10cSrcweir
110cdf0e10cSrcweirstart_dir=`pwd`
111cdf0e10cSrcweirlogfile=$TARFILE_LOCATION/fetch.log
112cdf0e10cSrcweirdate >> $logfile
113cdf0e10cSrcweir
114fb6b49d1SJürgen Schmidt# Create and go to a temporary directory under the tar file destination.
115cdf0e10cSrcweirmkdir -p $TARFILE_LOCATION/tmp
116cdf0e10cSrcweircd $TARFILE_LOCATION/tmp
117fb6b49d1SJürgen Schmidt
118fb6b49d1SJürgen Schmidt
119e4af8f11SPedro Giffunibasename ()
120a6ff3988SAndre Fischer{
121a6ff3988SAndre Fischer    echo $1 | sed "s/^\(.*\/\)//"
122a6ff3988SAndre Fischer}
123fb6b49d1SJürgen Schmidt
124a6ff3988SAndre Fischer
125a6ff3988SAndre Fischer#
126a6ff3988SAndre Fischer# Download a file from a URL and add its md5 checksum to its name.
127a6ff3988SAndre Fischer#
128e4af8f11SPedro Giffunidownload ()
129a6ff3988SAndre Fischer{
130a6ff3988SAndre Fischer    local URL=$1
131a6ff3988SAndre Fischer
132a6ff3988SAndre Fischer    if [ -n "$URL" ]; then
133a6ff3988SAndre Fischer        local basename=$(basename $URL)
134a6ff3988SAndre Fischer        local candidate=$(find "$TARFILE_LOCATION" -type f -name "*-$basename")
135a6ff3988SAndre Fischer        if [ -n "$candidate" ]; then
136a6ff3988SAndre Fischer            echo "$basename is already present ($candidate)"
137a6ff3988SAndre Fischer        else
138a6ff3988SAndre Fischer		    echo fetching $basename
139a6ff3988SAndre Fischer	        $fetch_bin $fetch_args $URL 2>&1 | tee -a $logfile
140a6ff3988SAndre Fischer
141a6ff3988SAndre Fischer            if [ $? -ne 0 ]; then
142a6ff3988SAndre Fischer                echo "download failed"
143a6ff3988SAndre Fischer                mv $basename ${basename}_broken
144fb6b49d1SJürgen Schmidt                failed="$failed $i"
145a6ff3988SAndre Fischer            elif [ -f "$basename" -a -n "$md5sum" ]; then
146a6ff3988SAndre Fischer                local sum=`$md5sum $md5special $basename | sed "s/ .*//"`
147a6ff3988SAndre Fischer                mv $basename "$TARFILE_LOCATION/$sum-$basename"
148a6ff3988SAndre Fischer                echo "added md5 sum $sum"
149fb6b49d1SJürgen Schmidt            fi
150fb6b49d1SJürgen Schmidt        fi
151a6ff3988SAndre Fischer    fi
152a6ff3988SAndre Fischer}
153fb6b49d1SJürgen Schmidt
154a6ff3988SAndre Fischer#
155a6ff3988SAndre Fischer# Download a file from a URL and check its md5 sum to the one that is part of its name.
156a6ff3988SAndre Fischer#
157e4af8f11SPedro Giffunidownload_and_check ()
158a6ff3988SAndre Fischer{
159a6ff3988SAndre Fischer    local URL=$1
160a6ff3988SAndre Fischer
161a6ff3988SAndre Fischer    if [ -n "$URL" ]; then
162a6ff3988SAndre Fischer        local basename=$(basename $URL)
163a6ff3988SAndre Fischer        if [ -f "$TARFILE_LOCATION/$basename" ]; then
164a6ff3988SAndre Fischer            echo "$basename is already present"
165a6ff3988SAndre Fischer        else
166a6ff3988SAndre Fischer		    echo "fetching $basename"
167a6ff3988SAndre Fischer	        $fetch_bin $fetch_args $URL 2>&1 | tee -a $logfile
168a6ff3988SAndre Fischer
169a6ff3988SAndre Fischer            if [ $? -ne 0 ]; then
170a6ff3988SAndre Fischer                echo "download failed"
171a6ff3988SAndre Fischer                mv $basename ${basename}_broken
172a6ff3988SAndre Fischer                failed="$failed $i"
173a6ff3988SAndre Fischer            elif [ -f "$basename" -a -n "$md5sum" ]; then
174a6ff3988SAndre Fischer                local sum=`$md5sum $md5special $basename | sed "s/ .*//"`
175a6ff3988SAndre Fischer                local sum_in_name=`echo $basename | sed "s/-.*//"`
176a6ff3988SAndre Fischer                if [ "$sum" != "$sum_in_name" ]; then
177a6ff3988SAndre Fischer                    echo checksum failure for $basename 2>&1 | tee -a $logfile
178a6ff3988SAndre Fischer                    failed="$failed $basename"
179a6ff3988SAndre Fischer                    mv $basename ${basename}_broken
180a6ff3988SAndre Fischer                fi
181a6ff3988SAndre Fischer                mv $basename "$TARFILE_LOCATION/$basename"
182a6ff3988SAndre Fischer            fi
183a6ff3988SAndre Fischer        fi
184a6ff3988SAndre Fischer    fi
185a6ff3988SAndre Fischer}
186a6ff3988SAndre Fischer
187a6ff3988SAndre Fischerecho "downloading tar balls to $TARFILE_LOCATION"
188a6ff3988SAndre Fischer
189a6ff3988SAndre Fischerwhile read line ; do
190a6ff3988SAndre Fischer    # Remove leading and trailing space and comments
191*68d0ce22SAndre Fischer    line=`echo $line | sed 's/^\s*//;s/\s*$//;s/\s*#.*$//'`
192a6ff3988SAndre Fischer    case $line in
193a6ff3988SAndre Fischer        # Ignore empty lines.
194a6ff3988SAndre Fischer        '')
195a6ff3988SAndre Fischer            ;;
196a6ff3988SAndre Fischer
197a6ff3988SAndre Fischer        # When a URL ends in a / then it is taken as a partial URL
198a6ff3988SAndre Fischer        # to which the following lines will be appended.
199a6ff3988SAndre Fischer        ftp:\/\/*\/ | http:\/\/*\/)
200a6ff3988SAndre Fischer            UrlHead=$line
201a6ff3988SAndre Fischer            echo $UrlHead
202a6ff3988SAndre Fischer            ;;
203a6ff3988SAndre Fischer
204a6ff3988SAndre Fischer        # A full URL represents a single file which is downloaded.
205a6ff3988SAndre Fischer        ftp:\/\/* | http:\/\/*)
206a6ff3988SAndre Fischer            download $line
207a6ff3988SAndre Fischer            ;;
208a6ff3988SAndre Fischer
2093c6578e9SAndre Fischer        # If the line starts with the name of an environment variable than the file is
2103c6578e9SAndre Fischer        # downloaded only when the variable evaluates to YES.
2113c6578e9SAndre Fischer        [A-Z0-9_]*:*)
2123c6578e9SAndre Fischer            prefix=`echo $line | sed 's/:.*$//'`
2133c6578e9SAndre Fischer            if [ -n "$prefix" ]; then
2143c6578e9SAndre Fischer                eval value=\$$prefix
2153c6578e9SAndre Fischer                if [ "x$value" = "xYES" ]; then
2163c6578e9SAndre Fischer                    line=`echo $line | sed 's/^.*://'`
2173c6578e9SAndre Fischer                    download_and_check $UrlHead$line
2183c6578e9SAndre Fischer                fi
2193c6578e9SAndre Fischer            fi
2203c6578e9SAndre Fischer            ;;
2213c6578e9SAndre Fischer
222a6ff3988SAndre Fischer        # Any other line is interpreted as the second part of a partial URL.
223a6ff3988SAndre Fischer        # It is appended to UrlHead and then downloaded.
224a6ff3988SAndre Fischer        *)
225a6ff3988SAndre Fischer            download_and_check $UrlHead$line
226a6ff3988SAndre Fischer            ;;
227a6ff3988SAndre Fischer    esac
228a6ff3988SAndre Fischerdone < "$file_list_name"
229a6ff3988SAndre Fischer
230a6ff3988SAndre Fischer
231a6ff3988SAndre Fischer# Special handling of dmake
232a6ff3988SAndre Fischerif [ -n "$DMAKE_URL" -a ! -x "$SOLARENV/$OUTPATH/bin/dmake$EXEEXT" ]; then
233a6ff3988SAndre Fischer    download $DMAKE_URL
234a6ff3988SAndre Fischerfi
235fb6b49d1SJürgen Schmidt
236b6b33854SAndre Fischer# Special handling of epm-3.7
237b6b33854SAndre Fischer# Basically just a download of the epm archive.
238b6b33854SAndre Fischer# When its name contains "-source" than that part is removed.
239b6b33854SAndre Fischerepm_archive_tail=`echo $(basename $EPM_URL) | sed 's/-source//'`
240b6b33854SAndre Fischerepm_archive_name=$(find "$TARFILE_LOCATION" -type f -name "*-$epm_archive_tail")
241b6b33854SAndre Fischerif [ -n "$EPM_URL" -a ! -x "$SOLARENV/$OUTPATH/bin/epm$EXEEXT" -a -z "$epm_archive_name" ]; then
242a6ff3988SAndre Fischer    download $EPM_URL
243b6b33854SAndre Fischer    archive_name=$(find "$TARFILE_LOCATION" -type f -name "*-epm-3.7-source*")
244b6b33854SAndre Fischer    if [ -n "$archive_name" ]; then
245b6b33854SAndre Fischer        epm_archive_name=`echo $archive_name | sed 's/-source//'`
246b6b33854SAndre Fischer        mv "$archive_name" "$epm_archive_name"
247a6ff3988SAndre Fischer    fi
248b6b33854SAndre Fischerfi
249cdf0e10cSrcweir
250cdf0e10cSrcweirif [ ! -z "$failed" ]; then
251cdf0e10cSrcweir    echo
252cdf0e10cSrcweir    echo ERROR: failed on:
253cdf0e10cSrcweir    for i in $failed ; do
254cdf0e10cSrcweir        echo $i
255cdf0e10cSrcweir    done
256cdf0e10cSrcweir    exit 1
257cdf0e10cSrcweirfi
258cdf0e10cSrcweir
259