#!/bin/env fish
set -l dir (status dirname)
source "$dir/../../lib/action.fish"

function backupAction -V dir
    source "$dir/../../lib/hooks.fish"
    source "$dir/../../lib/restoration.fish"

    if not type -q getDeploymentScript
        function getDeploymentScript
            echo "No deployment script specified! No software will be installed." 1>&2
            false
        end
    end

    set -l deployScript (getDeploymentScript)
    initBackupConfig --action backup

    runHook backupSoftware || begin
        echo "Backing up software..."

        and if [ -n "$deployScript" ]
            source $deployScript backup
        end
    end

    runHook backupUsers || begin
        if [ -n "$deployScript" ]
            for name in (getUsers | jq '.[]' --raw-output0 | string split0 || true)
                echo "Backing up user `$name`..."
                and source $deployScript userBackup --user $name
            end
        end
    end
end

runSetupUserAction backupAction