#!/bin/bash

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 
   exit 1
fi

if ! command -v curl &> /dev/null ; then
    echo "Please install curl first"
    exit 1
fi
if ! command -v bunzip2 &> /dev/null ; then
    echo "Please install bzip2 first"
    exit 1
fi

if [[ -z "${RESTIC_REPOSITORY+x}" ]]; then
    echo "Please set the RESTIC_REPOSITORY variable"
    echo "curl -sSL https://rx.zevs.me/linux/generic/installers/restic.sh | sudo RESTIC_REPOSITORY=<restic repository> bash"
    exit 1
fi

get_latest_release() {
  curl --silent "https://api.github.com/repos/$1/releases/latest" | # Get latest release from GitHub api
    grep '"tag_name":' |                                            # Get tag line
    sed -E 's/.*"([^"]+)".*/\1/'                                    # Pluck JSON value
}

echo ">>> Getting basic info"

RELEASE=$(get_latest_release restic/restic)

if [[ -z "${RELEASE+x}" ]]; then
    echo "Failed to get latest release, do you have an internet connection?"
    exit 1
fi

VERSION=$(awk -v rel="${RELEASE}" 'BEGIN {print substr(rel,2);}')

ARCH=""
case $(uname -m) in
    i386|i686) ARCH="386" ;;
    x86_64) ARCH="amd64" ;;
    arm) ARCH="arm" ;;
    aarch64) ARCH="arm64" ;;
esac
if [[ -z "${ARCH+x}" ]]; then
    echo "Unknown architecture, please install manually"
    exit 1
fi

echo ">>> Downloading latest Restic release to /opt/restic"
    
mkdir /opt/restic
curl -sSL "https://github.com/restic/restic/releases/download/${RELEASE}/restic_${VERSION}_linux_${ARCH}.bz2" | bunzip2 > /opt/restic/restic

chmod -R 0755 /opt/restic/

echo ">>> Initializing repository"

RESTIC_PASSWORD=$(date +%s | sha256sum | base64 | head -c 64)
# read -p 'Restic Repo: ' RESTIC_REPOSITORY

/opt/restic/restic -r "${RESTIC_REPOSITORY}" --password-command "echo ${RESTIC_PASSWORD}" init

if [ ! $? -eq 0 ]; then
    echo "Please check error message above and re-run this script after fixing the errors"
    exit 1
fi

echo ">>> Saving baseline configurations to /etc/restic"

mkdir -p /etc/restic

cat <<EOF > /etc/restic/restic.include
/
/boot
/boot/efi
EOF

cat <<EOF > /etc/restic/restic.exclude
/dev/*
/lost+found/*
/media/*
/mnt/*
/proc/*
/sys/*
/tmp/*

# Apt cache
/var/cache/apt/*

# Pacman cache
/var/cache/pacman/*

# Yum cache
/var/cache/yum/*
EOF

cat <<EOF > /etc/restic/restic.conf
RESTIC_PASSWORD=${RESTIC_PASSWORD}
RESTIC_REPOSITORY=${RESTIC_REPOSITORY}
EOF

echo -e ">>> Setting up cron jobs"

crontab -l > /opt/restic/tmp_cron 2> /dev/null
cat << EOF >> /opt/restic/tmp_cron
# Restic
0 4 * * * bash /opt/restic/restic-autobackup
0 2 * * 0 /opt/restic/restic self-update
EOF
crontab /opt/restic/tmp_cron
rm /opt/restic/tmp_cron

echo ">>> Creating auto-backup script"

cat << EOF > /opt/restic/restic-autobackup
#!/bin/bash
if [[ \$EUID -ne 0 ]]; then
   echo "This script must be run as root"
   exit 1
fi

source /etc/restic/restic.conf
/opt/restic/restic backup -r "\${RESTIC_REPOSITORY}" --password-command "echo \${RESTIC_PASSWORD}" --files-from /etc/restic/restic.include --exclude-file /etc/restic/restic.exclude --one-file-system
EOF
chmod +x /opt/restic/restic-autobackup

echo ">>> Creating symlinks to /usr/local/bin/"

ln -s /opt/restic/restic /usr/local/bin/restic

echo ">>> Done! Please read the following carefully"

echo -e "\nPlease edit /etc/restic/restic.exclude and exclude your package manager's cache directory"
echo "Please edit /etc/restic/restic.include and add your boot/efi partition (i.e. /boot/efi)"
echo "If you have any other important directories that are *not* on the root filesystem, add them to /etc/restic.include"
echo "/etc/restic/restic.conf contains env variables for configuring restic. Please edit this if you want to change parameters"
echo -e "\nTo update restic, please run restic self-update as root"
echo "A scheduled task has been set up to do this automatically"

echo -e "\n\nPlease back up the following values:"
cat /etc/restic/restic.conf