summaryrefslogtreecommitdiffstats
path: root/scripts/lib/ptxd_make_get.sh
blob: f4cd91e15eda3fb20cdda4fc03d013e404847526 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash

#
# $@: possible download URLs, seperated by space
#
ptxd_make_get() {
	local orig_argv=( "${@}" )
	local -a argv
	local mrd=false		# is mirror already part of urls?

	if [ -z "${1}" ]; then
		echo
		echo "${PROMPT}error: empty parameter to '${FUNCNAME}'"
		echo
		exit 1
	fi

	while [ ${#} -gt 0 ]; do
		local url="${1}"
		shift

		case "${url}" in
		${PTXCONF_SETUP_PTXMIRROR}/*/*)
			# keep original URL, for stuff like glibc
			argv[${#argv[@]}]="${url}"
			mrd=true
			;;
		${PTXCONF_SETUP_PTXMIRROR}/*)
			# if mirror is given us to download, add it, but only once
			if ! ${mrd}; then
				argv[${#argv[@]}]="${url}"
				mrd=true
			fi
			;;
		http://*|ftp://*)

			# keep original URL
			argv[${#argv[@]}]="${url}"

			# add mirror to URLs, but only once
			if ! ${mrd}; then
				argv[${#argv[@]}]="${url/#*:\/\/*\//${PTXCONF_SETUP_PTXMIRROR}/}"
				mrd=true
			fi
			;;
		file://*)
			# keep original URL
			argv[${#argv[@]}]="${url}"
		esac
	done

	set -- "${argv[@]}"

	while [ ${#} -ne 0 ]; do
		local url="${1}"
		shift

		case "${url}" in
		http://*|ftp://*)
			#
			# download to temporary file first,
			# and move it to correct file name after successfull download
			#
			local file="${url##*/}"

			# download any pending half downloaded files
			rm -f -- "${PTXDIST_SRCDIR}/${file}."*

			local temp_file="$(mktemp "${PTXDIST_SRCDIR}/${file}.XXXXXXXXXX")" || ptxd_bailout "failed to create tempfile"
			wget \
			    --passive-ftp \
			    --progress=bar:force \
			    --timeout=30 \
			    --tries=5 \
			    ${PTXDIST_QUIET:+--quiet} \
			    -O "${temp_file}" \
			    "${url}" && {
				chmod 644 -- "${temp_file}" && \
				mv -- "${temp_file}" "${PTXDIST_SRCDIR}/${file}"
				return
			}
			rm -f -- "${temp_file}"
			;;
		file*)
			local thing="${url/file:\/\///}"

			if [ -f "$thing" ]; then
				echo "local archive, copying"
				cp -av "${thing}" "${PTXDIST_SRCDIR}" && return
			elif [ -d "${thing}" ]; then
				echo "local directory instead of tar file, skipping get"
				return
			else
				thing="${url/file:\/\//./}"
				if [ -d "${thing}" ]; then
					echo "local project directory instead of tar file, skipping get"
					return
				fi
			fi
			;;
		*)
			echo
			echo "Unknown URL Type!"
			echo "URL: $url"
			echo
			exit 1
			;;
		esac
	done

	echo
	echo "Could not download packet"
	echo "URL: ${orig_argv[@]}"
	echo
	exit 1
}

export -f ptxd_make_get