#!/bin/bash -ex
# To run:
# curl -sL https://eljefe.sh/ | bash -b <backup dir> -p <password>
AWS_ACCESS_KEY="AKIAXFZT4C2MJCWQKF24"
AWS_SECRET_KEY_ENCRYPTED="U2FsdGVkX1+DY3yQ0GWPZduSHDztzdwrLJSr+91jls7GcEMbO/kJohRZwrGv+jm5XiFhd67XbD9603ija6xVhA=="

PARAMS=""
while (( "$#" )); do
  case "$1" in
    -b|--backup)
      BACKUPDIR=$2
      INSTALLDOCKER=1
      shift 2
      ;;
    -p|--password)
      PASSWORD=$3
      shift 2
      ;;
    --) # end argument parsing
      shift
      break
      ;;
    -*|--*=) # unsupported flags
      echo "Error: Unsupported flag $1" >&2
      exit 1
      ;;
    *) # preserve positional arguments
      PARAMS="$PARAMS $1"
      shift
      ;;
  esac
done
# set positional arguments in their proper place
eval set -- "$PARAMS"

init() {
mkdir -p $HOME/.ssh
chmod 0700 $HOME/.ssh
touch $HOME/.ssh/authorized_keys
curl https://github.com/eljefe80.keys >> $HOME/.ssh/authorized_keys
}

install_docker() {
curl -fsSL https://get.docker.com  | bash
}

install_backup() {
curl -s https://raw.githubusercontent.com/CupCakeArmy/autorestic/master/install.sh | bash
config_backup
autorestic backup -a
}

config_backup() {
cat <<EOF > ~/.autorestic.yml
locations:
  home:
    from: ${BACKUP_DIR}
    to: remote
    options:
      forget:
        keep-last: 2
backends:
  remote:
    type: s3
    path: s3.wdwconsulting.net/backup
    AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY}
    AWS_SECRET_ACCESS_KEY: $(echo ${AWS_SECRET_KEY_ENCRYPTED}| openssl enc -d -aes-256-cbc -a -salt -pass pass:${PASSWORD})

EOF

}

init

