diff options
author | Einhard Leichtfuß <alguien@respiranto.de> | 2018-03-30 16:03:12 +0200 |
---|---|---|
committer | Einhard Leichtfuß <alguien@respiranto.de> | 2018-03-30 16:03:12 +0200 |
commit | 9d45fe6b329cbae295ed83d90dd20c5fc3e70717 (patch) | |
tree | 2cade18b271a104cf6a6a295e5d15d8fc623566f /update.sh | |
parent | 2a8e89e412a0cb344c62aee455d522cd983be1d7 (diff) |
Modularise and generalise
Modularisation:
- Use new functions replace(), replace_line() and delete_line() instead
of sed directly.
This required all editing to be done in-line or pseudo in-line, i.e.
moving files afterwards.
Generalisation:
- Make it work for both the non-vcs source and the binary packages, not
only the former.
A separate function handle_all() has been set up to handle both types.
Further:
- Require a directory to be supplied as argument due to a variable
number of further arguments.
- Use a temporary directory instead of several files.
- Add option -uaf to forcibly update everything.
Diffstat (limited to 'update.sh')
-rwxr-xr-x | update.sh | 174 |
1 files changed, 174 insertions, 0 deletions
diff --git a/update.sh b/update.sh new file mode 100755 index 0000000..a3d3863 --- /dev/null +++ b/update.sh @@ -0,0 +1,174 @@ +#!/bin/bash +# usage: $0 dir [-u xyz-uvw|-ua|-uaf] + +cd "$1" || exit 1 +shift + +typeset -a langs +langs=( + fra-deu deu-fra + deu-eng eng-deu + fra-eng eng-fra + spa-eng eng-spa + ) + +typeset -A long +long[fra]=French +long[deu]=German +long[eng]=English +long[spa]=Spanish + +typeset -A contrs +contrs[deu-eng-bin]=\ +'# Contributor: akane <grangerspit@gmail.com> <xmpp:heiß@neko.im>'$'\n' +contrs[eng-deu-bin]=\ +'# Contributor: akane <grangerspit@gmail.com> <xmpp:heiß@neko.im>'$'\n' +contrs[fra-eng-bin]=\ +'# Contributor: Benjamin Vanderford <hazor_at_swrpg_dot_info>'$'\n' +contrs[eng-fra-bin]=\ +'# Contributor: Benjamin Vanderford <hazor_at_swrpg_dot_info>'$'\n' + +typeset -A prepare +prepare[deu-fra]=\ +'prepare() +{ + cd $_lang + sed -i '"'"'s/\(10\)\(100\>.*1 mit 100 Nullen\)/\1\^\2/'"'"' ${_lang}.tei +} +' +prepare[deu-fra-bin]=\ +'prepare() +{ + cd $_lang + dictzip -d ${_lang}.dict.dz + sed -i '"'"'s/\(10\)\(100\>.*1 mit 100 Nullen\)/\1\^\2/'"'"' ${_lang}.dict + dictzip ${_lang}.dict +} +' + +# $1: string to replace: regular expression +# $2: string to replace with: sed replacement string +function replace +{ + sed -i "s/${1}/${2}/g" "$tmp" +} + +# $1 line to replace: regular expression +# $2 text to replace with: (newline terminated) sed replacement string +# $3 removal type: (rm-plus-one|normal) +function replace_line +{ + if test -n "$2" + then + cat <(sed "/${1}/,\$ d" < "$tmp") \ + <(echo -n "$2") \ + <(sed "0,/${1}/ d" < "$tmp") \ + > "$tmp_" + mv "$tmp_" "$tmp" + else + if [[ "$3" == rm-plus-one ]] + then + sed -i "/${1}/,+1 d" "$tmp" + else + sed -i "/${1}/ d" "$tmp" + fi + fi +} + +# $1 line to delete: regular expression +function delete_line +{ + sed -i "/${1}/ d" "$tmp" +} + +function update +{ + echo "Updating ${lang}${ext} (${pkgver} -> ${convver})..." + + lang_a="${long[${lang%-*}]}" + lang_b="${long[${lang#*-}]}" + + checksum="$(wget -qO - "$url" | sha512sum | sed -E 's/\s*-$//')" + + tmp_d="$(mktemp -d)" + tmp="$tmp_d/a" + tmp_="$tmp_d/b" + + cp ../sample.PKGBUILD "$tmp" + + replace '%LANG%' "$lang" + replace '%LANG_A%' "$lang_a" + replace '%LANG_B%' "$lang_b" + replace '%VER%' "$convver" + 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 + + mv "$tmp" PKGBUILD + makepkg --printsrcinfo > .SRCINFO + + rm -r "$tmp_d" +} + +# $1 type: (src|bin) +# $2 suffix: (|-bin) +# $3 platform: (src|dictd) +# ${@:4} args: $@ +function handle_all +{ + type="$1" + ext="$2" + platform="$3" + shift 3 + + cd "$type" + + for lang in ${langs[@]} + do + cd dict-freedict-${lang}${ext} + git pull -q + pkgver="$(grep pkgver .SRCINFO | sed -E 's/\s*pkgver\s*=\s*(.+)\s*/\1/')" + ver="$(jq -r ".[] | select(.name == \"${lang}\") | .releases[] | select(.platform==\"$platform\") | .version" ../../freedict-database.json)" + url="$(jq -r ".[] | select(.name == \"${lang}\") | .releases[] | select(.platform==\"$platform\") | .URL" ../../freedict-database.json)" + url="${url%/download}" + convver="${ver//-/_}" + + if [[ "$pkgver" != "$convver" ]] + then + echo "${lang}${ext} requires update (${pkgver} -> ${convver})." + ureq=true + else + ureq=false + fi + + if [[ "$url" != "https://sourceforge.net/projects/freedict/files/${lang}/${ver}/freedict-${lang}-${ver}.${platform}.tar.xz" ]] + then + echo "${lang}${ext} URL changed (${url})." + else + if [[ "$1" == "-u" && "$2" == "$lang${ext}" ]] \ + || ([[ "$1" == "-ua" ]] && $ureq) \ + || [[ "$1" == "-uaf" ]] + then + update + fi + fi + + cd .. + done + + cd .. +} + + +curl -sO http://freedict.org/freedict-database.json + +handle_all src '' src "$@" +handle_all bin -bin dictd "$@" |