#!/usr/bin/env bash

function propose() {
    curl -X POST -H "Content-Type: text/plain" --data "${1}" http://localhost:9000/api/v1/feed/propose
}

function propose_file() {
    while 
        read feed 
    do 
        echo "proposing $feed"
        propose "$feed"
    done < "feeds-${1}.txt" 
}

function search() {
    curl "http://localhost:9000/api/v1/search?q=${1}&p=1&s=20"
}

function podcast() {
    curl "http://localhost:9000/api/v1/podcast/${1}"
}

function podcast_episodes() {
    curl "http://localhost:9000/api/v1/podcast/${1}/episodes"
}

function podcast_feeds() {
    curl "http://localhost:9000/api/v1/podcast/${1}/feeds"
}

function podcast_all() {
    curl "http://localhost:9000/api/v1/podcast?p=${1}&s=${2}"
}

function episode() {
    curl "http://localhost:9000/api/v1/episode/${1}"
}

function episode_chapters() {
    curl "http://localhost:9000/api/v1/episode/${1}/chapters"
}

function feed() {
    curl "http://localhost:9000/api/v1/feed/${1}"
}

function all_podcasts() {
    curl "http://localhost:9000/api/v1/podcast?p=${1}&s=${2}"
}

COMMAND=$@

case $1 in
    "propose")
        propose $2
        ;;
    "propose-file")
        propose_file $2
        ;;
    "search")
        search $2
        ;;
    "podcast")
        podcast $2
        ;;
    "podcast-episodes")
        podcast_episodes $2
        ;;
    "podcast-feeds")
        podcast_feeds $2
        ;;
    "podcast-all")
        podcast_all $2 $3
        ;;
    "episode")
        episode $2
        ;;
    "episode-chapters")
        episode_chapters $2
        ;;
    "feed")
        feed $2
        ;;
    "all-podcasts")
        all_podcasts $2 $3
        ;;
    *)
        # unknown option
        echo "Unknown command: ${$1}"
        exit 1
        ;;
esac
