summaryrefslogtreecommitdiff
path: root/bin/extra/ladd
blob: 1ce399149d0d8529f652667b6731bba63bcc1deb (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
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
154
155
156
157
#!/bin/sh
assume_yes=0
verbose=0

die ()
{
	# always print errors
	if [ "$verbose" -eq 1 ] || echo "$1" | grep -q "^E:"
	then
		echo "$1" >&2
	fi
}

help ()
{
	local verbose=1
	die "ladd [OPTION [ARG]...] [FILE]
        -c CATEGORY
        -d DESCRIPTION
        -f FILE
        -h                        display help
        -l LINK
        -v                        verbose
        -y                        assume 'yes'"
}

alias shiftt='
if [ $# -gt 0 ]
then
	shift
else
	die "E: No argument provided."
	exit 1
fi'

ftest ()
{
	if [ ! -f "$1" ]; then
		die "E: '$1' not found."
		exit 1
	fi
}

# Main execution

if [ $# -eq 0 ]
then
	help
	exit
fi
while [ $# -ne 0 ]
do
	case "$1" in
		'-c')
			shiftt
			category="$(echo "$1" | tr '[:lower:]' '[:upper:]')" 
			die "I: category -> '$category'"
			shiftt
			;;
		'-d')
			shiftt
			description="$1"
			die "I: description -> '$description'"
			shiftt
			;;
		'-f')
			shiftt
			test -n "$1" &&
				ftest "$1"
			file="$1"
			shiftt
			die "I: file -> '$file'"
			;;
		'-l')
			shiftt
			link="$1"
			die "I: link -> '$link'"
			shiftt
			;;
		'-v')
			verbose=1
			die "I: verbose -> $verbose"
			shiftt
			;;
		'-y')
			assume_yes=1
			die "I: assume_yes -> $assume_yes"
			shiftt
			;;
		*)
			if [ ! "$file" ]
			then
				ftest "$1"
				file="$1"
				die "I: file -> '$file'"
				shiftt
			else
				die "E: not a valid argument."
				help
				exit 1
			fi
			;;
	esac
done

# Tests
# Check on file and/or link
if [ -z "$file" ] || [ -z "$link" ]
then
	die "E: no file and/or link supplied."
	exit 1
fi
# Check on valid category
categories="$(pup -f "$file" -p 'h2 text{}' |
	tr '\n' ',' | sed 's/,$//')"
die ""
die "categories: $categories"
if ! echo "$categories" | grep -q "$category"
then
	die "E: '$category' is not a valid category."
fi
# Check on empty values
for var in category description
do
	eval value=\$$var
	if [ -z "$value" ]
	then
		die "W: $var is empty"
		if [ "$assume_yes" -eq 0 ]
		then
			die "W: are you sure you want to proceed? (Y/n)"
			echo -n ">" >&2
			read input
			test "$input" = "n" &&
				exit 1
		fi
		eval $var="'UNDEFINED'"
		die "I: $var -> $(eval echo "\$$var")"
	fi
done
# Checks if the link has already been added
if grep -q "$link" "$file"
then
	die "E: link already in $file"
	exit 1
fi

# constructing the tag
tag="<li><a href=\"$link\">$link</a> - $description</li>"
die "tag: $tag"

die "I: appending tag"
# use grep line numbers to add link after match of the category header
# header will have an <ul> element this one is used with -A 1 | tail -1
sed -i "$(grep -n "<h2.*$category" -A 1 "$file" |
	tail -n 1 |
	cut -f 1 -d '-')a $tag" "$file"