summaryrefslogtreecommitdiff
blob: 68c4ac3e66804e6789dbf998b8a099dc9a0aec80 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/bin/bash
# Copyright 2007-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2 or later

# Authors:
# Christian Faulhammer <fauli@gentoo.org>
# Ulrich Müller <ulm@gentoo.org>

VERSION=1.10
EMACS=/usr/bin/emacs
SITELISP=/usr/share/emacs/site-lisp

# Default actions
ACTIONS="sitedir rebuild"

# Default package manager
PM_COMMAND=pm_auto
PM_EXTRAOPTS=( )

# Other default variable settings
BATCH=
COLOUR=
EXACT=
MAJOR=
ORPHANS=
PRETEND=

usage() {
    sed -e 's/^X//' <<-EOF
	Usage: ${0##*/} [OPTION]...
	Rebuild Emacs packages that were compiled by another Emacs version,
	or that are installed in the wrong location.
	X
	X  -a, --action ACTION[,ACTION]...
	X                         specify actions, comma-separated list of:
	X                         'sitedir': find site-init files not in the
	X                           new-style location
	X                         'rebuild': rebuild packages with Elisp files
	X                           byte-compiled by a different Emacs version
	X                         'all': rebuild all packages that have
	X                           byte-compiled Elisp files
	X                         (default: sitedir,rebuild)
	X  -b, --batch            batch mode, don't ask any questions
	X  --color MODE, --colour=MODE
	X                         control colour output. MODE is 'yes', 'no',
	X                         or 'auto'. For 'auto', colour is enabled
	X                         if standard output is to a terminal
	X                         (default: auto)
	X  -e, --exact            match exact versions when remerging packages
	X  -m, --major            use only the major version when comparing
	X                         Emacs version numbers
	X  -o, --orphans          list orphan files (implies '--action all')
	X  -p, --pretend          don't actually emerge packages
	X  -P, --package-manager PM
	X                         select a package manager. PM is one out of
	X                         'portage', 'pkgcore', or 'paludis'
	X                         (default: automatically determined)
	X      --package-manager-command CMD
	X                         call CMD instead of the default package
	X                         manager command. If CMD includes options
	X                         the whole command string must be quoted
	X  -h, --help             display this help and exit
	X      --version          output version information and exit
	X
	X  -- OPTION...           Pass additional options to package manager
	EOF
    exit $1
}

version() {
    cat <<-EOF
	Emacs updater version ${VERSION}
	Copyright 2007-2013 Gentoo Foundation.
	Distributed under the terms of the GNU GPL version 2 or later.

	Gentoo Emacs project: <http://www.gentoo.org/proj/en/lisp/emacs/>
	EOF
    exit
}

# Wrapper for package manager commands
have_portage() { type -P emerge >/dev/null; }
pm_portage() { emerge --oneshot "$@"; }

have_pkgcore() { type -P pmerge >/dev/null; }
pm_pkgcore() { pmerge --oneshot "$@"; }

have_paludis() { type -P cave >/dev/null; }
pm_paludis() { cave resolve --execute --preserve-world "$@"; }

pm_auto() {
    local pm
    # Try environment variable first
    case ${PACKAGE_MANAGER} in
	portage|pkgcore|paludis) pm=${PACKAGE_MANAGER} ;;
    esac
    for pm in ${pm} portage pkgcore paludis; do
	if have_${pm}; then
	    PM_COMMAND=pm_${pm}
	    ${PM_COMMAND} "$@"
	    return
	fi
    done
    failure "No package manager found"
    return 1
}

# Read in all command-line options and force English output
OPTIONS=$(LC_ALL=C getopt -o a:behmnopP: \
    --long action:,batch,color:,colour:,nocolor,nocolour,exact,help,major \
    --long orphans,pretend,package-manager:,package-manager-command:,version \
    -n 'emacs-updater' -- "$@")
[ $? -eq 0 ] || usage 1

eval set -- "${OPTIONS}"

while true
do
    case $1 in
	-h|--help)      usage 0 ;;
	--version)      version ;;
	-b|--batch)     BATCH="true"; shift ;;
	-e|--exact)     EXACT="true"; shift ;;
	-m|--major)     MAJOR="true"; shift ;;
	-o|--orphans)   ORPHANS="true"; ACTIONS="all"; shift ;;
	-p|--pretend)   PRETEND="true"; shift ;;
	--color|--colour)
	    case $2 in
		yes|y|always|force) COLOUR="yes" ;;
		no|n|never|none) COLOUR="no" ;;
		auto|tty|if-tty) COLOUR="" ;;
		*)
		    echo "Invalid argument for '$1' option"
		    usage 20
		    ;;
	    esac
	    shift 2
	    ;;
	-n|--nocolor|--nocolour) COLOUR="no"; shift ;; # legacy option
	-a|--action)
	    ACTIONS=
	    for action in ${2/,/ }; do
		case ${action} in
		    sitedir|rebuild|all)
			ACTIONS="${ACTIONS}${ACTIONS:+ }${action}"
			;;
		    *)
			echo "Invalid action '$action' given!"
			usage 20
			;;
		esac
	    done
	    shift 2
	    ;;
	-P|--package-manager)
	    case $2 in
		auto|automatic) PM_COMMAND=pm_auto ;;
		portage|pkgcore|paludis)
		    if ! have_$2; then
			echo "Package manager '$2' not found!"
			exit 22
		    fi
		    PM_COMMAND=pm_$2
		    ;;
		*)
		    echo "Package manager '$2' not known!"
		    usage 21
		    ;;
	    esac
	    shift 2
	    ;;
	--package-manager-command) PM_COMMAND=$2; shift 2 ;;
	--) shift; PM_EXTRAOPTS=( "$@" ); break ;;
	*)
	    # this should not happen; getopt should return bad status
	    echo "Invalid option '$1' given!"
	    usage 22
	    ;;
    esac
done

# Set colours based on the --colour option and output redirection status
if [ -z "${COLOUR}" -a -t 1 -o "${COLOUR}" = "yes" ]; then
    BOLD=$(tput bold)
    NORMAL=$(tput sgr0)
    RED=$(tput setaf 1)${BOLD}
    GREEN=$(tput setaf 2)${BOLD}
    YELLOW=$(tput setaf 3)${BOLD}
    BLUE=$(tput setaf 4)${BOLD}
    MAGENTA=$(tput setaf 5)${BOLD}
    CYAN=$(tput setaf 6)${BOLD}
else
    BOLD=; NORMAL=; RED=; GREEN=; YELLOW=; BLUE=; MAGENTA=; CYAN=
fi

# Some type of output can be prettified and capsulated in functions
message() {
    local OUTPUT="$@"
    echo "${GREEN}*${NORMAL}${BOLD} ${OUTPUT}${NORMAL}"
}

warning() {
    local OUTPUT="$@"
    echo "${YELLOW}*${NORMAL}${BOLD} ${OUTPUT}${NORMAL}"
}

failure() {
    local OUTPUT="$@"
    echo "${RED}*${NORMAL}${BOLD} ${OUTPUT}${NORMAL}"
}

emacs_version() {
    "${EMACS}" -batch -q --no-site-file --eval "(princ emacs-version)" \
	|| { failure "Cannot run Emacs"; exit 5; }
}

# Get Emacs version from byte-compiled file
bytecomp_version() {
    sed -n '/^[^;]/q;s/\.$//;s/.*[Ee]macs version \([0-9].*\)/\1/p' "$1"
}

action_sitedir() {
    local sf

    message "Searching for site-init files in obsolete location ..."

    # Set nullglob option since there may be no matching files
    local old_shopts=$(shopt -p nullglob)
    shopt -s nullglob

    for sf in "${ROOT}${SITELISP}"/[0-9][0-9]*-gentoo.el
    do
	echo "Found ${sf##*/}"
	echo "${sf}" >> "${TMPFILE}"
    done

    eval "${old_shopts}"
    echo
}

action_rebuild() {
    local active version elc

    message "Searching for byte-compiled elisp files ..."
    active=$(emacs_version)
    [ -n "${active}" ] || { failure "Cannot determine Emacs version"; exit 6; }
    message "Currently selected GNU Emacs version: ${active}"

    for elc in $(find "${ROOT}${SITELISP}" -name "*.elc")
    do
	version=$(bytecomp_version "${elc}")
	version=${version:-unknown}

	if [ -z "${MAJOR}" -a "${version%.*}" != "${active%.*}" ] \
	    || [ "${version%%.*}" != "${active%%.*}" ] \
	    || [ "$1" = all ]
	then
	    echo "Found ${elc##*/} (compiled by Emacs ${version})"
	    echo "${elc}" >> "${TMPFILE}"
	else
	    echo "Skipping ${elc##*/} (compiled by Emacs ${version})"
	fi
    done
    echo
}

action_all() {
    action_rebuild all
}

cleanup() {
    rm -f "${TMPFILE}" "${PKGFILE}"
}

trap cleanup EXIT

TMPFILE="$(mktemp ${TMPDIR:-/tmp}/emacs-updater.files.XXXXXX)"
PKGFILE="$(mktemp ${TMPDIR:-/tmp}/emacs-updater.pkgs.XXXXXX)"

for action in ${ACTIONS}; do
    action_${action}
done

if [ ! -s "${TMPFILE}" ]; then
    warning "No files to update, quitting."
    exit 3
fi

NO_OF_FILES=$(wc -l <"${TMPFILE}")
[ "${NO_OF_FILES}" -eq 1 ] && s= || s=s
message "Assigning ${NO_OF_FILES} file${s} to packages ..."

if [ "${ORPHANS}" ]; then
    qfile -oCR -f "${TMPFILE}" | sort -u > "${PKGFILE}"
elif [ "${EXACT}" ]; then
    qfile -eqCR -f "${TMPFILE}" | sort -u | sed 's/^/=/' > "${PKGFILE}"
else
    # Get package and slot number, requires >=portage-utils-0.3
    qfile -SqCR -f "${TMPFILE}" | sort -u > "${PKGFILE}"
fi

NO_OF_PACKAGES=$(wc -l <"${PKGFILE}")
[ "${NO_OF_PACKAGES}" -eq 1 ] && s= || s=s
if [ "${ORPHANS}" ]; then
    message "${NO_OF_PACKAGES} orphan file${s} found:"
else
    message "${NO_OF_PACKAGES} package${s} to remerge:"
fi

cat "${PKGFILE}"

if [ "${PRETEND}" -o "${ORPHANS}" ]; then
    exit 4
fi

echo
if [ "${NO_OF_PACKAGES}" -eq 0 ]; then
    warning "No packages to update, quitting."
    exit 3
fi

if [ "${BATCH}" ]; then
    message "Remerging packages ..."
else
    echo -n "${BOLD}Remerge packages?${NORMAL} [${GREEN}Yes${NORMAL}/${RED}No${NORMAL}] "
    read choice
    echo
    case ${choice} in
	y*|Y*|"") ;;
	*) warning "Quitting."; exit 10 ;;
    esac
fi

${PM_COMMAND} "${PM_EXTRAOPTS[@]}" $(<"${PKGFILE}")

warning "If a package is being rebuilt over and over again,"
warning "please report it on http://bugs.gentoo.org/"