#!/bin/bash

# File: HelloWorld.bash
# 
# Description: This file exemplifies printing 'Hello world!' in bash. 
# 
# Package: AniNIX/HelloWorld
# Copyright: WTFPL
# 
# Author: DarkFeather <ircs://aninix.net:6697/DarkFeather>

# Should only be done as root.
if [ "$(whoami)" != "root" ]; then
    echo Needs to be run as root.
    exit 1;
fi

# Default the host to the Aether host in whatever domain this host lives in.
host="$1"
if [ -z "$host" ]; then
    host=Aether
fi

# Default the path to /
path="$2"
if [ -z "$path" ]; then
    path='/'
fi

# Try to contract the host
if (nmap --open -p 22 "$host" | grep -E '22/tcp\s+open' &>/dev/null); then

    # Confirm with the user
    printf "Are you sure you want to back everything up to %s?\n* Is the root password set and backup volume mounted on '/mnt'?\n* Do you have time for this backup to complete?\n* Does \`ssh-keyscan -D localhost\` on the server match this? `ssh-keyscan -D "$host" 2>/dev/null | grep 4\ 2`\nEnter YES in all capitals to continue..." "$host"

    read answer
    if [ "$answer" != "YES" ]; then
        echo User did not confirm.
        exit 1;
    else
        rsync -aAXv --delete --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} "$path" root@"$host":/mnt"$path"
        status="$?"
        echo rsync exited with status $status
        exit $status
    fi
else
    echo "Host is not responding in a timely fashion."
    exit 1;
fi
