summaryrefslogtreecommitdiff
path: root/cron-jobs/check_archlinux/parse_pkgbuilds.sh
blob: c8d861899a27b71b1b6107b04565421b94d76c96 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash

# Usage : parse_pkgbuilds.sh arch <pkgbuilds_dir1,dir2,...>
# Example : parse_pkgbuilds.sh i686 /var/abs/core /var/abs/extra

exit() { return; }

splitpkg_overrides=('depends' 'optdepends' 'provides' 'conflicts')
variables=('pkgname' 'pkgbase' 'epoch' 'pkgver' 'pkgrel' 'makedepends' 'arch' "${splitpkg_overrides[@]}")
readonly -a variables splitpkg_overrides

backup_package_variables() {
	for var in "${splitpkg_overrides[@]}"; do
		indirect="${var}_backup"
		eval "${indirect}=(\${$var[@]})"
	done
}

restore_package_variables() {
	for var in "${splitpkg_overrides[@]}"; do
		indirect="${var}_backup"
		if [ -n "${!indirect}" ]; then
			eval "${var}=(\${$indirect[@]})"
		else
			unset ${var}
		fi
	done
}

print_info() {
	echo -e "%NAME%\n$pkgname\n"
	if [ -n "$epoch" ]; then
		echo -e "%VERSION%\n$epoch:$pkgver-$pkgrel\n"
	else
		echo -e "%VERSION%\n$pkgver-$pkgrel\n"
	fi
	echo -e "%PATH%\n$dir\n"

	if [ -n "$pkgbase" ]; then
		echo -e "%BASE%\n$pkgbase\n"
	fi

	if [ -n "$arch" ]; then
		echo "%ARCH%"
		for i in "${arch[@]}"; do echo $i; done
		echo ""
	fi
	if [ -n "$depends" ]; then
		echo "%DEPENDS%"
		for i in "${depends[@]}"; do
			echo $i
		done
		echo ""
	fi
	if [ -n "$makedepends" ]; then
		echo "%MAKEDEPENDS%"
		for i in "${makedepends[@]}"; do
			echo $i
		done
		echo ""
	fi
	if [ -n "$conflicts" ]; then
		echo "%CONFLICTS%"
		for i in "${conflicts[@]}"; do echo $i; done
		echo ""
	fi
	if [ -n "$provides" ]; then
		echo "%PROVIDES%"
		for i in "${provides[@]}"; do echo $i; done
		echo ""
	fi
}

source_pkgbuild() {
	ret=0
	dir=$1
	pkgbuild=$dir/PKGBUILD
	for var in "${variables[@]}"; do
		unset ${var}
	done
	source $pkgbuild &>/dev/null || ret=$?

	# ensure $pkgname and $pkgver variables were found
	if [ $ret -ne 0 -o -z "$pkgname" -o -z "$pkgver" ]; then
		echo -e "%INVALID%\n$pkgbuild\n"
		return 1
	fi

	if [ "${#pkgname[@]}" -gt "1" ]; then
		pkgbase=${pkgbase:-${pkgname[0]}}
		for pkg in "${pkgname[@]}"; do
			if [ "$(type -t package_${pkg})" != "function" ]; then
				echo -e "%INVALID%\n$pkgbuild\n"
				return 1
			else
				backup_package_variables
				pkgname=$pkg
				while IFS= read -r line; do
					var=${line%%=*}
					var="${var#"${var%%[![:space:]]*}"}"   # remove leading whitespace characters
					for realvar in "${variables[@]}"; do
						if [ "$var" == "$realvar" ]; then
							eval $line
							break
						fi
					done
				done < <(type package_${pkg})
				print_info
				restore_package_variables
			fi
		done
	else
		echo
		print_info
	fi

	return 0
}

find_pkgbuilds() {
    #Skip over some dirs
    local d="${1##*/}"
    if [ "$d" = "CVS" -o "$d" = ".svn" ]; then
        return
    fi

	if [ -f $1/PKGBUILD ]; then
		source_pkgbuild $1
		return
	fi
	empty=1
	for dir in $1/*; do
		if [ -d $dir ]; then
			find_pkgbuilds $dir
			unset empty
		fi
	done
	if [ -n "$empty" ]; then
		echo -e "%MISSING%\n$1\n"
	fi
}

if [ -z "$1" -o -z "$2" ]; then
	exit 1
fi

CARCH=$1
shift
for dir in "$@"; do
	find_pkgbuilds $dir
done

exit 0