#!/bin/sh
set -eu
printf 'Alpnexa Drive Client for Linux\n'
printf 'The token will be stored in your local rclone config or desktop WebDAV mount. Revoke the token if this machine is shared, lost, or retired.\n'
printf 'Folder token: '
IFS= read -r TOKEN
if [ -z "$TOKEN" ]; then
  echo 'Missing token.' >&2
  exit 1
fi
URL="https://downloads.alpnexa.com/dav/$TOKEN/"
MOUNT="$HOME/AlpnexaDrive"
mkdir -p "$MOUNT"
if command -v rclone >/dev/null 2>&1; then
  rclone config create alpnexa_filedrive webdav url "$URL" vendor other >/dev/null
  rclone mount alpnexa_filedrive: "$MOUNT" --vfs-cache-mode writes --daemon
  if [ -d "$HOME/Desktop" ]; then
    ln -sfn "$MOUNT" "$HOME/Desktop/AlpnexaDrive"
  fi
  echo "Mounted at $MOUNT"
  echo 'Desktop shortcut created when ~/Desktop exists.'
elif command -v gio >/dev/null 2>&1; then
  gio mount "$URL"
  echo 'Mounted through the desktop file manager. Native WebDAV is a basic fallback; rclone plus FUSE is recommended for regular upload/download work.'
else
  echo 'Install rclone and FUSE first, then rerun this script.' >&2
  echo 'Debian/Ubuntu example: sudo apt install rclone fuse3' >&2
  exit 1
fi
