aboutsummaryrefslogtreecommitdiff
path: root/basic.sh
blob: 891d9c56ff2fd41ba48c95c2c0a0c70c02c6c873 (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
#!/usr/bin/env bash
#
# Copyright 2018 Einhard Leichtfuß
#
# basic.sh - script containing the array of the to be generated dictionary's
#            names and some common functionality, mostly wrapping sed.
#
# 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/>.
#

# All methods operate on the file "$tmp".

typeset -a langs
langs=(
	fra-deu deu-fra
	deu-eng eng-deu
	fra-eng eng-fra
	spa-eng eng-spa
	)

typeset -a vcs_langs
vcs_langs=(
	deu-eng eng-deu
	fra-eng eng-fra
	spa-eng eng-spa
	)

function init
{
	tmp_d="$(mktemp -d)"
	tmp="$tmp_d/a"
	tmp_="$tmp_d/b"
}

# $1: file to treat as input: filename
# requires: init to be run
function input
{
	cp "$1" "$tmp"
}

# $1: file to write to: filename
# requires: init and input to be run
function write
{
	cp "$tmp" "$1"
}

function cleanup
{
	rm -rf "$tmp_d"
}
trap cleanup EXIT

# $1: string to replace: extended regular expression
# $2: string to replace with: sed replacement string
function replace
{
	sed -Ei "s/${1}/${2}/g" "$tmp"
}

# Replace line by a string (possibly including newlines).
# $1 line to replace: extended regular expression
# $2 text to replace with: (newline terminated) sed replacement string
# $3 removal type: (rm-plus-one|normal)
#   `- If $2 is empty, whether to remove only that line or also the one
#      following.
function replace_line
{
	if test -n "$2"
	then
		printf '%s' "$2" > "$tmp_"

		sed -Ei "
			/${1}/ {
				r $tmp_
				d
			}
			" "$tmp"
	else
		if [[ "$3" == rm-plus-one ]]
		then
			sed -Ei "/${1}/,+1 d" "$tmp"
		else
			sed -Ei "/${1}/ d" "$tmp"
		fi
	fi
}

# $1 line to delete: extended regular expression
function delete_line
{
	sed -Ei "/${1}/ d" "$tmp"
}