aboutsummaryrefslogtreecommitdiff
path: root/wiki
blob: b27d4792df82954b596b1ff5da2015b0794485bd (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
#!/bin/bash

WIKIROOT=/home/hugo/wiki/
# Causes the last function in a non-background pipeline to use the
# current shell. Needed for readarray to set a variable in the script.
shopt -s lastpipe
declare -a WIKI_LIST
printf '%s\0' "$WIKIROOT"/* \
	| grep --null-data -v html \
	| xargs -0 -n1 basename -z \
	| readarray -d '' -t WIKI_LIST

# A simple script for managing a vimwiki git repo.
# Allows slightly simpler edits and commits.

declare -a wiki_list

GIT='git -c color.status=always -c color.ui=always'

#
# Adds all files to git, and commits with either the arguments as
# message, or with the current time as the message.
# Assumes that PWD is a git repo.
#
function commit {
	if [ $# -lt 2 ]; then
		msg="$(date)"
	else
		msg="$*"
	fi
	$GIT add -A
	$GIT commit -m "$msg"
}

#
# Like commit above, but ammends
#
function ammend {
	if [ $# -eq 1 ]; then
		$GIT commit --amend
	else
		msg="$*"
		$GIT commit --amend -m "$msg"
	fi
}

function grep_ {
	$GIT grep -E -i "$@"
}

#
# Run function on wiki
#
function wiki_do {
	wiki=$1
	command=$2

	pushd "$WIKIROOT/$wiki" || exit 1

	shift
	case $command in
		commit)
			shift
			commit "$@"
			;;
		ammend)
			shift
			ammend "$@"
			;;
		grep)
			shift
			grep_ "$@"
			;;
		g|go)
			commit "$@"
			$GIT push
			;;
		echo)
			echo "-- $wiki --"
			;;
		*)
			$GIT "$@"
			;;
	esac
}

# == MAIN ==

#
# Parse command line options
#
while [ $# -ne 0 ]; do
	case $1 in
		-w|--wiki)
			shift
			wiki_list+=("$1")
			shift
			;;
		-l|--list)
			shift
			echo "Available Wikis:"
			echo "================"
			for w in "${WIKI_LIST[@]}"; do
				echo " - $w"
			done
			exit
			;;
		-h|--help|-?)
			shift
			cat - <<- EOF
		Wiki helper. Usage:
		--help | -h :: Display this help
		--list | -l :: Show available wikis
		--wiki | -w <wiki> :: specify a specific wiki for operation

		Default acts on all wikis.

		wi commit [msg] :: Create a git commit on specified wikis
		wi ammend [msg] :: Change last commit
		wi g [msg] :: wi commit [msg]; wi push
		wi [git-command] :: run git commands on wikis
		EOF
		exit
		;;
	*)
		break
		;;
	esac
done

# Edit the given wiki if no further argument is given
if [ $# -eq 0 ]; then
	set -- "${wiki_list[0]:-${WIKI_LIST[0]}}"
	vim "$WIKIROOT/$1"/index.*
	exit
fi

# Run given command for all wikis
for wiki in "${wiki_list[@]:-${WIKI_LIST[@]}}"; do
	wiki_do "$wiki" "$@" | sed "s/^/$wiki /" &
done


wait