summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-package-manager/root/usr/libexec/package-manager-call
blob: 7667b8e8ce592503c2abd54108e7e73f5e76ea6a (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
#!/bin/sh

. /usr/share/libubox/jshn.sh

action=$1
shift

if [ -f /usr/bin/apk ]; then
	ipkg_bin="apk"
else
	ipkg_bin="opkg"
fi

case "$action" in
	list-installed)
		if [ $ipkg_bin = "apk" ]; then
			$ipkg_bin list -I --full 2>/dev/null
		else
			cat /usr/lib/opkg/status
		fi
	;;
	list-available)
		if [ $ipkg_bin = "apk" ]; then
			$ipkg_bin list --full 2>/dev/null
		else
			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
		fi
	;;
	install|update|upgrade|remove)
		(
			cmd="$ipkg_bin"

			# APK have command renamed
			if [ $ipkg_bin = "apk" ]; then
				case "$action" in
					install)
						action="add"
					;;
					update)
						action="update"
					;;
					upgrade)
						action="upgrade"
					;;
					remove)
						action="del"
					;;
				esac
			fi

			# APK have --autoremove enabled by default and
			# --force-removal-of-dependent-packages as -r option
			if [ $ipkg_bin = "apk" ]; then
				while [ -n "$1" ]; do
					case "$1" in
						--force-removal-of-dependent-packages)
							cmd="$cmd -r"
							shift
						;;
						--force-overwrite)
							cmd="$cmd $1"
							shift
						;;
						-*)
							shift
						;;
						*)
							break
						;;
					esac
				done
			else
				while [ -n "$1" ]; do
					case "$1" in
						--autoremove|--force-overwrite|--force-removal-of-dependent-packages)
							ipkg_bin="$apk $1"
							shift
						;;
						-*)
							shift
						;;
						*)
							break
						;;
					esac
				done
			fi

			if flock -x 200; then
				$cmd $action "$@" </dev/null >/tmp/ipkg.out 2>/tmp/ipkg.err
				code=$?
				stdout=$(cat /tmp/ipkg.out)
				stderr=$(cat /tmp/ipkg.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/ipkg.lock

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