-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle
executable file
·77 lines (64 loc) · 2.28 KB
/
handle
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
#!/usr/bin/env bash
##############################
# @jaimecgomezz
#
# This script handles all the
# actions related to patches
# on this dmenu distribution
##############################
set -e
SHOULD_REDIRECT="0" # Quiet by default
PATCH_ACTION="patch"
DEPATCH_ACTION="depatch"
TRACK_FILE="applied"
USAGE_FILE="handle-usage"
base_usage() {
echo "usage: handle ACTION PATCH [OPTIONS]"; echo
echo "Options:"
echo " -d Show compiler output."; echo
echo "Actions:"
echo " patch Apply the PATCH and rebuild"
echo " depatch Removes the PATCH and rebuild"; echo
}
print_usage() { base_usage ; cat "$USAGE_FILE" ; exit 1 ; }
redirect_output() { eval "$@ > /dev/null 2>&1" ; }
contains() {
str="$1"; substr="$2"
if test "${str#*$substr}" != "$str"; then return 0; fi
return 1
}
report_applied() { report_patch_conflict "$1" "has" "remove" ; exit 1 ; }
report_unapplied() { report_patch_conflict "$1" "hasn't" "add" ; exit 1 ; }
report_patch_conflict() { echo "[!] ( $1 ) $2 been applied, $3 the patch name from/to the '$TRACK_FILE' file if mistaken" ; }
# Test args syntax
[ "$#" -lt 2 ] && print_usage
action="$1"; patch="$2"; shift; shift
case "$action" in
patch)
;;
depatch)
options="-R"
;;
*)
print_usage
;;
esac
contains "$@" "-d" && SHOULD_REDIRECT="1"
# Verify patch exists
patches="$( ls patches/*patch | sed -E 's/(^.*\/)(.*)(\..*)/\2/g' )"
if ! echo "$patches" | grep -q "$patch"; then print_usage; fi
# Verify patch status, patched or not
if cat "$TRACK_FILE" | grep -q "$patch"; then
[ "$action" = "$DEPATCH_ACTION" ] || report_applied "$patch"
else
[ "$action" = "$PATCH_ACTION" ] || report_unapplied "$patch"
fi
# Patch handlers definition
apply_patch="patch -p1 $options < patches/${patch}.patch"
build_distro="sudo make clean install"
# Patch and build quietly by default, unless -d used
[ "$SHOULD_REDIRECT" = "0" ] && redirect_output "$apply_patch" || eval "$apply_patch"
[ "$SHOULD_REDIRECT" = "0" ] && redirect_output "$build_distro" || eval "$build_distro"
# Keep track of applied patches
[ "$action" = "$PATCH_ACTION" ] || sed -i "/^${patch}$/d" "$TRACK_FILE" # If depatch, remove it from track file
[ "$action" = "$DEPATCH_ACTION" ] || echo "$patch" >> "$TRACK_FILE" # If patch, add it to the trask file