#!/bin/bash

# Updates koreader

KINDLE_MOUNT_PATH=/run/media/nemo/Kindle
GITHUB_RELEASE_URL=https://api.github.com/repos/koreader/koreader/releases/latest
STORAGE_DIRECTORY=/home/nemo/Setups/Kindle
JSON_PATH="/tmp/koreader.json"

curl --silent $GITHUB_RELEASE_URL > "$JSON_PATH"

DOWNLOAD_URL=$(cat "$JSON_PATH" | jq --raw-output '.assets[] |select(.name | contains("kindle-v"))|.browser_download_url')
FILE_NAME=$(cat "$JSON_PATH" | jq --raw-output '.assets[] |select(.name | contains("kindle-v"))|.name')

echo "Latest version is $FILE_NAME"

wget --no-clobber --directory-prefix="$STORAGE_DIRECTORY" "$DOWNLOAD_URL" --output-file "$FILE_NAME"

if [[ -d "$KINDLE_MOUNT_PATH" ]]; then
  if [[ $@ == *'--please'* ]]; then
    unzip -uqqo "$STORAGE_DIRECTORY/$FILE_NAME" -d "$KINDLE_MOUNT_PATH"
    echo "Kindle KOReader has been upgraded to $FILE_NAME"
  else
    echo "Kindle is mounted, pass --please to actually upgrade"
  fi
fi

rm -f "$JSON_PATH"
