#!/bin/bash
# -*-coding: utf-8-unix-*-
#
# playlisttool -- Create playlist for each directory passed on the command-line
#
# Example 1: Create 2 playlists for all images and videos in directory images
# (and subdirectories).
#
#     $ playlisttool . images
#     ___.m3u:
#     ___images.m3u:
#
# Example 2: Only 1 directory deep.
#
#     $ find Bilder Filme -type d -maxdepth 1 -print0 | xargs -0 playlisttool
#
# See also Steve Carlson's solution at https://gist.github.com/scarlson/944860
#
# Copyright (C) 2007-2016.
#
# $Compile: ./playlisttool$
# $Maintained at: http://www.visualco.de$
# $Author: Andreas Spindler <info@andreasspindler.de>$
# $Writestamp: 2016-04-20 13:23:01$

optsep='___'
optdepth=1
{
    for dir in "${@:1}"
    do
        m3u="${dir//\./}"          # remove '.'
        m3u="${m3u//\//${optsep}}" # replace '/'
        m3u="${optsep}${m3u}.m3u"
        echo "$m3u:"
        rm -vf $m3u
        find "$dir" -type f                     \
             \( -iname "*.wmv" -o               \
                -iname "*.mp4" -o               \
                -iname "*.m4v" -o               \
                -iname "*.avi" -o               \
                -iname "*.mkv" -o               \
                -iname "*.gif" -o               \
                -iname "*.png" -o               \
                -iname "*.jpg" -o               \
                -iname "*.jpeg" \) | sort |
            while read f; do
                echo "$f" >> "$m3u"
            done
    done
}