forked from ArchStrike/pkgupdates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevel-addpkg
More file actions
executable file
·59 lines (44 loc) · 1.67 KB
/
devel-addpkg
File metadata and controls
executable file
·59 lines (44 loc) · 1.67 KB
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
#!/usr/bin/env bash
# Exit with an error if no arguments are given
[[ -z "$1" ]] && {
printf '%s\n' "Error: no arguments provided, please provide the name of the package you wish to add" >&2
exit 1
}
# Change to the script directory and remember where it is
cd "${0%/*}"
# The config file to load
config='pkgupdates.conf'
# Load the config if it exists
[[ -f "$config" ]] && source "$config"
# Load the default if it isn't in the config
[[ -z "$DEVELVERSION_CONF" ]] && DEVELVERSION_CONF="$PWD/develversion.conf"
# Create a temp directory
tmp_dir="$(mktemp -d)"
tmp_config="$tmp_dir/"$(sed 's|^.*\/||' <<< "$DEVELVERSION_CONF")
function tmpdir_cleanup {
# Remove the temp file if it exists
[[ -f "$tmp_config" ]] \
&& rm "$tmp_config"
# Remove the temp directory if it exists
[[ -d "$tmp_dir" ]] \
&& rmdir "$tmp_dir"
}
function error {
tmpdir_cleanup
printf '%s\n' "Error: $1" >&2
exit 1
}
# Copy the config to the temp location
cp "$DEVELVERSION_CONF" "$tmp_config"
# Add the new package to the temp config and sort the config alphabetically
printf '%s\n' "$1" >> "$tmp_config"
sort -u -o "$tmp_config" "$tmp_config"
# fail if the new package can't be found in the temp config
egrep -q "^$1$" "$tmp_config" \
|| error "tried and failed to add package"
# fail if the temp config isn't one line longer than the old
[[ $(( $(wc -l "$DEVELVERSION_CONF" | sed 's|\ .*$||') + 1 )) = $(wc -l "$tmp_config" | sed 's|\ .*$||') ]] \
|| error "package already contained in $DEVELVERSION_CONF"
# Things should be good if we haven't failed yet, so let's make the new config official and cleanup
mv "$tmp_config" "$DEVELVERSION_CONF"
tmpdir_cleanup