aboutsummaryrefslogtreecommitdiff
path: root/tools/update_man_pages.sh
blob: b82ee8a46c8a256a618dc41685c81651679a4c20 (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
#!/bin/sh

usage_exit() {
	echo "Usage: $(basename $0) (-d) (-g)"
	exit 1
}

if [[ "$1" == "-h" ]]; then
	usage_exit
fi

if [[ "$1" == "-?" ]]; then
	usage_exit
fi

if [[ "$1" == "-d" ]]; then
	date=true
	shift
fi

if [[ "$1" == "-g" ]]; then
	git=true
	shift
fi

version=$(grep '^our $VERSION' SBO-Lib/lib/SBO/Lib.pm | grep -Eo '[0-9]+(\.[0-9RC@gita-f]+){0,1}')

if ! [[ -d "./man1" ]]; then
	echo "you do not seem to be at the right place to run this."
	echo "the man{1,5}/ directories should be under ."
	exit 1
fi

old_version=$(head -1 man1/sbocheck.1 | rev | cut -d' ' -f2 | rev \
	| sed 's/"//g')

tmpfile=$(mktemp /tmp/XXXXXXXXX)

sed_file() {
	if [[ "$1" == "" || "$2" == "" ]]; then
		echo "sed_file(): two arguments required."
		exit 1
	fi

	file="$1"
	sed_cmd="$2"

	cat $file | sed "$sed_cmd" > $tmpfile
	if [[ "$?" == "0" ]]; then
		mv $tmpfile $file
	else
		return 1
	fi

	return 0
}

for i in $(ls man1); do
	sed_file man1/$i "s/$old_version/$version/g"
done

for i in $(ls man5); do
	sed_file man5/$i "s/$old_version/$version/g"
done

if [[ "$?" == "0" ]]; then
	echo "version updated."
fi

update_date() {
	if ! which ddate >/dev/null 2>&1; then
		echo "I can't find ddate."
		return 1
	fi

	old_date=$(head -1 man1/sbocheck.1 | cut -d' ' -f4- | rev \
		| cut -d' ' -f4- | rev | sed 's/"//g')

	new_date=$(ddate +"%{%A, %B %d%}, %Y YOLD")

	for i in man1/*; do
		sed_file $i "s/$old_date/$new_date/g"
	done

	for i in man5/*; do
		sed_file $i "s/$old_date/$new_date/g"
	done

	if [[ "$?" == "0" ]]; then
		echo "date updated."
	else
		return 1
	fi

	return 0
}

update_git() {
	if ! which git >/dev/null 2>&1; then
		echo "I can't find git."
		return 1
	fi

	if [[ "$date" == "true" ]]; then
		extra=" and dates"
	fi

	git add man1/* man5/*
	git commit -m "updated versions$extra for man pages"
	git push

	if [[ "$?" == "0" ]]; then
		echo "git updated."
	else
		return 1
	fi

	return 0
}

date_return=0
if [[ "$date" == "true" ]]; then
	update_date
	date_return=$?
fi

git_return=0
if [[ "$git" == "true" ]]; then
	update_git
	git_return=$?
fi

if [[ "$date_return" != "0" || "$git_return" != "0" ]]; then
	exit 1
fi

exit 0