#!/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 . # # All methods operate on the file "$tmp". script_path="$(realpath "$0")" script_dir="${script_path%/*}" . "${script_dir}/general-config.sh" || exit 1 . "${script_dir}/user-config.sh" || exit 1 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" }