summaryrefslogtreecommitdiff
path: root/scripts/pacman-db-upgrade.sh.in
blob: b0b0ac80b06de1540c1eca9b09f40632bce78c7c (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
#!/bin/bash -e
#
#   pacman-db-upgrade - upgrade the local pacman db to a newer format
#   @configure_input@
#
#   Copyright (c) 2010-2014 Pacman Development Team <pacman-dev@archlinux.org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# gettext initialization
export TEXTDOMAIN='pacman-scripts'
export TEXTDOMAINDIR='@localedir@'

declare -r myver='@PACKAGE_VERSION@'

eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf)
dbroot="${DBPath:-@localstatedir@/lib/pacman/}"

USE_COLOR='y'

m4_include(library/output_format.sh)

usage() {
	printf "pacman-db-upgrade (pacman) %s\n" "${myver}"
	echo
	printf -- "$(gettext "Upgrade the local pacman database to a newer format")\n"
	echo
	printf -- "$(gettext "Usage: %s [--nocolor] [pacman_db_root]")\n" "$0"
}

version() {
	printf "pacman-db-upgrade (pacman) %s\n" "$myver"
	printf -- "$(gettext "\
Copyright (c) 2010-2014 Pacman Development Team <pacman-dev@archlinux.org>.\n\
This is free software; see the source for copying conditions.\n\
There is NO WARRANTY, to the extent permitted by law.\n")"
}

die() {
	error "$@"
	exit 1
}

die_r() {
	rm -f "$lockfile"
	die "$@"
}

# PROGRAM START

# determine whether we have gettext; make it a no-op if we do not
if ! type gettext &>/dev/null; then
	gettext() {
		echo "$@"
	}
fi

if [[ $1 = "-h" || $1 = "--help" ]]; then
	usage
	exit 0
fi

if [[ $1 = "-V" || $1 = "--version" ]]; then
	version
	exit 0
fi

if [[ $1 = "--nocolor" ]]; then
	USE_COLOR='n'
	shift
fi

m4_include(library/term_colors.sh)

if [[ -n $1 ]]; then
	dbroot="$1"
fi

if [[ ! -d $dbroot ]]; then
	die "$(gettext "%s does not exist or is not a directory.")" "$dbroot"
fi

if [[ ! -d $dbroot/local ]]; then
	die "$(gettext "%s is not a pacman database directory.")" "$dbroot"
fi

if [[ ! -w $dbroot ]]; then
	die "$(gettext "You must have correct permissions to upgrade the database.")"
fi

# strip any trailing slash from our dbroot
dbroot="${dbroot%/}"
# form the path to our lockfile location
lockfile="${dbroot}/db.lck"

# make sure pacman isn't running
if [[ -f $lockfile ]]; then
	die "$(gettext "Pacman lock file was found. Cannot run while pacman is running.")"
fi
# do not let pacman run while we do this
touch "$lockfile"

# pacman-3.4 to 3.5 upgrade - merge depends into desc
if [[ $(find "$dbroot"/local -name depends) ]]; then
	msg "$(gettext "Pre-3.5 database format detected - upgrading...")"
	for i in "$dbroot"/local/*; do
		if [[ -f "$i"/depends ]]; then
			cat "$i"/depends >> "$i"/desc
			rm "$i"/depends
		fi
	done
	msg "$(gettext "Done.")"
fi

# remove the lock file
rm -f "$lockfile"

# vim: set noet: