#!/bin/bash
# scan-to-paperless — scan from Brother MFC-J5855DW via AirScan → Paperless consume folder
# Usage: scan-to-paperless [name] [--flatbed] [--gray] [--150dpi]
set -e

DEVICE="airscan:e0:Brother MFC-J5855DW"
CONSUME="/mnt/seagate8tb/paperless/consume"
MODE="Color"
RES="300"
SOURCE="ADF"

NAME=""
while [[ $# -gt 0 ]]; do
    case "$1" in
        --flatbed) SOURCE="Flatbed" ;;
        --gray)    MODE="Gray" ;;
        --150dpi)  RES="150" ;;
        *)         NAME="$1" ;;
    esac
    shift
done

SCAN_OPTS="--mode $MODE --resolution $RES"
[ "$SOURCE" = "ADF" ] && SCAN_OPTS="$SCAN_OPTS --source ADF --batch-count=1"

TIMESTAMP=$(date +%Y%m%d_%H%M%S)
FILENAME="${NAME:-scan}_${TIMESTAMP}.pdf"
OUTFILE="$CONSUME/$FILENAME"

echo "📄 Scanning ($SOURCE, $MODE, ${RES}dpi) → $FILENAME"
scanimage --device "$DEVICE" $SCAN_OPTS --format pdf -o "$OUTFILE"

SIZE=$(du -h "$OUTFILE" | cut -f1)
echo "✅ Done — $SIZE saved to Paperless"
echo "   File: $FILENAME"
