#!/bin/bash

# We have left the hash exclamation mark in so that the script can be 
# executed from the command line if required.
# The script file is shown as executable by naming the file extension '.command'
# To make the file executable the file flag for execution must be set.
# This can be done on a terminal command line using the command
# 'chmod +x filename.command' to set the executable flag in the file properties.
# 
# This script builds the current basic development version of stellarium

echo "Add path to Qt5.11.2 to 'PATH'"

export PATH=~/Qt5.11.2/5.11.2/clang_64/bin:$PATH

echo "List full current 'PATH'"

echo $PATH

echo "Compile current Stellarium development source in the '~User/Developer' directory"

cd ~/Developer

echo "Delete previous 'stellarium' build directory"

rm -r stellarium

# above and below amended to not back up the previous stellarium dir
# echo "Rename last stellarium directory as stellariumprv"

# mv stellarium stellariumprv

echo "Copy latest github stellarium source code from 'Downloads/stellarium-master' directory"

# recursive copy (including hidden files) downloaded source code retaining original download
cp -r ~/Downloads/stellarium-master/ ~/Developer/stellarium

# mv ~/Downloads/stellarium-master/ ~/Developer/stellarium

echo "Got source, now create new build directories under 'stellarium' directory"

cd stellarium
mkdir -p builds/macosx
cd builds/macosx

echo "Run cmake .."

cmake ../..

echo "Run make, install and bundle for OS X"

make
make install

make mac_app

echo "Stellarium app should now be built"

# ~ is home directory starting point

echo "Now create Stellarium dmg and move to Uploads directory for sf.net."

cd ~/Uploads

echo "Delete previous temporary stellarium directory"

rm -r stellarium

echo "Create new stellarium directory"

mkdir stellarium

echo "Move stellarium app from build to upload directory"

mv ~/Developer/stellarium/builds/macosx/Stellarium.app ~/Uploads/stellarium

echo "Create stellarium disc image with better compression"
echo "Amend dmg name to match version no."

hdiutil create -format UDBZ -srcfolder stellarium StellariumQT511_2-0.18.3.dmg

echo "Move disc image to StellariumSFnetFiles upload directory"

mv StellariumQT511_2-0.18.3.dmg StellariumSFnetFiles