diff --git a/examples/archlinux-gnome/masterconf.sh b/examples/archlinux-gnome/masterconf.sh index 6e19c12f644559d71b3311ebea0ee552e11c4c74..6486203a316dea4fcdfc1bdac56e385f60bd8122 100644 --- a/examples/archlinux-gnome/masterconf.sh +++ b/examples/archlinux-gnome/masterconf.sh @@ -2,33 +2,3 @@ lc_include conf.d/* -lc_assert_user_is root - -# I strongly suggest u design lc_init() as an idempotent operation. Just in case u accidentally run `linuxconf register` elsewhere. -function lc_init () { - # as root / as user? - - # note: in demo, append /etc/profile for PATH -} - -function lc_startup () { - # as root / as user? - # with desktop env? (not supported yet) - -} - -function lc_login () { - # warning: less useful. happens again if user logout/login again. - # (no plan to support in first ver) - lc_login_is_x11? -} - -function lc_cron () { - # hourly / daily / weekly / monthly? - # as root / as user? - -} - -# Warning: watch out for unintended user -lc_fsmap files/vimrc $LC_USER_HOME/.vimrc - diff --git a/linuxconf b/linuxconf index 7a7444369b6d1734e17d63cc283657047f5ad362..40af03aaad75c6c152f342b74ba5c8e2b740fddc 100755 --- a/linuxconf +++ b/linuxconf @@ -25,9 +25,11 @@ function lc_include () { # print the return code of the eval-ed function to stderr, but this function should always return success. for script in "$@"; do + [[ -f "$script" ]] || err "ERROR: DEBUG: script not exist >>$script<<" || continue echo2 "$script -- $LCI_SUBSHELL_OP started as $(whoami), at $(pwd)" ( source "$script" + declare -F "$LCI_SUBSHELL_OP" >/dev/null 2>&1 || exit 0 eval "$LCI_SUBSHELL_OP" ) echo2 "$script -- $LCI_SUBSHELL_OP completed with status $return_code" @@ -41,7 +43,7 @@ function lci_fsmap () { # Otherwise, delete the existing symlink. If the existing item is file/folder, move to '$filename.linuxsync_backup'. # Then create a symlink. - local content_path="$(realpath "$1")" + local content_path="$(realpath "$1")" || return $? local symlink_path="$2" if [ -L "$symlink_path" ]; then @@ -69,7 +71,7 @@ function lci_state_file_append () { local prefix="$2" local uname="$3" - echo "${prefix}_u_$uname=1" | tee -a "$fname" + echo "${prefix}_u_$uname=1" | tee -a "$fname" > /dev/null return $? } function lci_state_file_contains () { @@ -77,7 +79,7 @@ function lci_state_file_contains () { local prefix="$2" local uname="$3" - grep "^${prefix}_u_$uname=1" "$fname" + grep "^${prefix}_u_$uname=1" "$fname" > /dev/null return $? } function lci_state_file_list () { @@ -90,25 +92,27 @@ function lci_state_file_list () { return $? } +function lci_conf_get_masterconf_path () { + local fname="$1" # lc config path + grep ^masterconf= "$fname" | sed s/^masterconf=// +} function lci_overwrite_conf () { local fname="$1" # (output) lc config path local confpath="$2" # user masterconf script path - local newconf="masterconf=$(realpath "$confpath")" + local newpath="$(realpath "$confpath")" + [ "$newpath" != "" ] || err "lci_overwrite_conf: cannot unfold path $confpath. Permission error?" || return $? # Don't re-create if old config already looks good. In this case, init_done should be preserved. TODO: rename lc register to lc init, then should I remove this behavior?? if [[ -f "$fname" ]]; then - local oldconf="$(grep ^masterconf= "$fname" 2>/dev/null)" - [[ "$oldconf" = "$newconf" ]] && return 0 + local oldpath="$(lci_conf_get_masterconf_path $fname)" + [[ "$oldpath" = "$newpath" ]] && return 0 fi - echo "$newconf" | tee "$fname" || err "lci_overwrite_conf: unable to create $fname" || return $? + echo "masterconf=$newpath" | tee "$fname" > /dev/null || err "lci_overwrite_conf: unable to create $fname" || return $? chmod ugo+rw "$fname" } -function lci_conf_get_masterconf_path () { - local fname="$1" # lc config path - grep ^masterconf= "$fname" | sed s/^masterconf=// -} + function lci_register () { local confpath="$1" @@ -121,6 +125,7 @@ function lci_register () { function lci_init_if_needed () { local uname="$(whoami)" lci_state_file_contains /etc/linuxconf.conf init_done "$uname" && return 0 + echo2 "RDEBUG: init needed for $uname" # call lc_init() export LCI_SUBSHELL_OP=lc_init @@ -141,7 +146,7 @@ function lci_startup_if_needed () { touch $state_file && chmod ugo+rw $state_file || die "failed to create tmp file $state_file" fi - lci_state_file_contains $state_file startup_done "$uname" && continue + lci_state_file_contains $state_file startup_done "$uname" && return 0 # call lc_startup() export LCI_SUBSHELL_OP=lc_startup