mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

822 lines
22 KiB

  1. #!/bin/bash
  2. #
  3. # --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  4. #
  5. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  6. # Please add additional copyright information _after_ the line containing
  7. # the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  8. # the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  9. #
  10. # ROCK Linux: rock-src/scripts/Download
  11. # ROCK Linux is Copyright (C) 1998 - 2003 Clifford Wolf
  12. #
  13. # This program is free software; you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License as published by
  15. # the Free Software Foundation; either version 2 of the License, or
  16. # (at your option) any later version. A copy of the GNU General Public
  17. # License can be found at Documentation/COPYING.
  18. #
  19. # Many people helped and are helping developing ROCK Linux. Please
  20. # have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  21. # file for details.
  22. #
  23. # --- ROCK-COPYRIGHT-NOTE-END ---
  24. #
  25. # Run this command from the ROCK directory as ./scripts/Download [ options ]
  26. #
  27. # It enables you to download source files as described in the package
  28. # definitions (optionally using a mirroring 'cache' server).
  29. #
  30. # This script also allows for checksum display/validation.
  31. #
  32. # Key to understanding this script is that all file lists get broken
  33. # down to a single file download using ./scripts/Download filename.
  34. umask 022
  35. if [ "$1" = '--help' ] ; then
  36. { echo
  37. echo "Usage:"
  38. echo
  39. echo " ./scripts/Download [options] [ Filename(s) ]"
  40. echo " ./scripts/Download [options] -pattern Pattern(s)"
  41. echo " ./scripts/Download [options] -package Package(s)"
  42. echo " ./scripts/Download [options] -repository Repositories"
  43. echo " ./scripts/Download [options] { -all | -required }"
  44. echo
  45. echo " Where [options] is an alias for:"
  46. echo " [ -cfg <config> ] [ -nock ] [ -alt-dir <AlternativeDirectory> ]"
  47. echo " [ -mirror <URL> | -check ] [ -try-questionable ] [ -notimeout ]"
  48. echo " [ -longtimeout ] [ -curl-opt <curl-option>[:<curl-option>[:..]] ]"
  49. echo " [ -proxy <server>[:<port>] ] [ -proxy-auth <username>[:<password>] ]"
  50. echo
  51. echo " On default, this script auto-detects the best ROCK Linux mirror."
  52. echo
  53. echo " Mirrors can also be a local directories in the form of 'file:///<dir>'."
  54. echo
  55. echo " ./scripts/Download -mk-cksum Filename(s)"
  56. echo " ./scripts/Download [ -list | -list-unknown | -list-missing | -list-cksums ]"
  57. echo
  58. echo "See '-mirror none' output for help on bypassing the official mirrors."
  59. echo ; } >&2
  60. exit 1
  61. fi
  62. # -mk-cksum mode (display ROCK type package checksum): it
  63. # displays the checksum ROCK validates against.
  64. #
  65. # Currently bz2, tbz2, gz, tgz, Z are unpacked
  66. #
  67. if [ "$1" = -mk-cksum ] ; then
  68. shift
  69. for x ; do
  70. echo -n "$x: "
  71. if [ ! -f "$x" ] ; then
  72. echo "No such file."
  73. elif [ "${x%.bz2}" != "$x" -o "${x%.tbz2}" != "$x" ] ; then
  74. bunzip2 < "$x" | cksum | cut -f1 -d' '
  75. elif [ "${x%.gz}" != "$x" -o "${x%.tgz}" != "$x" ] ; then
  76. gunzip < "$x" | cksum | cut -f1 -d' '
  77. elif [ "${x%.Z}" != "$x" ] ; then
  78. uncompress < "$x" | cksum | cut -f1 -d' '
  79. else
  80. cksum < "$x" | cut -f1 -d' '
  81. fi
  82. done
  83. exit 1
  84. fi
  85. # Handle options passed on the command line
  86. #
  87. mkdir -p src/ download/ ; config=default
  88. this_is_the_2nd_run=0
  89. mirror='' ; checkonly=0 ; altdir='' ; loop=1
  90. tryques=0 ; nocheck=0 ; options='-this_is_the_2nd_run '
  91. notimeout=0 ; curl_options='--disable-epsv --location'
  92. #
  93. while [ $loop -eq 1 ] ; do
  94. case "$1" in
  95. -this_is_the_2nd_run)
  96. this_is_the_2nd_run=1
  97. shift ;;
  98. -cfg)
  99. options="$options -cfg $2"
  100. config="$2" ; shift ; shift ;;
  101. -nock)
  102. # -nock skips checksum checking (don't use lightly)
  103. options="$options -nock"
  104. nocheck=1 ; shift ;;
  105. -mirror)
  106. # -mirror uses a mirror for finding source files
  107. if [ "$2" = none ]; then
  108. echo
  109. echo "The option '-mirror none' is not supported anymore!"
  110. echo
  111. echo "You may 'echo none > download/Mirror' if you really"
  112. echo "want to use the original download resources. However, this"
  113. echo "is not supported and if such a download fails, this is not"
  114. echo "a bug in ROCK Linux and doesn't neccessarily needs fixing."
  115. echo
  116. exit 1;
  117. else
  118. mkdir -p download
  119. echo "$2" > download/Mirror
  120. options="$options -mirror $2"
  121. mirror="$2"
  122. fi
  123. shift ; shift ;;
  124. -check)
  125. # -check just validates the file using the checksum
  126. options="$options -check"
  127. checkonly=1 ; shift ;;
  128. -notimeout)
  129. # don't add timeout curl options
  130. options="$options -notimeout"
  131. notimeout=2 ; shift ;;
  132. -longtimeout)
  133. # don't add timeout curl options
  134. options="$options -longtimeout"
  135. notimeout=1 ; shift ;;
  136. -curl-opt)
  137. # additional curl options
  138. options="$options -curl-opt $2"
  139. curl_options="$curl_options `echo $2 | tr : ' '`"
  140. shift ; shift ;;
  141. -proxy)
  142. # proxy option for curl
  143. mkdir -p download
  144. echo -n "$2" > download/Proxy
  145. options="$options -proxy $2"
  146. shift ; shift ;;
  147. -proxy-auth)
  148. # proxy authentication for curl - can be seen with ps!
  149. mkdir -p download
  150. echo -n "$2" > download/Proxy-auth
  151. chmod 600 download/Proxy-auth
  152. options="$options -proxy-auth $2"
  153. shift ; shift ;;
  154. -alt-dir)
  155. # check for an alternative directory where to search for
  156. # package source tarballs
  157. options="$options -alt-dir $2"
  158. altdir=$2 ; shift ; shift ;;
  159. -try-questionable)
  160. # also try to download questionable URLs
  161. options="$options -try-questionable"
  162. tryques=1 ; shift ;;
  163. *)
  164. loop=0 ;;
  165. esac
  166. done
  167. if [ $notimeout -eq 0 ] ; then
  168. curl_options="$curl_options -y 10 -Y 10 --connect-timeout 60"
  169. fi
  170. if [ $notimeout -eq 1 ] ; then
  171. curl_options="$curl_options -y 60 -Y 1 --connect-timeout 300"
  172. fi
  173. # cksum_chk filename cksum origfile
  174. #
  175. # This function verifies the checksum. If it fails it renames the file
  176. # to file.chksum-err and returns failure.
  177. #
  178. # It seams like the [ ] command has problems with comparing high numbers.
  179. # That's why I'm using a text comparison here.
  180. #
  181. # Not doing anything if checksum is '0' or a text of 'X'.
  182. #
  183. cksum_chk() {
  184. y="`echo $2 | sed 's,^0*,,;'`"
  185. [ $nocheck = 1 -o -z "$y" -o -z "${2//X/}" ] && return 0
  186. x="`cksum "$1" | cut -f1 -d' ' | sed 's,^0*,,;'`"
  187. if [ "$x" != "$y" ] ; then
  188. # Add .cksum-err extension to filename:
  189. echo "Cksum ERROR: $3.cksum-err ($x)"
  190. mv "$3" "$3.cksum-err" ; return 1
  191. fi
  192. return 0
  193. }
  194. # Autodetect best Mirror and safe url in $mirror
  195. #
  196. detect_mirror() {
  197. if [ -f download/Mirror ] ; then
  198. mirror="`cat download/Mirror`"
  199. if [ -z "$mirror" -o "$mirror" = "none" ] ; then
  200. echo "INFO: Found download/Mirror: none" \
  201. "(use the original download locations)"
  202. else
  203. echo "INFO: Found cached mirror URL in download/Mirror:"
  204. echo "INFO: $mirror"
  205. fi
  206. echo "INFO: To force a new mirror auto-detection, remove download/Mirror."
  207. else
  208. echo "INFO: Auto-detecting best mirror ..."
  209. eval "$(egrep '^(rockver)=' scripts/parse-config)"
  210. echo "INFO: Downloading mirror-list from www.rocklinux.net."
  211. curl -s -S $curl_options -o src/Download-Mirror-List \
  212. "http://www.rocklinux.net/mirrors.cgi?$rockver"
  213. bestval=0 ; result='No Mirror Found!'
  214. while read mirror_name ; do
  215. if [ "${mirror_name#=}" != "$mirror_name" ] ; then
  216. mirror_name="${mirror_name#= }"
  217. mirror_name="${mirror_name% =}"
  218. read mirror_url
  219. echo -n "INFO: Testing <$mirror_name> ..."
  220. val="$(curl -s $curl_options -m 20 "${mirror_url%/}/DOWNTEST" \
  221. -w "ok %{speed_download}" -o /dev/null)"
  222. if [ "$val" = "${val#ok }" -o "$val" = "ok 0.000" ] ; then
  223. echo " error"
  224. else
  225. xval=`echo ${val#ok } | tr -d .` ; echo " $val"
  226. if [ "$xval" -gt "$bestval" ] ; then
  227. bestval=$xval ; mirror="${mirror_url%/}"
  228. result="Using mirror <$mirror>."
  229. fi
  230. fi
  231. fi
  232. done < src/Download-Mirror-List
  233. echo $mirror > download/Mirror
  234. echo "INFO: $result"
  235. fi
  236. }
  237. # download_file local-filename download-location cksum
  238. #
  239. # This function decides if download directly or from a mirror,
  240. # validates checksum, etc.
  241. # Calls download_file_now to do the actual download.
  242. #
  243. download_file() {
  244. # Init
  245. #
  246. gzfile="$1" ; location="$2" ; cksum="$3"
  247. # Make src directory for creating tar balls
  248. mkdir -p src/
  249. # Tarball file name: (if you change this one - also adapt Create-ISO)
  250. bzfile="`echo "$gzfile" | sed 's,\.\(t\?\)\(gz\|Z\)$,.\1bz2,'`"
  251. # Lock file name:
  252. lkfile="src/down.lockfile.`echo $bzfile | tr / -`"
  253. # Check if it's already there
  254. #
  255. [ -s "$bzfile" -a $checkonly != 1 ] && return 0
  256. # Make locking
  257. #
  258. if [ -s "$lkfile" ]; then
  259. echo "Found $lkfile -> skip download."
  260. return 0
  261. fi
  262. trap 'rm -f "$lkfile"' INT
  263. echo $$ > "$lkfile"
  264. # Check if we only like to test the cksum(s)
  265. #
  266. if [ $checkonly = 1 ] ; then
  267. gzfile="$bzfile"
  268. if [ ! -f "$bzfile" ] ; then
  269. echo "File missing: $bzfile"
  270. rm -f "$lkfile" ; trap INT ; return 1
  271. fi
  272. if [ -z "${cksum##X*}" ] ; then
  273. echo "No checksum (ignore): $bzfile"
  274. rm -f "$lkfile" ; trap INT ; return 1
  275. fi
  276. if [ "$cksum" -eq 0 ] ; then
  277. echo "No checksum (missing): $bzfile"
  278. rm -f "$lkfile" ; trap INT ; return 1
  279. fi
  280. elif [ -s "$gzfile" ] ; then
  281. echo ; echo "Already downloaded $gzfile ..."
  282. else
  283. echo ; echo "Downloading $gzfile ..."
  284. # Existing *.cksum-err
  285. #
  286. if [ -s "$gzfile.cksum-err" ] ; then
  287. # cksum-err file alread exists:
  288. echo "ERROR: Found $gzfile.cksum-err."
  289. echo "ERROR: That means that we downloaded the" \
  290. "file already and it had an"
  291. echo "ERROR: incorrect checksum. Remove the" \
  292. "*.cksum-err file to force a"
  293. echo "ERROR: new download of that file."
  294. rm -f "$lkfile" ; trap INT ; return 1
  295. fi
  296. # Existing *.extck-err
  297. #
  298. if [ -s "$gzfile.extck-err" ] ; then
  299. # extck-err file alread exists:
  300. echo "ERROR: Found $gzfile.extck-err."
  301. echo "ERROR: That means that we downloaded the" \
  302. "file already and it's content"
  303. echo "ERROR: did not match it's filename extension." \
  304. "Remove the *.extck-err file"
  305. echo "ERROR: to force a new download of that file."
  306. rm -f "$lkfile" ; trap INT ; return 1
  307. fi
  308. # Questionable URL
  309. #
  310. if [ "$location" != "${location#\?}" ] ; then
  311. if [ "$tryques" = 0 ] ; then
  312. echo "ERROR: URL is marked as questionable." \
  313. "Not downloading this file."
  314. rm -f "$lkfile" ; trap INT ; return 1
  315. else
  316. echo "WARNING: URL is marked as questionable." \
  317. "Downloading it anyways."
  318. location="${location#\?}"
  319. fi
  320. fi
  321. # Make directory (if required)
  322. #
  323. if [ ! -d `dirname "$bzfile"` ] ; then
  324. mkdir -p `dirname "$bzfile"`
  325. fi
  326. # Alternative Directory
  327. #
  328. if [ "$altdir" ] ; then
  329. altfile=$(find $altdir/ -name `basename $bzfile` | head -n 1)
  330. else
  331. altfile=""
  332. fi
  333. if [ "$altfile" ] ; then
  334. echo "Found `basename $bzfile` as $altfile."
  335. cp -lv $altfile $bzfile ; gzfile="$bzfile"
  336. else
  337. # Mirroring
  338. #
  339. mirror="$( cat download/Mirror )"
  340. if [ "$mirror" -a "$mirror" != "none" ] ; then
  341. # try to use mirror
  342. if ! download_file_now "!$mirror/${bzfile#download/}" $bzfile $bzfile; then
  343. # oops... so try direct (original URL)
  344. echo "INFO: download from mirror failed, trying original URL."
  345. download_file_now $location $gzfile $bzfile
  346. else
  347. gzfile="$bzfile"
  348. fi
  349. else
  350. # don't want to use mirror
  351. download_file_now $location $gzfile $bzfile
  352. fi
  353. fi
  354. if [ ! -s "$gzfile" ]; then
  355. rm -f "$lkfile" ; trap INT ; return 1
  356. fi
  357. fi
  358. # Convert a .gz to .bz2 and test checksum
  359. #
  360. if [ "$gzfile" != "$bzfile" ] ; then
  361. echo "bzip'ing + cksum-test: $gzfile"
  362. gunzip < "$gzfile" > src/down.$$.dat
  363. if cksum_chk src/down.$$.dat $cksum "$gzfile" ; then
  364. bzip2 < src/down.$$.dat > "$bzfile" ; rm -f "$gzfile"
  365. fi
  366. rm -f src/down.$$.dat
  367. # Execute a cksum test on a bzip2 file
  368. #
  369. elif [ "${gzfile%.bz2}" != "$gzfile" -o \
  370. "${gzfile%.tbz2}" != "$gzfile" ]
  371. then
  372. echo "cksum-test (bzip2): $bzfile"
  373. if [ $nocheck = 0 ] ; then
  374. bunzip2 < "$bzfile" > src/down.$$.dat
  375. cksum_chk src/down.$$.dat $cksum "$bzfile"
  376. fi
  377. rm -f src/down.$$.dat
  378. # Execute a cksum test on a raw data file
  379. #
  380. else
  381. echo "cksum-test (raw): $gzfile"
  382. cksum_chk "$gzfile" $cksum "$gzfile"
  383. fi
  384. # Free Lock and finish
  385. #
  386. rm -f "$lkfile" ; trap INT ; return 0
  387. }
  388. # download_file_now location remote_filename local_filename
  389. #
  390. # This function executes the actual download using curl.
  391. #
  392. download_file_now() {
  393. local location="$1" gzfile="$2" bzfile="$3"
  394. # Create URL
  395. #
  396. if [ "${location#!}" != "$location" ] ; then
  397. url="`echo "$location" | sed 's,!,,'`"
  398. else
  399. url="`echo "$location" | \
  400. sed 's,/[^/]*$,,'`/`echo $gzfile | sed 's,.*/,,'`"
  401. fi
  402. # Check for existing Error Log
  403. #
  404. if test -s src/Download-Errors &&
  405. grep -q " $url\$" src/Download-Errors ; then
  406. echo "ERROR: According to src/Download-Errors" \
  407. "we had already an error for that URL."
  408. echo "ERROR: So I'm not trying to download" \
  409. "it again (remove src/Download-Errors"
  410. echo "ERROR: if you want to force a retry)."
  411. return 1
  412. fi
  413. # Download
  414. #
  415. if [[ $url = cvs://* ]] ; then
  416. # cvs://mode:[login[:password]@]server[:port]:/path::module!revision/
  417. # btw, at least current cvs supports password at CVSROOT.
  418. url="${url#cvs://}"; url="${url%/*}"
  419. # cvs://mode:loc::module!date/
  420. #
  421. mode="${url%%:*}"; loc="${url#*:}"
  422. module="${loc##*::}"; loc="${loc%%::*}"
  423. revision="${module#*!}"; module="${module%%!*}"
  424. [[ $loc != *@* ]] && loc="anonymous@$loc"
  425. # everything after the first 'bang' (!) is analysed here
  426. # someday we could add more cvs options.
  427. #
  428. dat="$( echo $revision | \
  429. sed -n -e 's,\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\),-D \1,p' )"
  430. cvsdir="src/down.cvsdir.`echo $bzfile | tr / -`"
  431. saved_pwd=$PWD ; mkdir -p $cvsdir ; cd $cvsdir
  432. echo CVS $mode $loc $dat $module
  433. { [ $mode = ssh ] && export CVS_RSH=ssh
  434. [ $mode = pserver ] && loc=":pserver:$loc"
  435. # sometimes cvs wants to read ~/.cvspass just for fun ..
  436. touch $HOME/.cvspass
  437. # for ssh we need some way to quitely accept the key ...
  438. echo cvs -z9 -Q -d $loc checkout $dat -P $module
  439. if ! cvs -z9 -Q -d $loc checkout $dat -P $module
  440. then touch .cvs_error ; fi
  441. } &> .cvs_output &
  442. while fuser .cvs_output &> /dev/null ; do
  443. echo -ne `nice du -sh 2> /dev/null | cut -f1` 'downloaded from' \
  444. 'CVS archive so far...\r'
  445. sleep 3
  446. done
  447. echo `du -sh 2> /dev/null | cut -f1` 'downloaded from' \
  448. 'CVS archive (download finished).'
  449. if [ ! -f .cvs_error ] ; then
  450. cd `dirname $module`
  451. dir="`echo "$bzfile" | sed s/\.tar\.bz2$//`"
  452. dir="`basename $dir`"
  453. mv `basename $module` $dir
  454. tar --owner root --group root \
  455. --use-compress-program=bzip2 \
  456. -cf $dir.tar.bz2 $dir
  457. mv $dir.tar.bz2 $saved_pwd/$bzfile
  458. cd $saved_pwd ; rm -rf $cvsdir
  459. else
  460. cat .cvs_output
  461. cd $saved_pwd ; rm -rf $cvsdir
  462. echo ERROR: CVS $dat $loc $module \
  463. returned an error.
  464. echo "0 $gzfile $url" >> src/Download-Errors
  465. fi
  466. else
  467. if [ -s "$gzfile.incomplete" ] ; then
  468. echo "INFO: Trying to resume previous download .."
  469. resume="-C -"
  470. else
  471. resume=""
  472. fi
  473. curl -w '\rFinished downloading %{size_download} bytes in %{time_total} seconds (%{speed_download} bytes/sec). \n' -f --progress-bar $resume $curl_options "$url" -o "$gzfile.incomplete"
  474. curlret="$?"
  475. if [ "$resume" ] && \
  476. [ $curlret -eq 33 -o $curlret -eq 36 ] ; then
  477. echo "INFO: Resuming download not possible. ->" \
  478. "Overwriting old file."
  479. rm -f "$gzfile.incomplete"
  480. curl -w '\rFinished downloading %{size_download} bytes in %{time_total} seconds (%{speed_download} bytes/sec). \n' -f --progress-bar $curl_options "$url" -o "$gzfile.incomplete"
  481. curlret="$?"
  482. fi
  483. if [ $curlret -ne 0 ] ; then
  484. case "$curlret" in
  485. 18)
  486. echo "WARNING: Got only some of the" \
  487. "file. A re-run of $0"
  488. echo "WARNING: is required to complete" \
  489. "the download." ;;
  490. 130)
  491. echo -e '\rWARNING: CURL got a SIGINT' \
  492. "(someone pressed Ctrl-C). A re-run of"
  493. echo "WARNING: $0 is required to complete" \
  494. "the download." ; sleep 1 ;;
  495. *)
  496. echo "$curlret $gzfile $url" \
  497. >> src/Download-Errors
  498. echo -e '\rERROR: CURL Returned Error' \
  499. "$curlret. Please read" \
  500. "the curl manpage." ;;
  501. esac
  502. return 1
  503. elif [ ! -s "$gzfile.incomplete" ] ; then
  504. echo "0 $gzfile $url" >> src/Download-Errors
  505. echo "ERROR: CURL returned success but" \
  506. "we have no data!"
  507. curlret=1
  508. else
  509. case "$gzfile" in
  510. *.gz|*.tgz)
  511. typeexpr="gzip compressed data" ;;
  512. *.bz2|*.tbz2)
  513. typeexpr="bzip2 compressed data" ;;
  514. *.Z|*.tZ)
  515. typeexpr="compress'd data" ;;
  516. *.zip|*.jar)
  517. typeexpr="Zip archive data" ;;
  518. *.tar)
  519. typeexpr="tar archive" ;;
  520. *)
  521. echo "WARNING: Unkown file extension: $gzfile"
  522. typeexpr="." ;;
  523. esac
  524. if file "$gzfile.incomplete" | grep -v "$typeexpr"
  525. then
  526. echo "ERROR: File type does not match" \
  527. "filename ($typeexpr)!"
  528. mv "$gzfile.incomplete" "$gzfile.extck-err"
  529. else
  530. mv "$gzfile.incomplete" "$gzfile"
  531. fi
  532. fi
  533. fi
  534. }
  535. # handle_file filename
  536. #
  537. # This function fetches the checksum and download information
  538. # from the desc files and calls the download_file function.
  539. #
  540. handle_file() {
  541. # Handle pkg file (package/.../*.desc)
  542. #
  543. tree="`echo "$1" | cut -f2 -d/`"
  544. pkg="` echo "$1" | cut -f3 -d/`"
  545. file="`echo "$1" | cut -f4 -d/`"
  546. if grep -q "^\[D\].* $file " package/$tree/$pkg/$pkg.desc 2> /dev/null
  547. then
  548. grep "^\[D\].* $file " package/$tree/$pkg/$pkg.desc |
  549. while read tag cksum file url ; do
  550. download_file "$1" "$url" "$cksum"
  551. done
  552. return 0
  553. fi
  554. # Handle target file (target/.../download.txt)
  555. #
  556. target="`echo "$1" | cut -f2 -d/`"
  557. file="`echo "$1" | cut -f3- -d/`"
  558. if grep -q " $file " target/$target/download.txt 2> /dev/null
  559. then
  560. grep " $file " target/$target/download.txt |
  561. while read cksum file url ; do
  562. download_file "$1" "$url" "$cksum"
  563. done
  564. return 0
  565. fi
  566. # Handle misc file (scripts/miscdown.txt)
  567. #
  568. if [ "${1#download/misc/}" != "$1" ] ; then
  569. file="`echo "$1" | cut -f3- -d/`"
  570. if grep -q " $file " scripts/miscdown.txt
  571. then
  572. grep " $file " scripts/miscdown.txt |
  573. while read cksum file url ; do
  574. download_file "$1" "$url" "$cksum"
  575. done
  576. return 0
  577. fi
  578. fi
  579. # Can't handle file / file not found
  580. #
  581. return 1
  582. }
  583. list_cksums() {
  584. trap '' INT
  585. { grep -H '^\[D\] ' package/*/*/*.desc | tr '\t' ' ' | tr -s ' ' |
  586. sed -e 's,^package/,download/,;'
  587. grep -H '^[X0-9]' target/*/download.txt | tr '\t' ' ' | tr -s ' ' |
  588. sed -e 's,^target/,download/,; s,:,:[D] ,;'
  589. grep -H '^[X0-9]' scripts/miscdown.txt | tr '\t' ' ' | tr -s ' ' |
  590. sed 's,^scripts/,download/misc/,; s,:,:[D] ,;'
  591. } | sed 's,^\(.*/\)[^/:]*:[^ ]* \([X0-9]*\) ,\2 \1,;' | cut -f1,2 -d' '
  592. trap INT
  593. }
  594. list() {
  595. trap '' INT
  596. list_cksums | cut -f2- -d' '
  597. trap INT
  598. }
  599. list_unknown() {
  600. trap '' INT
  601. mkdir -p src/ ; list | sed 's,\.\(t\?\)\(gz\|Z\)$,.\1bz2,' > src/down.$$.lst
  602. ls download/{INDEX,README,DOWNTEST,LAST-UPDATE} \
  603. >> src/down.$$.lst 2> /dev/null
  604. find download/* -type f -o -type l | grep -v -e download/Mirror \
  605. -e download/Proxy -e download/Proxy-auth | \
  606. while read fn ; do
  607. grep -qx "$fn" src/down.$$.lst || echo "Unknown file: $fn"
  608. done
  609. rm -f src/down.$$.lst
  610. trap INT
  611. }
  612. list_missing() {
  613. trap '' INT
  614. list | sed 's,\.\(t\?\)\(gz\|Z\)$,.\1bz2,' | \
  615. while read fn ; do
  616. [ -f "$fn" ] || echo "$fn"
  617. done
  618. trap INT
  619. }
  620. pattern() {
  621. for pattern ; do
  622. echo "Processing pattern $pattern ..."
  623. list | while read fn ; do
  624. if [ "${fn##$pattern}" = '' ] ; then
  625. echo "Matched: $fn"
  626. $0 $options "$fn"
  627. fi
  628. done
  629. done
  630. }
  631. package() {
  632. for package ; do
  633. pattern "download/*/$package/*"
  634. done
  635. }
  636. repository() {
  637. for repository ; do
  638. pattern "download/$repository/*"
  639. done
  640. }
  641. required() {
  642. # Choosen config must exist
  643. #
  644. if [ ! -f config/$config/packages ]; then
  645. echo "ERROR: Config $config doesn't exist."
  646. echo "ERROR: try ./scripts/Config -cfg $config first."
  647. exit 1
  648. fi
  649. while read on a b tree pkg c ; do
  650. if [ "$on" = "X" ] ; then
  651. grep -H '^\[D\] ' package/$tree/$pkg/$pkg.desc > src/down.$$.lst
  652. while read tag cksum file url ; do
  653. download_file "download/$tree/$pkg/$file" "$url" "$cksum"
  654. done < src/down.$$.lst ; rm -f src/down.$$.lst
  655. fi
  656. done < config/$config/packages
  657. target=`grep '^export ROCKCFG_TARGET=' config/$config/config | \
  658. cut -f2 -d= | tr -d "'"`
  659. if [ -f target/$target/download.txt ] ; then
  660. while read cksum file url ; do
  661. download_file "download/$target/$file" "$url" "$cksum"
  662. done < target/$target/download.txt
  663. fi
  664. }
  665. single_files() {
  666. for file_name ; do
  667. file_name_gz="`echo "$file_name" | sed 's,\.\(t\?\)bz2$,.\1gz,'`"
  668. file_name_Z="`echo "$file_name" | sed 's,\.\(t\?\)bz2$,.\1Z,'`"
  669. if ! handle_file "$file_name" && \
  670. ! handle_file "$file_name_gz" && \
  671. ! handle_file "$file_name_Z"
  672. then
  673. echo "ERROR: Unknown file: $file_name."
  674. fi
  675. done
  676. }
  677. all() {
  678. if [ $checkonly = 1 ] ; then list
  679. else list_missing ; fi > src/down.$$.lst
  680. while read fn ; do single_files $fn ; done < src/down.$$.lst
  681. rm -f src/down.$$.lst
  682. }
  683. # Things to do only for downloading
  684. #
  685. if [ "${1:0:5}" != "-list" -a $checkonly = 0 ]; then
  686. # Set proxy information
  687. if [ -f download/Proxy ]; then
  688. proxy="`cat download/Proxy`"
  689. if [ "$proxy" ]; then
  690. curl_options="$curl_options --proxy $proxy"
  691. else
  692. echo "INFO: No proxy information... removing download/Proxy."
  693. rm download/Proxy
  694. fi
  695. fi
  696. if [ -f download/Proxy-auth ]; then
  697. proxyauth="`cat download/Proxy-auth`"
  698. if [ "$proxyauth" ]; then
  699. curl_options="$curl_options --proxy-user $proxyauth"
  700. else
  701. echo "INFO: No proxy-auth information... removing download/Proxy-auth."
  702. rm download/Proxy-auth
  703. fi
  704. fi
  705. # Thing to do only once
  706. #
  707. if [ $this_is_the_2nd_run = 0 ]; then
  708. # am i using a proxy?
  709. # -- say i'm doing it even when i already did ;-)
  710. if [ "$proxy" ]; then
  711. echo "INFO: Setting proxy to $proxy."
  712. fi
  713. if [ "$proxyauth" ]; then
  714. echo "INFO: Setting proxy authentication information."
  715. fi
  716. # do mirror detection
  717. detect_mirror
  718. fi
  719. fi
  720. case "$1" in
  721. -list) list ;;
  722. -list-unknown) list_unknown ;;
  723. -list-missing) list_missing ;;
  724. -list-cksums) list_cksums ;;
  725. -pattern) shift ; pattern "$@" ;;
  726. -package) shift ; package "$@" ;;
  727. -repository) shift ; repository "$@" ;;
  728. -required) required ;;
  729. -all) all ;;
  730. -*|"") exec $0 --help ;;
  731. *) single_files "$@" ;;
  732. esac
  733. exit 0