#!/bin/bash

# Process SMIL playlist with Unified Remix
docker run --rm \
  -v ${PWD}:/var/www/unified-origin \
  -w /var/www/unified-origin \
  -e UspLicenseKey \
  unifiedstreaming/unified_remix:1.11.9 \
  -o remixed.mp4 vod2live_hands_on.smil

if [ $? != 0 ];
then
    echo "Processing of SMIL did not succeed, exiting script."
    exit 1
fi

# Create VOD2Live enabled Live server manifest with mp4split
docker run --rm --entrypoint mp4split \
  -v ${PWD}:/var/www/unified-origin \
  -w /var/www/unified-origin \
  -e UspLicenseKey \
  unifiedstreaming/mp4split:1.11.9 \
  -o vod2live.isml \
  --vod2live \
  --dvr_window_length=600 \
  --hls.client_manifest_version=4 \
  --hls.no_multiplex \
  remixed.mp4

if [ $? != 0 ];
then
    echo "Creation of Live server manifest did not succeed, exiting script."
    exit 2
fi

# Spin up a container running Unified Origin to serve VOD2Live stream
docker run --rm -d -e UspLicenseKey \
  -v ${PWD}:/var/www/unified-origin -p ${DOCKER_HOST_PORT:-80}:80 \
  unifiedstreaming/origin:1.11.9

if [ $? == 0 ];
then
    echo "Locally available URL of DASH stream: localhost:${DOCKER_HOST_PORT:-80}/vod2live.isml/.mpd"
    echo "Locally available URL of HLS stream: localhost:${DOCKER_HOST_PORT:-80}/vod2live.isml/.m3u8"
    exit 0
else
    echo "Something went wrong spinning up container running Unified Origin, exiting script."
    exit 3
fi