From 2a7eb7221b85af3f2c5141c3918ff1959566f55a Mon Sep 17 00:00:00 2001 From: Einhard Leichtfuß Date: Fri, 30 Mar 2018 22:08:42 +0200 Subject: Split some functionality off update.sh Such that it may be used by the to be bettered make_install.sh. In particular, the latter is supposed to work for both non-vcs source and binary packages. Also add a main() method to update.sh. --- basic.sh | 49 +++++++++++++++++++++++++++++++++ make_install.sh | 15 +++++++++++ sample.install | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/make_install.sh | 15 ----------- src/sample.install | 78 ----------------------------------------------------- update.sh | 48 ++++++--------------------------- 6 files changed, 150 insertions(+), 133 deletions(-) create mode 100644 basic.sh create mode 100755 make_install.sh create mode 100644 sample.install delete mode 100755 src/make_install.sh delete mode 100644 src/sample.install diff --git a/basic.sh b/basic.sh new file mode 100644 index 0000000..65b44e5 --- /dev/null +++ b/basic.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +function init +{ + tmp_d="$(mktemp -d)" + tmp="$tmp_d/a" + tmp_="$tmp_d/b" +} + +function cleanup +{ + rm -r "$tmp_d" +} +trap cleanup EXIT + +# $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" +} diff --git a/make_install.sh b/make_install.sh new file mode 100755 index 0000000..a24837f --- /dev/null +++ b/make_install.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +if [ "$#" -lt 1 ] +then + echo "Usage: $0 xy-zw [base_dir]" >&2 + exit 1 +elif [ "$#" -gt 1 ] +then + cd "$2" || exit 1 +fi + +lang=$1 +pkgname=dict-freedict-${lang} + +sed "s/%LANG%/${lang}/g" sample.install > ${pkgname}/${pkgname}.install diff --git a/sample.install b/sample.install new file mode 100644 index 0000000..7ce3ba2 --- /dev/null +++ b/sample.install @@ -0,0 +1,78 @@ +basename=%LANG% +pkgname=dict-freedict-$basename%SUFFIX% +dictd_conf=/etc/dict/dictd.conf +datadir=/usr/share/dictd +conf="database $basename { + data $datadir/$basename.dict.dz + index $datadir/$basename.index +}" + +post_install() +{ + echo + if pacman -Qq dictd > /dev/null 2>&1 + then + if grep -q "^database *$basename" "$dictd_conf" + then + echo "$pkgname already configured in $dictd_conf" + else + echo "Adding configuration for $pkgname to $dictd_conf" + echo "$conf" >> "$dictd_conf" + fi + + if systemctl -q is-active dictd.service + then + echo "Restarting dictd service in order to" \ + "use the new dictionary database" + systemctl restart dictd.service + else + echo "Starting dictd service in order to" \ + "use the new dictionary database" + systemctl start dictd.service + fi + else + echo "dictd does not appear to be installed." + echo "In order to use this database you should either" \ + "install dictd or alternatively" \ + "another dict server and configure it on your own." + fi + echo +} + +post_upgrade() +{ + if pacman -Qq dictd > /dev/null 2>&1 && \ + systemctl -q is-active dictd.service + then + echo -e "\nRestarting dictd service in order to" \ + "use the updated dictionary database" + systemctl restart dictd.service + fi +} + +post_remove() +{ + if pacman -Qq dictd > /dev/null 2>&1 + then + current_conf="$(grep -A 3 "^database *$basename" "$dictd_conf")" + if test -n "$current_conf" + then + echo + if test "$current_conf" = "$conf" + then + echo "Removing configuration for $pkgname from $dictd_conf" + sed -i "/database $basename {/,/}/d" "$dictd_conf" + else + echo "User created / modified configuration" \ + "for $pkgname in $dictd_conf is left untouched." + fi + fi + + if systemctl -q is-active dictd.service + then + echo "Restarting dictd service in order to" \ + "stop using the removed dictionary database" + systemctl restart dictd.service + fi + fi +} diff --git a/src/make_install.sh b/src/make_install.sh deleted file mode 100755 index a24837f..0000000 --- a/src/make_install.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -if [ "$#" -lt 1 ] -then - echo "Usage: $0 xy-zw [base_dir]" >&2 - exit 1 -elif [ "$#" -gt 1 ] -then - cd "$2" || exit 1 -fi - -lang=$1 -pkgname=dict-freedict-${lang} - -sed "s/%LANG%/${lang}/g" sample.install > ${pkgname}/${pkgname}.install diff --git a/src/sample.install b/src/sample.install deleted file mode 100644 index 882fbf5..0000000 --- a/src/sample.install +++ /dev/null @@ -1,78 +0,0 @@ -basename=%LANG% -pkgname=dict-freedict-$basename -dictd_conf=/etc/dict/dictd.conf -datadir=/usr/share/dictd -conf="database $basename { - data $datadir/$basename.dict.dz - index $datadir/$basename.index -}" - -post_install() -{ - echo - if pacman -Qq dictd > /dev/null 2>&1 - then - if grep -q "^database *$basename" "$dictd_conf" - then - echo "$pkgname already configured in $dictd_conf" - else - echo "Adding configuration for $pkgname to $dictd_conf" - echo "$conf" >> "$dictd_conf" - fi - - if systemctl -q is-active dictd.service - then - echo "Restarting dictd service in order to" \ - "use the new dictionary database" - systemctl restart dictd.service - else - echo "Starting dictd service in order to" \ - "use the new dictionary database" - systemctl start dictd.service - fi - else - echo "dictd does not appear to be installed." - echo "In order to use this database you should either" \ - "install dictd or alternatively" \ - "another dict server and configure it on your own." - fi - echo -} - -post_upgrade() -{ - if pacman -Qq dictd > /dev/null 2>&1 && \ - systemctl -q is-active dictd.service - then - echo -e "\nRestarting dictd service in order to" \ - "use the updated dictionary database" - systemctl restart dictd.service - fi -} - -post_remove() -{ - if pacman -Qq dictd > /dev/null 2>&1 - then - current_conf="$(grep -A 3 "^database *$basename" "$dictd_conf")" - if test -n "$current_conf" - then - echo - if test "$current_conf" = "$conf" - then - echo "Removing configuration for $pkgname from $dictd_conf" - sed -i "/database $basename {/,/}/d" "$dictd_conf" - else - echo "User created / modified configuration" \ - "for $pkgname in $dictd_conf is left untouched." - fi - fi - - if systemctl -q is-active dictd.service - then - echo "Restarting dictd service in order to" \ - "stop using the removed dictionary database" - systemctl restart dictd.service - fi - fi -} diff --git a/update.sh b/update.sh index a3d3863..c36b556 100755 --- a/update.sh +++ b/update.sh @@ -46,39 +46,16 @@ prepare[deu-fra-bin]=\ } ' -# $1: string to replace: regular expression -# $2: string to replace with: sed replacement string -function replace -{ - sed -i "s/${1}/${2}/g" "$tmp" -} +. basic.sh -# $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 +function main { - 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 -} + curl -sO http://freedict.org/freedict-database.json -# $1 line to delete: regular expression -function delete_line -{ - sed -i "/${1}/ d" "$tmp" + init + handle_all src '' src "$@" + handle_all bin -bin dictd "$@" + cleanup } function update @@ -90,10 +67,6 @@ function update 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" @@ -114,8 +87,6 @@ function update mv "$tmp" PKGBUILD makepkg --printsrcinfo > .SRCINFO - - rm -r "$tmp_d" } # $1 type: (src|bin) @@ -168,7 +139,4 @@ function handle_all } -curl -sO http://freedict.org/freedict-database.json - -handle_all src '' src "$@" -handle_all bin -bin dictd "$@" +main "$@" -- cgit v1.2.3