#!/bin/bash

# This script should be run in an Ubuntu 12.04 "Precise Pangolin" 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}")")/../../../../.."

apt-get update # no package lists in Docker image

# basic dependencies (needed by Docker but provided on Travis)
apt-get -y install \
  git \
  unzip \
  libxslt1-dev \
  libxrender-dev \
  libxcomposite-dev \
  libdrm-dev \
  libgl1-mesa-dev \
  libopenal1 \
  gstreamer0.10-plugins-base \
  python-software-properties # installs `add-apt-repository`

# get newer compiler
if [ ! -f /etc/apt/sources.list.d/ubuntu-toolchain-r-test-precise.list ]; then
  add-apt-repository -y ppa:ubuntu-toolchain-r/test
  apt-get update
fi

# MuseScore dependencies (same as on Travis)
apt-get -y install \
  g++-4.8 \
  wget \
  make \
  curl \
  alsa \
  libasound2-dev \
  portaudio19-dev \
  libportmidi-dev \
  libsndfile1-dev \
  zlib1g-dev \
  libfreetype6-dev \
  libfontconfig1-dev \
  lame \
  libmp3lame-dev \
  libegl1-mesa-dev \
  libpulse-dev

update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8

# get Qt
if [ ! -d "qt5" ]; then
  mkdir qt5
  if [ "$(arch)" = "i686" ]; then
    QT_URL="http://utils.musescore.org.s3.amazonaws.com/qt542-x86.zip"
  else
    QT_URL="http://utils.musescore.org.s3.amazonaws.com/qt542.zip"
  fi
  wget -q -O qt5.zip "${QT_URL}"
  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 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 AppImageKit
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

# 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

./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"
