aboutsummaryrefslogtreecommitdiff
path: root/libexec/build-url
blob: 72be2119a7f3b76effca1d38ed5e89ed224655ab (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
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env build

build_url_usage()
{
cat<<END_OF_USAGE
usage: build-url [options] <url>

options:
  -a, --archive		Return the archive portion of the URL.
  -A, --arg [key]	Return the specified URL argument, or all arguments if
			not specified.
  -B, --base		Return the proto://host/path portion of the URL
  -P, --proto		Return the protocol portion of the URL
  -h, --help		Display this help

END_OF_USAGE
}

build_url_proto() { printf '%s' "${1%%://*}"; }
build_url_base() { printf '%s' "${1%%\?*}"; }

build_url_arg()
{
	build_url_arg_match=
	if test "$#" -gt '1'; then
		build_url_arg_match="${1}"
		shift
	fi
	set -- $(echo "${1##$(build_url_base "${1}")\?}"|sed -e 's/&/ /')

	if test -z "${build_url_arg_match}"; then
		printf '%s' "${*}"
		return
	fi

	for arg; do
		case "${arg}" in
		(${build_url_arg_match}=*)
			echo "${arg#*=}"; return;;
		esac
	done
}

build_url_archive()
{
	build_url_name_var="$(build_url_base "${1}")"
	build_url_name_var="${build_url_name_var##*/}"
	build_url_name_var="${build_url_name_var%%\?*}"

	build_url_name_complete="0"
	case "${build_url_name_var}" in
	(*.tar.*)		build_url_name_complete=1;;
	(*.t[bgx]z)		build_url_name_complete=1;;
	(*.tbz2)		build_url_name_complete=1;;
	(*.tz)			build_url_name_complete=1;;
	(*.zip|*.jar)		build_url_name_complete=1;;
	(*.rpm)			build_url_name_complete=1;;
	(*.z|*.z)		build_url_name_complete=1;;
	(*.gz|*.gzip)		build_url_name_complete=1;;
	(*.bz|*.bz2)		build_url_name_complete=1;;
	(*.xz)			build_url_name_complete=1;;
	esac

	if [ "${build_url_name_complete}" -eq "1" ]; then
		printf '%s' "${build_url_name_var}"
		unset build_url_name_var
		return
	fi

	# The filename to archive has to be supplied on the SOURCES_URI, else
	# we can't do anything about packages which share common sources.
	build_url_name_var=
	for arg in $(build_url_arg "${1}"); do
		case "${arg}" in
		(archive=*) build_url_name_var="${arg##*=}";;
		esac
	done

	if [ -z "${build_url_name_var}" ]; then
		die "do not know how to store source from '${1}'"
	fi

	printf '%s' "${build_url_name_var}"
	unset build_url_name_var
}

if test "${BUILDER_CALL_STACK}" = '__main__'; then
# Look for requests for help "anywhere" in the command line
	for arg; do
		case "${arg}" in
		(-h|-help|--help) build_url_usage; exit 0;;
		esac
	done

	URL_ACTION="url"
	while [ "$#" -gt "0" ]; do
		case "${1}" in
		(-a|-archive|--archive)
			URL_ACTION="archive"
			shift
			;;
		(-A|-arg|--arg)
			URL_ACTION="arg"
			shift
			;;
		(-B|-base|--base)
			URL_ACTION="base"
			shift
			;;
		(-P|-proto|--proto|-protocol|--protocol)
			URL_ACTION="proto"
			shift
			;;
		(-*)	die "unknown url action '${1}'";;
		(*)	break;;
		esac
	done

	case "${URL_ACTION}" in
	(arg)		build_url_arg "${@}"; exit $?;;
	(archive)	build_url_archive "${@}"; exit $?;;
	(base)		build_url_base "${@}"; exit $?;;
	(proto)		build_url_proto "${@}"; exit $?;;
	(*)	die "unknown url action '${URL_ACTION}'";;
	esac
fi

# vim: filetype=sh