#!/usr/bin/env bash

URL="https://pastebin.dessa.dev"

FILEPATH="$1"
FILENAME=$(basename -- "$FILEPATH")
EXTENSION="''${FILENAME##*.}"

if [ ! -f $FILEPATH ]; then
  # curl the file from the internet, given the id
  # future regex for any wantsgunbin link: ^((http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b\/)?[0-9a-zA-Z]{5,}(\..*)?$
  if [[ $FILEPATH =~ ^(https:\/\/pastebin\.dessa\.dev\/)?[0-9a-zA-Z]{5,}(\..*)?$ ]]; then
    OUT=$(mktemp)
    echo $OUT
    curl -sL https://pastebin.dessa.dev/$FILEPATH > $OUT
    exit 0
  else
    echo "File not found: $FILEPATH"
    exit 1
  fi
fi

RESPONSE=$(curl --data-binary @''${FILEPATH:-/dev/stdin} --url $URL)
PASTELINK="$URL$RESPONSE"

[ -z "$EXTENSION" ] && echo $PASTELINK || echo $PASTELINK.$EXTENSION