summaryrefslogtreecommitdiffstats
path: root/src/ob-unpacksource.sh
blob: c07a67b816825ff4b17f5fef08251966b6feccad (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
129
130
# Unpack native or upstream source
#
# Copyright (C) 2012 Patrick McDermott
#
# This file is part of opkbuild.
#
# opkbuild is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# opkbuild is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with opkbuild.  If not, see <http://www.gnu.org/licenses/>.

. "${LIBDATADIR}/libopkbuild.${SHSOEXT}.${LIBOPKBUILD_SHSOVERSION}"

upstream_ar_base=
upstream_ar=
upstream_ar_z=
upstream_ar_dir=

unpack_native()
{
	ob_info "$(ob_get_msg 'unpacking_native')"
	cp -Rp '../src' 'src' || ob_error "$(ob_get_msg 'cant_unpack_native')"
}

get_upstream_archive()
{
	upstream_ar_base="${OPK_SOURCE}-${OPK_SOURCE_VERSION_UPSTREAM}.orig.tar"
	upstream_ar="$(printf '%s ' "../${upstream_ar_base}"*)"
	case "${upstream_ar}" in
		"../${upstream_ar_base}*")
			ob_info "$(ob_get_msg 'no_sources')"
			return 1
			;;
		*' '*)
			ob_error "$(ob_get_msg 'multiple_upstream_sources')"
			;;
		*)
			return 0
			;;
	esac
}

get_upstream_compression()
{
	z_ext="${upstream_ar#../${upstream_ar_base}}"
	case "${z_ext}" in
		'')
			upstream_ar_z=''
			;;
		'.gz')
			upstream_ar_z='z'
			;;
		'.bz2')
			upstream_ar_z='j'
			;;
		'.lz')
			upstream_ar_z='a'
			;;
		'.Z')
			upstream_ar_z='Z'
			;;
		*)
			ob_error "$(ob_get_msg 'unsupported_archive_compression')" \
				"${z_ext}"
			;;
	esac
}

get_upstream_dir()
{
	upstream_ar_dir="$(tar "-t${upstream_ar_z}f" "${upstream_ar}" | \
		sed -n 's|^\(\./\)\{0,1\}\([^/]\{1,\}\).*$|\2|p' | sort | uniq)"
	dir_count="$(wc -l <<-EOF
		"${upstream_ar_dir}"
		EOF
		)"
	case "${dir_count}" in
		0)
			ob_error "$(ob_get_msg 'no_upstream_dirs')"
			;;
		1)
			;;
		*)
			ob_error "$(ob_get_msg 'multiple_top_upstream_dirs')"
	esac
}

extract_upstream()
{
	ob_info "$(ob_get_msg 'unpacking_upstream')"
	tar "-x${upstream_ar_z}f" "${upstream_ar}" || \
		ob_error "$(ob_get_msg 'cant_unpack_upstream')"
	mv "${upstream_ar_dir}" 'src' || \
		ob_error "$(ob_get_msg 'cant_move_native')"
}

unpack_upstream()
{
	get_upstream_compression
	get_upstream_dir
	extract_upstream
}

main()
{
	ob_set_text_domain 'opkbuild'

	ob_init_package '..' || exit 1
	ob_parse_package_metadata -c '.opkbuild.cache' || exit 1

	if [ -d 'src' ]; then
		ob_info "$(ob_get_msg 'already_unpacked')"
	else
		if [ -d '../src' ]; then
			unpack_native
		elif get_upstream_archive; then
			unpack_upstream
		fi
	fi
}

main "${@}"