-
Notifications
You must be signed in to change notification settings - Fork 1
/
pakt
executable file
·153 lines (130 loc) · 3.62 KB
/
pakt
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/sh
# Pakt (PAcman KaTegories) - Pacman wrapper for categorizing packages
# v0.1.0 GPL-2.0
# https://github.com/mrminede/pakt
# Help message part.
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
echo "PAKT: Displaying your package manager's help message.
If you wanted to see pakt, see the man page at \`man pakt\`
"
fi
# Save all the packages for a group of categories temporarily in /tmp.
# This is needed, because we allow different categories for different packages.
# We could do this with an array of some sort, but would lose POSIX-compliancy.
pkg_tempsave() {
for cat in $(echo "$CATS" | tr ' ' '\n'); do
for pkg in $PKGS; do
echo "$pkg" >> /tmp/pakt/"$cat"
done
done
PKGS=""
CATS="$DEFAULT_CATEGORIES"
}
# Strict mode. The script will throw an error on an unbound variable encounter.
set -u
# Temporary path storing new package and category data.
mkdir -p "/tmp/pakt"
# Check whether the data path was defined with XDG.
if [ -z "${XDG_DATA_HOME:-}" ]; then
DATA_PATH="$HOME/.local/share/pakt"
else
DATA_PATH="$XDG_DATA_HOME/pakt"
fi
mkdir -p "$DATA_PATH"
# Control/set config path variable.
if [ -z "${PAKT_CONF_PATH:-}" ]; then
PAKT_CONF_PATH="/etc"
fi
# Check whether pakt.conf exists in the first place.
if [ ! -e "$PAKT_CONF_PATH/pakt.conf" ]; then
echo "PAKT: ERROR: /etc/pakt.conf not found. Exiting ..."
exit 1
fi
. "$PAKT_CONF_PATH/pakt.conf"
# Process variables
CMD="$PACKAGE_MANAGER"
CATS="$DEFAULT_CATEGORIES"
PKGS=""
IS_TRANSACTION_COMMAND=0
TRANSACTION_READY=1
REMOVE_MODE=0
for arg in "$@"; do
case "$arg" in
"-S"*)
CMD="${CMD} ${arg}"
;;
"-R"*)
CMD="${CMD} ${arg}"
REMOVE_MODE=1
;;
"-"*)
#if the arg is part of the nonstdargs (has input after the arg e.g. --output ARG2.
#We dont want to interpret ARG2 as a package, therefor we skip it)
for nonstdarg in $NON_STD_ARGS; do
if [ "$arg" -eq "$nonstdarg" ]; then
SKIP_ARG=1
fi
done
;;
"++")
CATS=$(ls "$DATA_PATH")
;;
"+"*)
# Split up comma-separated categories (+cat1,cat2,test,cli).
arg=$(echo "$arg" | tr ',' ' ')
CATS="${CATS} ${arg#+}"
# This is used to detect when a package is declared after a category.
# When that happens, we want to first save the package and category data before resetting both.
TRANSACTION_READY=0
;;
*)
if [ $SKIP_ARG -eq 1 ]; then
SKIP_ARG=0
continue
fi
# See comment above.
if [ "$TRANSACTION_READY" -eq 0 ]; then
pkg_tempsave
TRANSACTION_READY=1
fi
CMD="${CMD} ${arg}"
PKGS="${PKGS} ${arg}"
;;
esac
done
# Save the last packages.
pkg_tempsave
# Execute pacman.
$CMD
if [ "$IS_TRANSACTION_COMMAND" -eq 1 ]; then
echo "PAKT: NOTE: this doesn't look like a job for me. Exiting ..."
exit 1
fi
if [ ! "$?" ]; then
echo "PAKT: ERROR: package manager failed. Exiting..."
exit 1
fi
# Omit the rest of the script if no category tomfoolery was performed.
if [ ! "$(ls /tmp/pakt)" ]; then
exit
fi
for tmpfile in /tmp/pakt/*; do
catfile=$DATA_PATH/`basename $tmpfile`
touch $catfile
for pkg in $(cat "$tmpfile"); do
if [ "$REMOVE_MODE" -eq 1 ]; then
# Remove package from category file.
# You can't pipe directly into tmpfile because it would encounter an empty file.
sed -i "/^$pkg$/d" "$catfile"
elif [ "$pkg" != "$(grep "^$pkg\$" "$catfile")" ]; then
# Add package to category file, if not already there.
echo "$pkg" >> "$catfile"
fi
done
done
# At last we want to clean up, so we don't work on the packages from last time.
rm -r "/tmp/pakt"
# Clear empty catfiles from DATA_PATH. See the third setting in your `pakt.conf`.
if [ "$REMOVE_EMPTY_CATFILES" -eq 1 ]; then
find "$DATA_PATH" -type f -empty -delete
fi