#!/bin/bash

# Build a portable AppImage starting from a clean system. Other examples at:
# https://github.com/probonopd/AppImages/blob/master/recipes/scribus/Recipe

# NOTES:
#
# 1) IT IS NOT SAFE TO RUN THIS RECIPE ON A PERSISTENT FILESYSTEM! Use either:
#      * A chroot into a Live ISO, squashfs or Docker Image.
#      * Or, a virtual machine on a dedicated build server (e.g. Travis CI)
#    DO NOT RUN THE RECIPE ON A NORMAL COMPUTER OUTSIDE OF A TEMPORARY CHROOT!
#    
# 2) Run the recipe inside a CentOS 6 or 7 chroot. You could do it like this:
#     $  git  clone  https://github.com/probonopd/AppImageKit.git
#     $  ./AppImageKit/build.sh
#     $  cd  AppImageKit/AppImageAssistant.AppDir/
#     $  sudo  ./testappimage  CentOS-6.7-x86_64-LiveCD.iso  bash
#       #  Now we're inside the chroot!
#       #  Must put the recipe in the chroot if it's not already there:
#     $  nano Recipe
#       #  Paste recipe into terminal window (Ctrl+Shift+V)
#       #  Save (Ctrl+O) and exit (Ctrl+X)
#     $  chmod +x Recipe
#     $  ./Recipe
#
# 3) Pass in as arguments any overrides for the "make portable" target. E.g.:
#     $  ./Recipe PREFIX="MuseScoreDev" SUFFIX="-dev" LABEL="Development Build"

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

##########################################################################
# CHECK SYSTEM
##########################################################################

# This script should be run inside CentOS 6 if possible,
# or CentOS 7 if 6 is unavailable for your architecture.
if [ "$(grep "CentOS release 6" /etc/*release*)" ]; then
  OS="CentOS 6"
elif [ "$(grep "CentOS Linux release 7" /etc/*release*)" ]; then
  OS="CentOS 7"
  echo "${0}: Running on CentOS 7 (CentOS 6 might be a better choice)" >&2
else
  echo "${0}: Error: Not running on CentOS 6 or 7!" >&2
  exit 1
fi

##########################################################################
# 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

if [ "${OS}" == "CentOS 6" ]; then
  # Get newer compiler than available by default
  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
  source /opt/rh/devtoolset-2/enable # enable new compiler
else
  yum -y install gcc gcc-c++ binutils
fi

# Build AppImageKit now to avoid conflicts with MuseScore's dependencies (LAME)
[ -d "AppImageKit" ] || git clone --depth 1 https://github.com/probonopd/AppImageKit.git
cd AppImageKit
./build.sh

cd ..

# MuseScore's dependencies:
#yum -y install epel-release
#wget http://people.centos.org/tru/devtools-2/devtools-2.repo -O /etc/yum.repos.d/devtools-2.repo
yum -y install cmake mesa-libGL-devel pulseaudio-libs-devel alsa-lib-devel jack-audio-connection-kit-devel portaudio-devel libsndfile-devel libvorbis-devel qt5-qtbase-devel qt5-qttools-libs-designercomponents qt5-qttools-devel qt5-qtdeclarative-devel qt5-qtscript-devel qt5-qtwebkit-devel qt5-qtxmlpatterns-devel qt5-qtquick1-devel qt5-qtsvg-devel qt5-qttools-devel qt5-qttools-static qt5-qtmultimedia-devel qt5-qtwebchannel-devel qt5-qtimageformats qt5-qtquickcontrols

# Install LAME (get this dependency last because rpmforge and epel-release conflict)
wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.$(arch).rpm
rpm -ivh rpmforge-release-0.5.3-1.el6.rf.$(arch).rpm || true # don't fail if already installed
yum -y install lame-devel

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

# If not building on Travis then might need to fetch MuseScore
[ -d "MuseScore" ] || git clone --depth 1 https://github.com/musescore/MuseScore.git

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"
