#!/bin/bash

set -e

BIN_NAME=$(basename "$0")

function stdout() {
  echo -e "[$BIN_NAME] $1"
}

function stderr() {
  echo -e "[$BIN_NAME] $1" 1>&2
}

function usage() {
  stderr "usage:"
  stderr "\t# $BIN_NAME create"
  stderr "\t# $BIN_NAME restore"
}

if [[ $EUID -ne 0 ]]; then
  stderr "fail: this script must be run as root"
  exit 1
fi

if [ -z "$1" ]; then
  stderr "fail: invalid args"
  usage "$0"
  exit 1
fi

function handle_create() {
  local dpkg_path=$1

  dpkg -l > "$dpkg_path"
}

case $1 in
  create)
    stdout "exec: create"
    handle_create ./dpkg.txt
    stdout "done: create"
    ;;

  restore)
    stdout "exec: restore"
    stdout "done: restore"
    ;;

  *)
    stderr "fail: unsupported (action:$1)"
    usage "$0"
    exit 1
    ;;
esac
