#!/bin/bash
# MyPlayer - Copyright 2014 - 2024 tilt@linuxfoo.de GPLv3
# Build Script; generates a minified collective CSS file

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

cssdir="$(readlink -f "$dir/../dist/css")"

if ! cd "$dir" ; then
    echo "ERROR: Could not change working directory; aborted." >&2 ;
    exit 1 ;
elif ! mkdir -p "$cssdir" ; then
    echo "ERROR: Could not create directory $cssdir; aborted." >&2 ;
    exit 1 ;
elif [[ -z $(type -p r.js) ]] ; then
    echo "ERROR: r.js optimizer not found; install it using 'npm install -g requirejs'." >&2
    exit 1
elif ! r.js -o cssIn=myplayer.css out="$cssdir"/myplayer.min.css optimizeCss=standard.keepComments.keepLines ; then
    echo "ERROR: r.js failed; aborted." >&2
    exit 1
else
    sed -i -e 's|../mediaelement-plugins/dist/playlist/||' "$cssdir"/myplayer.min.css
    chmod 0644 "$cssdir"/myplayer.min.css

    echo "INFO: Minimized CSS to $cssdir/myplayer.min.css" >&2

    svg=../mediaelement-plugins/dist/playlist/playlist-controls.svg 
    svg_out="$cssdir/$(basename "$svg")"

    if ! cp "$svg" "$svg_out" ; then
        echo "ERROR: Copying controls SVG $svg failed; aborted." >&2
        exit 1
    else
        echo "INFO: Copied controls SVG to $svg_out" >&2
    fi
fi
