#!/bin/sh

cookie_path=cookies.txt

print_help() {
  echo "tesoft.dev helper script"
  echo ""
  echo "usage: $0 <action>"
  echo ""
  echo "login into the webpage with a password:"
  echo "> $0 login"
  echo ""
  echo "logout from the webpage:"
  echo "> $0 logout"
  echo ""
  echo "create a new blog article:"
  echo "> $0 upload ./article-dir"
  echo ""
  echo "update an existing blog article:"
  echo "> $0 patch 123 ./article-dir"
  echo ""
  echo "publish an existing blog article:"
  echo "> $0 publish 123"
  echo ""
  echo "unpublish an existing blog article:"
  echo "> $0 unpublish 123"
  echo ""
  echo "delete an existing blog article:"
  echo "> $0 drop 123"
  echo ""
}

login() {
  echo -n "password: "
  read -s password
  echo ""
  out=$(curl -v -f -H 'Content-Type: application/json' -d "{\"password\":\"$password\"}" "$URL/api/login" -c "$cookie_path" 2>&1)
  if [ $? -ne 0 ]; then
    echo "$out" >&2
    exit 1
  fi

  echo "ok"
}

logout() {
  out=$(curl -v -f -X POST "$URL/api/logout" -b "$cookie_path" 2>&1)
  if [ $? -ne 0 ]; then
    echo "$out" >&2
    exit 1
  fi

  rm -f "$cookie_path"
  echo "ok"
}

upload() {
  if [ -z "$1" ]; then
    echo "error: article directory is not passed" >&2
    exit 1
  fi
  if ! [ -d "$1" ]; then
    echo "error: article directory not found" >&2
    exit 1
  fi

  tar -C "$1" --exclude=$cookie_path -cf /tmp/article.tar $(ls "$1")
  out=$(curl -v -f -X PUT -F "file=@/tmp/article.tar" "$URL/api/blog" -b "$cookie_path" 2>&1)
  if [ $? -ne 0 ]; then
    echo "$out" >&2
    exit 1
  fi

  echo "ok, id = $(echo "$out" | tail -1 | grep -oP '[0-9]+')"
}

patch() {
  if [ -z "$1" ]; then
    echo "error: article id is not passed" >&2
    exit 1
  fi
  if [ -z "$2" ]; then
    echo "error: article directory is not passed" >&2
    exit 1
  fi
  if ! [ -d "$2" ]; then
    echo "error: article directory not found" >&2
    exit 1
  fi

  tar -C "$2" --exclude=$cookie_path -cf /tmp/article.tar $(ls "$2")
  out=$(curl -v -f -X PATCH -F "file=@/tmp/article.tar" "$URL/api/blog/$1" -b "$cookie_path" 2>&1)
  if [ $? -ne 0 ]; then
    echo "$out" >&2
    exit 1
  fi

  echo "ok"
}

publish() {
  if [ -z "$1" ]; then
    echo "error: article id is not passed" >&2
    exit 1
  fi

  out=$(curl -v -f -X POST "$URL/api/blog/$1/publish" -b "$cookie_path" 2>&1)
  if [ $? -ne 0 ]; then
    echo "$out" >&2
    exit 1
  fi

  echo "ok"
}

unpublish() {
  if [ -z "$1" ]; then
    echo "error: article id is not passed" >&2
    exit 1
  fi

  out=$(curl -v -f -X POST "$URL/api/blog/$1/unpublish" -b "$cookie_path" 2>&1)
  if [ $? -ne 0 ]; then
    echo "$out" >&2
    exit 1
  fi

  echo "ok"
}

drop() {
  if [ -z "$1" ]; then
    echo "error: article id is not passed" >&2
    exit 1
  fi

  out=$(curl -v -f -X DELETE "$URL/api/blog/$1" -b "$cookie_path" 2>&1)
  if [ $? -ne 0 ]; then
    echo "$out" >&2
    exit 1
  fi

  echo "ok"
}

if [ "$#" -eq 0 ] || [ "$1" = "--help" ]; then
  print_help
  exit 0
fi

if [ -z "$URL" ]; then
  URL="https://tesoft.dev"
fi

case "$1" in
  login)
    login
    ;;
  logout)
    logout
    ;;
  upload)
    upload "$2"
    ;;
  patch)
    patch "$2" "$3"
    ;;
  publish)
    publish "$2"
    ;;
  unpublish)
    unpublish "$2"
    ;;
  drop)
    drop "$2"
    ;;
  *)
    echo "error: unknown action" >&2
    exit 1
    ;;
esac

