#!/bin/sh export PATH="/usr/bin:/bin" error() { : ${ERR:=1} echo "$@" >&2 return "$ERR" } alt_ab() { if [ "$1" == "a" ]; then echo "b"; else echo "a"; fi } : ${S6_PATH:='/etc/s6'} : ${S6_RC_PATH:="$S6_PATH/rc"} : ${S6_SV_PATH:="$S6_PATH/sv"} # A/B mode, always keep last copy DB_FRESH_ACT="" DB_FRESH_NAC="" cstate() { DB_FRESH_ACT="$(readlink "$S6_SV_PATH/current" | cut -d. -f2)" # if it fails (missing), it will be empty and 'a' will be used as inactive by default DB_FRESH_NAC="$(alt_ab "$DB_FRESH_ACT")" } generate() { mkdir -p "$S6_SV_PATH" || ERR="$?" error "Failed to create sv directory" # A/B current if [ -d "$S6_SV_PATH/current.$DB_FRESH_NAC" ]; then rm -rf "$S6_SV_PATH/current.$DB_FRESH_NAC" || ERR="$?" error "Failed to remove inactive database path" fi s6-rc-compile "$S6_SV_PATH/current.$DB_FRESH_NAC" "$S6_RC_PATH"/* || ERR="$?" error "Failed to compile current s6 database" } update() { s6-rc-update "$S6_SV_PATH/current.$DB_FRESH_ACT" || ERR="$?" "Failed to update live state of the database" } swap() { ln -sfn "current.$DB_FRESH_NAC" "$S6_SV_PATH/current" || ERR="$?" "Failed to update A/B current symlink" } for act in $@; do cstate if [ "$act" == "generate" ]; then generate elif [ "$act" == "update" ]; then update elif [ "$act" == "swap" ]; then swap else error "Action must be generate, update or swap" fi done