#!/bin/sh
#
# Simple script for batch-uploading emojis to a GoToSocial instance
#
# Based on this comment:
# https://codeberg.org/superseriousbusiness/gotosocial/issues/2299#issuecomment-4015514
#
# You will need:
#
# - Your instance base URL, e.g. https://gts.example.com
# - A Bearer token, can be extracted via your browser's developer tools from the HTTP Authorization header
# - A category for your emoji
#
# Then run:
#
# ./gts-emoji-import.sh -u BASEURL -t TOKEN -c CATEGORY *.png
#
# The filename (minus the .png extension) will be used as the emoji shortcode.

while getopts "u:t:c:" o; do
    case "$o" in
        u)
            baseurl="$OPTARG"
            ;;
        t)
            token="$OPTARG"
            ;;
        c)
            category="$OPTARG"
            ;;
    esac
done

if test -z "$baseurl" || test -z "$token" || test -z "$category"; then
    echo "Usage: $0 -u BASEURL -t TOKEN -c CATEGORY FILES..."
    exit 1
fi

shift $((OPTIND-1))

for fn in "$@"; do
    echo "Uploading: $fn"
    curl "$baseurl/api/v1/admin/custom_emojis" \
        -X POST \
        -H "Accept: application/json" \
        -H "Authorization: Bearer $token" \
        -H "Content-Type: multipart/form-data" \
        -F "shortcode=\"$(basename -- $fn .png)\"" \
        -F "image=@$fn" \
        -F "category=\"$category\""
    echo
done
