#!/usr/bin/env bash

DOMAIN="fuckup.solutions"
SHORTENER="/var/www/html/s/index.html"

# do not edit below this line

TURL="$1" # first word (a url)
STITLE="$2" #second word (a short code)
TTITLE="${*:3}" #rest of the input (user title)

if [ -z "$TTITLE" ]; then TTITLE="$STITLE"; fi

URLCHECK=$( grep -wc "$TURL" $SHORTENER )
STITLECHECK=$( grep -wc "$STITLE" $SHORTENER )
if [ $URLCHECK -ne 0 ]; then
	echo "URL: \"$TURL\" already added, try again"
	exit 0;
fi
if [ $STITLECHECK -ne 0 ]; then
	echo "Short Title: \"$STITLE\" already in use, try again"
	exit 0;
fi


sed -i "/<ol>/a <li>$DOMAIN/s/#$STITLE = <a id='$STITLE' rel='bookmark' href='$TURL'>$TTITLE</a></li>" $SHORTENER
printf "short URL added:\n%s/s/#%s\n" "$DOMAIN" "$STITLE"
