#!/bin/bash

# This script installs Opencast on a Debian or Ubuntu system.
# Tested with Ubuntu 16.04.

# If database host is localhost (the default), a mariadb database is also installed
# and populated with a fresh dataset.

# Example invocation:
#   sudo OC_SERVER_URL="http://opencast.catturavideo.com" ./install_oc
#   curl https://repositories.catturavideo.com/install_oc | sudo OC_SERVER_URL="http://opencast.catturavideo.com" bash

# Variables
APT_USERNAME=${APT_USERNAME:-"cattura"}
APT_PASSWORD=${APT_PASSWORD:-"A19RgPucdri38u0O"}

OC_DIST=${OC_DIST:-"allinone"}
OC_VERSION=${OC_VERSION:-"2.3.1-1"}
ACTIVEMQ_VERSION=${ACTIVEMQ_VERSION:-"5.13.0-1"}
FFMPEG_VERSION=${FFMPEG_VERSION:-"3.2.2-1"}

OC_DB_HOST=${OC_DB_HOST:-"localhost"}
OC_DB=${OC_DB:-"opencast"}
OC_DB_USER=${OC_DB_USER:-"opencast"}

if [ -z "$OC_DB_PASSWORD" ]; then
  echo ">> Generating random password"
  OC_DB_PASSWORD=$(head /dev/urandom | sha512sum | cut -d ' ' -f 1)
fi;

OC_INSTALL_DIR="/usr/share/opencast"
OC_CONFIG_DIR="/etc/opencast"
OC_CONFIG_FILE="$OC_CONFIG_DIR/custom.properties"

OC_SERVER_URL=${OC_SERVER_URL:-"http://localhost/"}
OC_ADMIN_EMAIL=${OC_ADMIN_EMAIL:-"admin@localhost"}
OC_SECURITY_DIGEST_USER=${OC_SECURITY_DIGEST_USER:-"opencast_system_account"}
OC_SECURITY_DIGEST_PASS=${OC_SECURITY_DIGEST_PASS:-"CHANGE_ME"}

main () {
  # Confirm installation
  echo "Opencast Install Script 1.0.0"
  echo ">> Opencast distribution (OC_DIST) $OC_DIST, version (OC_VERSION): $OC_VERSION"
  echo ">> Opencast server url (OC_SERVER_URL): $OC_SERVER_URL"
  echo ">> Opencast admin email (OC_ADMIN_EMAIL): $OC_ADMIN_EMAIL"
  echo ">>"
  echo ">> ffmpeg version (FFMPEG_VERSION): $FFMPEG_VERSION"
  echo ">> activemq version (ACTIVEMQ_VERSION): $ACTIVEMQ_VERSION"
  echo ">>"
  echo ">> Database host (OC_DB_HOST): $OC_DB_HOST"
  echo ">> Database user (OC_DB_USER): $OC_DB_USER"
  echo ">>"
  echo ">> Opencast system user (OC_SECURITY_DIGEST_USER): $OC_SECURITY_DIGEST_USER"
  echo ">>"
  read -p ">> Press Enter to begin installation: "

  # Install prereqs
  echo ">> Installing prerequisites"
  apt-get update
  apt-get install -y wget apt-transport-https lsb-release sudo

  # Install opencast repo
  DIST_CODENAME=$(lsb_release -sc)
  echo ">> Installing opencast repo for $DIST_CODENAME"
  wget -O - https://pkg.opencast.org/gpgkeys/opencast-deb.key | apt-key add -
  echo "deb https://$APT_USERNAME:$APT_PASSWORD@pkg.opencast.org/debian $DIST_CODENAME testing" | tee /etc/apt/sources.list.d/opencast-testing.list
  echo "deb https://$APT_USERNAME:$APT_PASSWORD@pkg.opencast.org/debian $DIST_CODENAME main" | tee /etc/apt/sources.list.d/opencast.list
  apt-get update

  # Install opencast packages
  echo ">> Installing opencast ($OC_DIST) $OC_VERSION; activemq $ACTIVEMQ_VERSION; ffmpeg $FFMPEG_VERSION"
  apt-get install -y "opencast-$OC_DIST=$OC_VERSION" "opencast-common=$OC_VERSION" \
                     "activemq=$ACTIVEMQ_VERSION" "opencast-activemq=$OC_VERSION" \
                     "ffmpeg=$FFMPEG_VERSION" tesseract-ocr hunspell sox

  # Initial configuration for opencast
  echo ">> Configuring server url ($OC_SERVER_URL) and admin email ($OC_ADMIN_EMAIL)"
  sed -i "s@org.opencastproject.server.url=http://localhost:8080@org.opencastproject.server.url=$OC_SERVER_URL@g" "$OC_CONFIG_FILE"
  sed -i "s/org.opencastproject.admin.email=admin@localhost/org.opencastproject.admin.email=$OC_ADMIN_EMAIL/g" "$OC_CONFIG_FILE"
  sed -i "s/org.opencastproject.security.digest.user=opencast_system_account/org.opencastproject.security.digest.user=$OC_SECURITY_DIGEST_USER/g" "$OC_CONFIG_FILE"
  sed -i "s/org.opencastproject.security.digest.pass=CHANGE_ME/org.opencastproject.security.digest.pass=$OC_SECURITY_DIGEST_PASS/g" "$OC_CONFIG_FILE"

  service activemq restart
  service opencast restart

  # Install and configure database
  if [ "$OC_DB_HOST" == "localhost" ]; then
    echo ">> Installing database (mariadb-server)"
    apt-get -y install mariadb-server
    service mysql start
    echo ">> Creating initial opencast database and user"
    mysql -u root -e "CREATE DATABASE $OC_DB CHARACTER SET utf8 COLLATE utf8_general_ci;"
    mysql -u root -e "GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,INDEX ON $OC_DB.* TO '$OC_DB_USER'@'localhost' IDENTIFIED BY '$OC_DB_PASSWORD';"
    service mysql restart
    echo ">> Loading database dump"
    mysql "$OC_DB" "-u$OC_DB_USER" "-p$OC_DB_PASSWORD" "-h$OC_DB_HOST" < "$OC_INSTALL_DIR/docs/scripts/ddl/mysql5.sql"
  fi;

  echo ">> Configuring opencast to use database"
  sed -i "s/org.opencastproject.db.ddl.generation=true/org.opencastproject.db.ddl.generation=false/g" "$OC_CONFIG_FILE"
  sed -i "s/#org.opencastproject.db.vendor=MySQL/org.opencastproject.db.vendor=MySQL/g" "$OC_CONFIG_FILE"
  sed -i "s/#org.opencastproject.db.jdbc.driver=com.mysql.jdbc.Driver/org.opencastproject.db.jdbc.driver=com.mysql.jdbc.Driver/g" "$OC_CONFIG_FILE"
  sed -i "s@#org.opencastproject.db.jdbc.url=jdbc:mysql://localhost/opencast@org.opencastproject.db.jdbc.url=jdbc:mysql://$OC_DB_HOST/$OC_DB@g" "$OC_CONFIG_FILE"
  sed -i "s/#org.opencastproject.db.jdbc.user=opencast/org.opencastproject.db.jdbc.user=$OC_DB_USER/g" "$OC_CONFIG_FILE"
  sed -i "s/#org.opencastproject.db.jdbc.pass=dbpassword/org.opencastproject.db.jdbc.pass=$OC_DB_PASSWORD/g" "$OC_CONFIG_FILE"

  service opencast restart

  # Install and configure nginx
  echo ">> Installing nginx"
  apt-get install -y nginx
  echo ">> Configuring nginx"
  echo -e "server {\r\n\tlisten 80;\r\n\r\n\tlocation / {\r\n\t\tproxy_pass http://127.0.0.1:8080/;\r\n\t\tclient_max_body_size 0;\r\n\t\tproxy_buffering off;\r\n\t}\r\n}" > /etc/nginx/sites-available/opencast.conf
  ln -s ../sites-available/opencast.conf /etc/nginx/sites-enabled/opencast.conf
  rm /etc/nginx/sites-enabled/default
  service nginx restart

  echo ">> Opencast ($OC_DIST) $OC_VERSION installed"
  echo ">> Configuration file is at $OC_CONFIG_FILE"
}

main

