#!/bin/bash
# MyPlayer - Copyright 2014 - 2024 tilt@linuxfoo.de GPLv3
# Build Script; recompiles the MyPlayer JavaScript from source.

dir=$(readlink -f "$(dirname "$0")")

jsdir=$(readlink -f "$dir/../dist/js")

if ! cd "$dir" ; then
    echo "ERROR: Could not change working directory; aborted." >&2 ;
    exit 1 ;
elif ! mkdir -p "$jsdir" ; then
    echo "ERROR: Could not create directory $jsdir; aborted." >&2 ;
    exit 1 ;
elif ! type minify > /dev/null ; then
    echo "ERROR: Executable \"minify\" not found in PATH; aborted." >&2 ;
    exit 1 ;
fi

echo '/*
jQuery
   Copyright 2014 jQuery Foundation and other contributors
   http://jquery.com/
MediaElement.js
   Copyright 2010-2013, John Dyer (http://j.hn)
MediaElement Playlist Feature (plugin) -
   Andrew Berezovsky <andrew.berezovsky@gmail.com>
   and Junaid Qadir Baloch <shekhanzai.baloch@gmail.com>
JavaScript-ID3-Reader
   Copyright (c) 2008 Jacob Seidelin, http://blog.nihilogic.dk/ BSD License
   Copyright (c) 2009 Opera Software ASA BSD License
   Copyright (c) 2010 Antonio Afonso BSD License
   Copyright (c) 2010 Joshua Kifer BSD License
MyPlayer
   Copyright (c) 2014 - 2024 tilt@linuxfoo.de MIT License
*/' > "$jsdir"/myplayer.min.js

rm -f "$jsdir"/myplayer.min.js

for js in \
    ../mediaelement/build/mediaelement-and-player.min.js \
    ../mediaelement-plugins/dist/playlist/playlist.min.js \
    ../jsmediatags/dist/jsmediatags.js
do
    echo "INFO: Appending $js ..." >&2
    cat $js >> "$jsdir"/myplayer.min.js
done

echo "INFO: Compressing player.js ..." >&2
minify player.js >> "$jsdir"/myplayer.min.js

chmod 644 "$jsdir"/myplayer.min.js

echo "INFO: Javascript source was minimized to $jsdir/myplayer.min.js" >&2

rm -f "$jsdir"/myplayer.js

for js in \
    ../mediaelement/build/mediaelement-and-player.js \
    ../mediaelement-plugins/dist/playlist/playlist.js \
    ../jsmediatags/dist/jsmediatags.min.js \
    player.js
do
    echo "INFO: Appending $js ..." >&2
    cat $js >> "$jsdir"/myplayer.js
done

chmod 644 "$jsdir"/myplayer.js

echo "INFO: Uncompressed javascript source was collected in $jsdir/myplayer.js" >&2
