#!/bin/bash
# Tag a file by adding its name to the files tagname.playlist.  See tq.
set -e

f=$1
shift

# As a hokey special case, list the tags currently applied to a file
if [[ $# -eq 0 ]]; then
    for pl in *.playlist; do
        while read line; do
            if [[ "x$line" = "x$f" ]]; then
                echo -n "${pl%.playlist} "
                break
            fi
        done < "$pl"
    done
    echo
    exit 0
fi

if [[ ! -f "$f" ]]; then
    echo "no $f" >&2
    exit 1
fi

while [[ $# -gt 0 ]]; do
    pl=${1%.playlist}.playlist
    shift

    if [[ ! -f "$pl" ]]; then
        echo "warning: creating $pl" >&2
    fi

    echo "$f" >> "$pl"
done
