-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean-templates
executable file
·73 lines (66 loc) · 1.3 KB
/
clean-templates
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
#!/bin/bash
search='^[[:space:]]*# (vim|template\ (start.*name=([[:alnum:]-]*)|end)).*$'
skip=true
while getopts 'hm' argument
do
case $argument in
h)
echo "usage: $0 [options] < PKGBUILD"
echo 'cleans a given PKGBUILD'
echo ' -h show this help text'
echo ' -m only remove duplicates'
exit ;;
m)
search='^[[:space:]]*# (template\ (start.*name=([[:alnum:]-]*))).*$'
skip=false ;;
\?) exit 1 ;;
esac
done
IFS=''
collect_until_end() {
while read -r line
do
if [[ $line =~ ^[[:space:]]*'# template '(start|end)';'.*$ ]]
then
case ${BASH_REMATCH[1]} in
start)
collect_until_end ;;
end)
return 0 ;;
esac
fi
done
return 1
}
seen=/
while read -r line
do
if ${empty-false}
then
empty=false
if [ "$line" = '' ]
then
continue
fi
fi
if [[ $line =~ $search ]]
then
case ${BASH_REMATCH[1]} in
vim) unset last ;;
template\ start*)
name=${BASH_REMATCH[3]}
if [[ $name =~ ^($seen)$ ]]
then
collect_until_end || exit 1
empty=true
continue
else
seen+="|${name}"
fi ;;
esac
$skip && continue
fi
echo -En "$last"
last=$line$'\n'
done < ${!OPTIND:-/dev/stdin}
echo -En "$last"