aboutsummaryrefslogtreecommitdiff
path: root/eselect/news.eselect
blob: 903f9a531f6796c30b4b31c2ab8166fc07a11116 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# Copyright 2006 Ciaran McCreesh
# Distributed under the terms of the GNU General Public License v2
# $Id: $

DESCRIPTION="Read GLEP 42 news items"
MAINTAINER="ciaranm@ciaranm.org"
SVN_DATE='$Date: $'
VERSION=$(svn_date_to_version "${SVN_DATE}" )

paludis_command() {
	echo ${PALUDIS_COMMAND:-paludis}
}

get_repositories() {
	$(paludis_command ) --list-repositories | \
		sed -n -e 's/^\* *//p'
}

get_unread() {
	local repo repos
	repos=( $(get_repositories ) )
	for repo in ${repos[@]} ; do
		[[ -f "${ROOT}/var/lib/paludis/news/news-${repo}.unread" ]] || continue
		sed -e '/^#/d' -e "/./s,$,::${repo}," < \
			${ROOT}/var/lib/paludis/news/news-${repo}.unread
	done
}

get_read() {
	local repo repos
	repos=( $(get_repositories ) )
	for repo in ${repos[@]} ; do
		[[ -f "${ROOT}/var/lib/paludis/news/news-${repo}.read" ]] || continue
		sed -e '/^#/d' -e "/./s,$,::${repo}," < \
			${ROOT}/var/lib/paludis/news/news-${repo}.read
	done
}

mark_read() {
	local name=${1%::*} repo=${1##*::}

	touch "${ROOT}"/var/lib/paludis/news/news-${repo}.read || \
		die -q "Couldn't touch read file for ${1}"

	if ! grep -q "^${name}\$" < "${ROOT}"/var/lib/paludis/news/news-${repo}.read ; then
		echo "${name}" >> "${ROOT}"/var/lib/paludis/news/news-${repo}.read || \
			die -q "Couldn't mark ${1} as read"
	fi

	sed -i -e "/^${name}\$/d" "${ROOT}"/var/lib/paludis/news/news-${repo}.unread || \
		die -q "Couldn't remove ${1} from unread list"
}

mark_unread() {
	local name=${1%::*} repo=${1##*::}

	touch "${ROOT}"/var/lib/paludis/news/news-${repo}.unread || \
		die -q "Couldn't touch unread file for ${1}"

	if ! grep -q "^${name}\$" < "${ROOT}"/var/lib/paludis/news/news-${repo}.unread ; then
		echo "${name}" >> "${ROOT}"/var/lib/paludis/news/news-${repo}.unread || \
			die -q "Couldn't mark ${1} as unread"
	fi

	sed -i -e "/^${name}\$/d" "${ROOT}"/var/lib/paludis/news/news-${repo}.read || \
		die -q "Couldn't remove ${1} from read list"
}

purge_read() {
	local repo
	for repo in $(get_repositories ) ; do
		: > "${ROOT}"/var/lib/paludis/news/news-${repo}.read || \
			die -q "Couldn't purge ${repo} read list"
	done
}

get_dir_name() {
	local name=${1%::*} repo=${1##*::}
	$(paludis_command ) --list-repositories --repository ${repo} | \
		sed -n -e "s~^ *newsdir: *\(.*\)~\1/${name}/~p"
}

get_headers() {
	local dir=${1}
	dir=${dir}/$(basename ${1%/} ).en.txt
	sed -e '/^$/,$d' < ${dir}
}

get_body() {
	local dir=${1}
	dir=${dir}/$(basename ${1%/} ).en.txt
	sed -e '1,/^$/d' < ${dir}
}

get_title() {
	local f=$(get_dir_name ${1} )
	if [[ -d ${f} ]] ; then
		get_headers ${f} | sed -n -e 's/^Title:\s*//p'
	else
		echo "(no title available)"
	fi
}

### list action ###

describe_list() {
	echo "List news items"
}

do_list() {
	targets=( $(get_unread ) )
	write_list_start "Unread news items:"
	if [[ -n ${targets[@]} ]] ; then
		local target
		for target in ${targets[@]} ; do
			write_kv_list_entry "${target%::*}" "$(get_title ${target} )"
		done
	else
		write_kv_list_entry "(none found)" ""
	fi

	targets=( $(get_read ) )
	write_list_start "Read news items:"
	if [[ -n ${targets[@]} ]] ; then
		local target
		for target in ${targets[@]} ; do
			write_kv_list_entry "${target%::*}" "$(get_title ${target} )"
		done
	else
		write_kv_list_entry "(none found)" ""
	fi
}

### read action ###

describe_read() {
	echo "Read a news item"
}

describe_read_parameters() {
	echo "<target>"
}

describe_read_options() {
	echo "target : News item ID (from 'list' action), or the special" \
		"item 'new', to list all new news items, or 'all', to" \
		"show all items"
}

do_read() {
	[[ -z ${1} ]] && die -q "You didn't tell me what you want to read"

	local i item= items
	items=( $(get_unread ) )

	if [[ "${1}" == "new" ]] ; then
		for i in "${items[@]}" ; do
			do_read "${i%::*}"
		done
		return
	elif [[ "${1}" == "all" ]] ; then
		items=( "${items[@]}" $(get_read) )
		for i in "${items[@]}" ; do
			do_read "${i%::*}"
		done
		return
	fi

	for i in ${items[@]} ; do
		[[ -n "${item}" ]] && break
		[[ "${i%::*}" == "${1}" ]] || continue
		item=${i}
	done

	items=( $(get_read ) )
	for i in ${items[@]} ; do
		[[ -n "${item}" ]] && break
		[[ "${i%::*}" == "${1}" ]] || continue
		item=${i}
	done

	[[ -z "${item}" ]] && die -q "Can't find news item '${1}'"

	write_list_start "${item%::*}"
	local f=$(get_dir_name ${item} ) line
	get_headers ${f} | \
	while read line ; do
		local k=${line%%:*}
		case ${k} in
			Title|Author|Posted|Revision|Translator)
				write_kv_list_entry "${k}" "${line#*:}"
				;;
		esac
	done
	echo
	get_body ${f}

	mark_read "${item}"
}

### unread action ###

describe_unread() {
	echo "Unread a news item"
}

describe_unread_parameters() {
	echo "<target>"
}

describe_unread_options() {
	echo "target : News item ID (from 'list' action), or the special" \
		"item 'all', to mark all items as unread"
}

do_unread() {
	[[ -z ${1} ]] && die -q "You didn't tell me what you want to unread"

	local i item= items
	items=( $(get_read ) )

	if [[ "${1}" == "all" ]] ; then
		for i in "${items[@]}" ; do
			do_unread "${i%::*}"
		done
		return
	fi

	for i in ${items[@]} ; do
		[[ -n "${item}" ]] && break
		[[ "${i%::*}" == "${1}" ]] || continue
		item=${i}
	done

	[[ -z "${item}" ]] && die -q "Can't find read news item '${1}'"

	mark_unread "${item}"
}

### purge action ###

describe_purge() {
	echo "Purge read news items"
}

do_purge() {
	purge_read
}

# vim: ts=4 sw=4 noet :