aboutsummaryrefslogtreecommitdiff
path: root/update.sh
blob: a032671a8f49001917525f05b7f8132d9be4bfb4 (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
#!/usr/bin/env bash
#
# update.sh - script to check for updates and perform them (on the PKGBUILDs)
#
# Copyright 2018 Einhard Leichtfuß
#
# This file is part of aur-fd-scripts
#
# aur-fd-scripts is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# aur-fd-scripts 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with aur-fd-scripts.  If not, see <https://www.gnu.org/licenses/>.
#

# usage: $0 dir type [--nopull] [-u xyz-uvw|-ua|-uaf]
# $1 base directory: string
# $2 type: (src|bin|svn)
# $4 option: --nopull {can be omitted}
# ${4,5} update force mode: (-u lang1-lang2|-ua|-uaf) {can be omitted}
#     -u x: Update x, even if it appears up to date.
#     -ua:  Update all that are out of date.
#     -uaf: Update all, even if they appear up to date.


script_path="$(realpath "$0")"
script_dir="${script_path%/*}"

. "${script_dir}/basic.sh" || exit 1


function main
{
	if [ "$#" -lt 2 ]
	then
		echo "Usage: $0 <base_dir> <type> [--nopull] [-u <xy>-<zw>|-ua|-uaf]" >&2
		exit 1
	else
		cd "$1" || exit 1
	fi

	type="$2"

	if [ "$3" = "--nopull" ]
	then
		nopull=true
		shift 3
	else
		nopull=false
		shift 2
	fi


	init
	if [[ "$type" == src ]]
	then
		handle_all src ''   src   "$@"
	elif [[ "$type" == bin ]]
	then
		handle_all bin -bin dictd "$@"
	elif [[ "$type" == svn ]]
	then
		handle_all_vcs svn -svn "$@"
	else
		echo "Type $type not supported." >&2
		exit 1
	fi
}


# Update PKGBUILD and .SRCINFO
#
# requires: $lang, $ext, $type, $ver, $convver to be set;
#           $PWD == <base directory>/<type>/<lang>
#
function update
{
	echo "Updating ${lang}${ext} (${pkgver} -> ${convver})..."

	lang_a="${long[${lang%-*}]}"
	lang_b="${long[${lang#*-}]}"

	input "${script_dir}/${type}/sample.PKGBUILD"

	replace '%MAINTAINER%' "$maintainer"
	replace '%LANG%' "$lang"
	replace '%LANG_A%' "$lang_a"
	replace '%LANG_B%' "$lang_b"
	replace '%VER%' "$convver"
	replace '%LICENSES%' "${licenses[$lang]}"
	replace '%CHECKSUM%' "$checksum"

	replace_line '%CONTRS%' "${contrs[${lang}${ext}]}" normal

	if [[ "$ver" == "$convver" ]]
	then
		delete_line '^_pkgver=\$\{pkgver\/\/_\/-\}$'
		replace '_pkgver' 'pkgver'
	fi

	replace_line '%PREPARE%' "${prepare[${lang}${ext}]}" rm-plus-one

	write PKGBUILD
	makepkg --printsrcinfo > .SRCINFO
}


# Check for updates for src / bin packages and optionally update them.
#
# $1 type: (src|bin)
# $2 suffix: (|-bin)
# $3 platform: (src|dictd)
# ${@:4} args: $@
#
# requires: $PWD == <base directory>
#
function handle_all
{
	type="$1"
	ext="$2"
	platform="$3"
	shift 3

	curl -sO https://freedict.org/freedict-database.json || exit 1

	cd "$type" || exit 1

	for lang in ${langs[@]}
	do
		cd $lang || exit 1
		$nopull || git pull -q
		pkgver="$(sed -En 's/\s*pkgver\s*=\s*(.+)\s*/\1/p' .SRCINFO)"
		data="$(jq -r ".[] | select(.name == \"${lang}\") | .releases[] | select(.platform==\"$platform\")" ../../freedict-database.json)"
		ver="$(jq -r '.version' <<< "$data")"
		url="$(jq -r '.URL' <<< "$data")"
		checksum="$(jq -r '.checksum' <<< "$data")"
		convver="${ver//-/_}"

		if [[ "$pkgver" != "$convver" ]]
		then
			echo "${lang}${ext} requires update (${pkgver} -> ${convver})."
			ureq=true
		else
			ureq=false
		fi

		if [[ "$url" != "https://download.freedict.org/dictionaries/${lang}/${ver}/freedict-${lang}-${ver}.${platform}.tar.xz" ]]
		then
			echo "${lang}${ext} URL changed (${url})."
		else
			if [[ "$1" == "-u" && "$2" == "$lang" ]] \
				|| ([[ "$1" == "-ua" ]] && $ureq) \
				|| [[ "$1" == "-uaf" ]]
			then
				update
			fi
		fi

		cd ..
	done

	cd ..
}


# Check for updates for VCS (svn) packages and optionally update them.
#
# $1 type: (svn)
# $2 suffix: (-svn)
# ${@:3} args: $@
#
# requires: $PWD == <base directory>
#
function handle_all_vcs
{
	type="$1"
	ext="$2"
	shift 2

	cd "$type" || exit 1

	for lang in ${vcs_langs[@]}
	do
		cd $lang || exit 1
		$nopull || git pull -q
		pkgver="$(sed -En 's/\s*pkgver\s*=\s*(.+)\s*/\1/p' .SRCINFO)"
		makepkg --nobuild --nodeps > /dev/null 2>&1
		ver="$(makepkg --printsrcinfo \
			| sed -En 's/\s*pkgver\s*=\s*(.+)\s*/\1/p')"
		convver="$ver"

		if [[ "$1" == "-u" && "$2" == "$lang" ]] || [[ "$1" == "-uaf" ]]
		then
			update
		fi

		cd ..
	done

	cd ..
}


main "$@"