commit f4ea9fd39b6cee6339924c96c87a16d4b4f7f846
parent 30c9a7cccd564ff094ef5801da13d06f9ba64049
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Sun, 14 Feb 2021 14:55:37 -0800
Rewrite sysconf script in sh(1) much better
The previous version was unreadable and clunky, this one is halfway
decent and does roughly the same thing, it also splits listing
installed files into a separate command, `check'.
Diffstat:
1 file changed, 42 insertions(+), 73 deletions(-)
diff --git a/modules/scripts/.local/bin/sysconf b/modules/scripts/.local/bin/sysconf
@@ -1,80 +1,49 @@
-#!/usr/local/bin/rc -e
-#! sysconf -- copy or remove files from a list of files
-#!
-#! There are three commands defined, h(elp), i(nstall), u(ninstall), and
-#! l(ist) or ls. The list command is default.
-#!
-#! Each item on the file list not prefixed by hash (`#') and not empty
-#! is evaluated with rc(1)'s eval then executed. The format of each
-#! item should be `$src[<tab><space>]+$dst'.
-#!
-#! Configuration files are takin in the following order: first
-#! `sysconf', then `Sysconf', and finaly `.sysconf'.
+#!/bin/sh -e
+# Copywrite 2021 Jacob R. Edwards
+# License: GPLv3
+#
+# sysconf -- install file from a file list
+#
+# sysconf preprocesses a list of source destination pairs (separated
+# by whitespace) from the file `sysconf' with m4(1) and proceeds
+# to perform operations on it based on the command given. A command
+# can be one of the following:
+#
+# l[ist] List source and destinations
+# c[heck] List installed files
+# i[nstall] Install files
+# u[ninstall] Uninstall files
-__progname = `{ basename $0 }
-
-# utility
-fn warn { printf '%s: ' $__progname && printf $* >[2=1] }
-fn err { warn $*; exit 1 }
+process() {
+ egrep -ve '^#' -e '^$' $* | m4
+}
-# config processing
-fn filter { grep -ve '^#' -e '^$' $* }
-fn match { egrep $* }
+list() column -t $*
-# command execution
-fn intern { }
-fn execute {
- if (test $#* -ne 2) {
- warn 'execute: Invalid argument count (%d).\n' $#*
- return 1
- }
- intern $*
-}
-fn list_configs {
- stest -e sysconf Sysconf .sysconf || {
- if (test $status -eq 1)
- err 'No config file.\n';
- exit 1
- }
+check() {
+ awk '{ print $NF }' $* | stest -e
}
-if (test $#* -lt 1)
- * = ('ls');
-switch ($1) {
- case i install
- fn intern { cp -vr $1 $2 }
- case u uninstall
- fn intern {
- if (test -d $1) {
- rm -vr $2
- } else {
- rm -v $2;
- }
- }
- case l ls list
- fn intern {
- suffix = ''
- if (test -e $2)
- suffix = ' (installed)'
- printf '%s -> %s%s\n' $1 $2 $suffix
- }
- case h help
- grep '^#! ' $0 | cut -c4- >[2=1]
- exit 1
- case *
- err '''%s'': Invalid command.\n' $1
+install() {
+ while read line
+ do
+ cp -r $line
+ done
}
-config = `{ list_configs | head -1 }
-ifs = '
-'
-if (test $#* -eq 1) {
- commands = `{ filter $config }
-} else if (test $#* -eq 2) {
- commands = `{ filter $config | match $2 }
-} else
- err '''%d'': Too many arguments given.\n' $#*;
-
-ifs = ' ' # <space><tab>
-for (command in $commands)
- eval execute $command
+uninstall() rm -r `check $*`
+
+process <sysconf | case $1
+in
+(l|list)
+ list;;
+(c|check)
+ check;;
+(i|install)
+ install;;
+(u|uninstall)
+ uninstall;;
+(*)
+ printf 'usage: %s [list|check|install|uninstall]\n' `basename $0` 1>&2
+ exit 1;;
+esac