#!/bin/bash

# This script should be run in a CentOS 6 Docker image

set -e # Halt on errors
set -x # Be verbose

##########################################################################
# GET DEPENDENCIES
##########################################################################

# go one-up from MuseScore root dir regardless of where script was run from:
cd "$(dirname "$(readlink -f "${0}")")/../../../../.."

yum -y install epel-release

# basic dependencies (needed by Docker image)
yum -y install git wget which automake unzip

# get newer compiler than available by default
if [ ! -f "/etc/yum.repos.d/devtools-2.repo" ]; then
  wget http://people.centos.org/tru/devtools-2/devtools-2.repo -O /etc/yum.repos.d/devtools-2.repo
  yum -y install devtoolset-2-gcc devtoolset-2-gcc-c++ devtoolset-2-binutils
fi
source /opt/rh/devtoolset-2/enable # enable new compiler

# build AppImageKit now to avoid conflicts with MuseScore's dependencies (LAME)
if [ ! -d "AppImageKit" ]; then
  git clone https://github.com/probonopd/AppImageKit.git
  cd AppImageKit
  git reset --hard 076a88a57d7ca1a8d77bdbf7d8e92f37eea03dab
  rm -rf .git*
  ./build.sh
  cd ..
fi

# MuseScore's dependencies:
yum -y install \
  alsa-lib-devel \
  fontconfig-devel \
  freetype-devel \
  jack-audio-connection-kit-devel \
  libsndfile-devel \
  libvorbis-devel \
  libXcomposite-devel \
  libXcursor-devel \
  libXrender-devel \
  libxslt-devel \
  mesa-libGL-devel \
  portaudio-devel \
  portmidi-devel \
  pulseaudio-libs-devel

# get Qt
if [ ! -d "qt5" ]; then
  mkdir qt5
  wget -q -O qt5.zip https://s3.amazonaws.com/utils.musescore.org/qt542-x86.zip
  unzip -qq qt5.zip -d qt5
  rm -f qt5.zip
fi
export PATH="${PWD}/qt5/bin:$PATH"
export QT_PLUGIN_PATH="${PWD}/qt5/plugins"
export QML2_IMPORT_PATH="${PWD}/qt5/qml"
export LD_LIBRARY_PATH="${PWD}/qt5/lib:$LD_LIBRARY_PATH"

# install LAME (get this dependency last because rpmforge and epel-release conflict)
if [ "$(arch)" = "i686" ]; then
  nux_url="http://li.nux.ro/download/nux/dextop/el6/i386/nux-dextop-release-0-2.el6.nux.noarch.rpm"
else
  nux_url="http://li.nux.ro/download/nux/dextop/el6/$(arch)/nux-dextop-release-0-2.el6.nux.noarch.rpm"
fi
rpm -Uvh "${nux_url}" || true # don't fail if already installed
yum -y install lame-devel

# install cmake
if [ ! -d cmake ]; then
  if [ "$(arch)" = "i686" ]; then
    CMAKE_URL="http://www.cmake.org/files/v3.5/cmake-3.5.1-Linux-i386.tar.gz"
  else
    CMAKE_URL="http://www.cmake.org/files/v3.5/cmake-3.5.1-Linux-$(arch).tar.gz"
  fi
  mkdir cmake
  wget --no-check-certificate --quiet -O - "${CMAKE_URL}" | tar --strip-components=1 -xz -C cmake
fi
export PATH="${PWD}/cmake/bin:${PATH}"

# Build MuseScore on Travis but not on Docker Hub (reduce container size)
[ -d "MuseScore" ] || exit 0

##########################################################################
# BUILD MUSESCORE
##########################################################################

cd MuseScore
make revision
make "$@" portable

appdir="$(cat build.release/PREFIX.txt)"
appimage="$(echo "$appdir" | sed 's|\.AppDir$|.AppImage|')"

##########################################################################
# PACKAGE INTO APPIMAGE WITH APPIMAGEKIT
##########################################################################

cd ../AppImageKit/AppImageAssistant.AppDir
chmod u+x /$appdir/bin/QtWebEngineProcess 

./package  "$appdir" "$appimage"

# allow access to AppImage from outside the chroot
chmod a+rwx "$appimage"
parent_dir="$(dirname "$appimage")"
while [ "$(dirname "$parent_dir")" != "$parent_dir" ]; do
  [ "$parent_dir" == "/" ] && break
  chmod a+rwx "$parent_dir"
  parent_dir="$(dirname "$parent_dir")"
done

ls -lh "$appimage"
