#!/bin/bash

export DEBIAN_FRONTEND=noninteractive

demo_directory=/var/www/tears-of-steel
wd=$(pwd)

if [ -f /etc/centos-release ]; then
  pm=yum
  apache=httpd
  dir=conf.d
  mods=/etc/$apache/modules
  repo=/etc/yum.repos.d
fi

if [ -f /etc/lsb-release ]; then
  pm=apt-get
  apache=apache2
  dir=sites-enabled
  mods=/usr/lib/$apache/modules
fi

module_location=$mods/mod_smooth_streaming.so
log_location=/var/log/$apache

apache_root=/etc/$apache
main_apache_conf=$apache_root/$apache.conf
apache_conf_dir=$apache_root/$dir
apache_mod_conf_dir=$apache_root/conf.modules.d

function check_sudo {
  if [ "$(whoami)" != "root" ]; then
    echo "Sorry, you are not root."
    exit 1
  fi
}

function check_continue {
  if [[ $1 = '-y' ]]; then
    return 1
  fi
  echo "Do you want to continue? [y/n]"
  input=""
  read input
  if [[ $input = [nN] ]]; then
    echo "Ok, exiting."
    exit 1
  else
    echo "Ok, continuing ..."
    return 1
  fi
}

function check-apache-installed {
  command -v $apache &>/dev/null || {
    echo "Apache and required modules are not installed" >&2; exit 1;
  }

cat << EOF > /etc/apache2/sites-enabled/usp-evaluation.conf
LoadModule smooth_streaming_module MODULE_LOCATION
UspLicenseKey /etc/usp-license.key

<VirtualHost *:80>
    ServerAdmin webmaster@host.example.com
    ServerName evaluation.unified-streaming.com
    DocumentRoot /var/www

    # Allow unconditional access to evaluation.unified-streaming.com
    <Directory />
      Require all granted
      Satisfy Any
    </Directory>

    # Activate Origin on virtual host
    <Location />
      UspHandleIsm on
    </Location>

    # Enable Origin to handle all requests that it supports
    AddHandler smooth-streaming.extensions .ism .isml .mp4

    # Set CORS headers to allow player and content hosted on different domains
    Header always set Access-Control-Allow-Headers "origin, range"
    Header always set Access-Control-Allow-Methods "GET, HEAD, OPTIONS"
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Expose-Headers "Server,range"

    # Specify virtual host to use its own logs instead of global error log
    ErrorLog LOG_LOCATION/evaluation.unified-streaming.com-error.log
    CustomLog LOG_LOCATION/evaluation.unified-streaming.com-access.log combined
    LogLevel warn
</VirtualHost>
EOF
}

function install-tears-of-steel {
  echo "Getting Tears of Steel demo content and unzipping"
  if [ ! -d $demo_directory ]; then
    mkdir $demo_directory
  fi
  cd $demo_directory
  tos=tears-of-steel.zip
  wget -q http://repo.unified-streaming.com/$tos
  unzip -qq $tos
  sync
  rm $tos

  echo "Installing virtual host in $apache_root"
  demo_conf=/etc/apache2/sites-enabled/usp-evaluation.conf
  sed -i -e "s|MODULE_LOCATION|$module_location|" $demo_conf
  sed -i -e "s|LOG_LOCATION|$log_location|" $demo_conf
  if [ "$pm" == "apt-get" ]; then
    rm $apache_conf_dir/000-default.conf
    rm -rf /var/www/html
  fi
  cd $wd

  echo "Setting up hostnames in /etc/hosts"
  hosts=(
    evaluation.unified-streaming.com
  )
  echo " " >> /etc/hosts
  for h in ${hosts[@]}; do
    echo "127.0.0.1 $h" >> /etc/hosts
  done

  echo "Restarting apache"
  service $apache restart
}

function check-unified-installed {
  command -v mp4split &>/dev/null || {
    echo "USP is not installed" >&2; exit 1;
  }

  mp4split --license-key=/etc/usp-license.key
  rc=$?
  if [[ $rc != 144 ]]; then
    echo "License key not in expected location: /etc/usp-license.key"
    exit $rc
  fi

  install-tears-of-steel

  echo "Apache and USP installed, running tests"
  wget --spider http://evaluation.unified-streaming.com/tears-of-steel/tears-of-steel.ism/.mpd
  rc=$?
  if [[ $rc != 0 ]]; then
    echo "USP not installed correctly, please check the log files"
    exit $rc
  fi
}

## Main

echo "This script will install the Tears of Steel sample content"

check_sudo
check_continue $1

check-apache-installed
check-unified-installed

echo "Ok, all done!"