summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-opkg/root/usr/libexec/opkg-call
blob: 1234b70330aa65b29d91a3dc512a68b301142e2e (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
#!/bin/sh

. /usr/share/libubox/jshn.sh

action=$1
shift

case "$action" in
	list-installed)
		cat /usr/lib/opkg/status
	;;
	list-available)
		lists_dir=$(sed -rne 's#^lists_dir \S+ (\S+)#\1#p' /etc/opkg.conf /etc/opkg/*.conf 2>/dev/null | tail -n 1)
		find "${lists_dir:-/usr/lib/opkg/lists}" -type f '!' -name '*.sig' | xargs -r gzip -cd
	;;
	install|update|remove)
		(
			opkg="opkg"

			while [ -n "$1" ]; do
				case "$1" in
					--autoremove|--force-overwrite|--force-removal-of-dependent-packages)
						opkg="$opkg $1"
						shift
					;;
					-*)
						shift
					;;
					*)
						break
					;;
				esac
			done

			if flock -x 200; then
				$opkg $action "$@" </dev/null >/tmp/opkg.out 2>/tmp/opkg.err
				code=$?
				stdout=$(cat /tmp/opkg.out)
				stderr=$(cat /tmp/opkg.err)
			else
				code=255
				stderr="Failed to acquire lock"
			fi

			json_init
			json_add_int code $code
			[ -n "$stdout" ] && json_add_string stdout "$stdout"
			[ -n "$stderr" ] && json_add_string stderr "$stderr"
			json_dump
		) 200>/tmp/opkg.lock

		rm -f /tmp/opkg.lock /tmp/opkg.err /tmp/opkg.out
	;;
	*)
		echo "Usage: $0 {list-installed|list-available}"       >&2
		echo "       $0 {install|upgrade|remove} pkg[ pkg...]" >&2
		exit 1
	;;
esac