#!/bin/sh . samizdat-paths.sh error() { printf 'Error: %s\n' "${*:-command failed}" >&2 exit 1 } warning() { printf 'Warning: %s\n' "${*:-something went wrong}" >&2 } store_tmpfs() { local dir="$1" mountpoint -q "$dir" || return 0 [ ! -d "$dir".backup ] || error "existing child backup found: '$dir.backup'. Refusing to continue." cp -a "$dir" "$dir".backup || error "failed to make copy of child." umount "$dir" || error "failed to unmount child tmpfs" rmdir "$dir" || mv -Tb "$dir" "$dir".unlikely mv -T "$dir".backup "$dir" || error "failed to move copied child dir into place. Child will not function!" } assert_root() { [ "$(id -u)" = 0 ] || error "you are not root" } main() { assert_root child=$1 [ "$child" ] || usage if [ -d "$samizdat_child_dir"/child."$child" ]; then store_tmpfs "$samizdat_child_dir"/child."$child" else error "not a directory: $samizdat_child_dir/child.$child" fi } case $# in 0) read child < reused-child && main "$child" ;; *) main "$@" ;; esac