# Build it on Mac howto in script form
# but be aware, some frameworks need patching to have this working

# errors fatal
set -e

# steps to build kate on make

# install Qt 5.5.0
# install CMake 3.3.2

# set path to Qt 5.5.0 stuff
export QTDIR=~/Qt5.5.0/5.5/clang_64
export PATH=$QTDIR/bin:/Applications/CMake.app/Contents/bin:$PATH

# remember some dirs
export PREFIX=`pwd`/usr
export BUILD=`pwd`/build
export SRC=`pwd`/src

# install gettext
mkdir -p src
cd src
curl http://ftp.gnu.org/pub/gnu/gettext/gettext-0.19.6.tar.gz > gettext-0.19.6.tar.gz
tar -xvzf gettext-0.19.6.tar.gz
pushd gettext-0.19.6
./configure --prefix=$PREFIX
make install
popd
cd ..

# build helper
function build_framework
{ (
    # errors fatal
    set -e

    # framework
    FRAMEWORK=$1

    # clone if not there
    mkdir -p $SRC
    cd $SRC
    if ( test -d $FRAMEWORK )
    then
        echo "$FRAMEWORK already cloned"
    else
        git clone kde:$FRAMEWORK
    fi

    # create build dir
    mkdir -p $BUILD/$FRAMEWORK

    # go there
    cd $BUILD/$FRAMEWORK

    # cmake it
    cmake $SRC/$FRAMEWORK -DCMAKE_INSTALL_PREFIX:PATH=$PREFIX

    # make
    make -j2

    # install
    make install
) }

# get & build stuff

build_framework extra-cmake-modules
build_framework kconfig
build_framework kguiaddons
build_framework ki18n
build_framework kitemviews
build_framework sonnet
build_framework kwindowsystem
build_framework kwidgetsaddons
build_framework kcompletion
build_framework kdbusaddons
build_framework karchive
build_framework kcoreaddons
build_framework kjobwidgets
build_framework kcrash
build_framework kservice
build_framework kcodecs
build_framework kauth
build_framework kconfigwidgets
build_framework kiconthemes
build_framework ktextwidgets
build_framework kglobalaccel
build_framework kxmlgui
build_framework kbookmarks
build_framework solid
build_framework kio
build_framework kparts
build_framework kitemmodels
build_framework threadweaver
build_framework attica
build_framework knewstuff
build_framework ktexteditor

# create icon resource file
cd src
rm -rf iconres
mkdir iconres
cd iconres
git archive --format tar --remote kde:breeze HEAD icons > breeze.tgz
mkdir icons
tar -C icons -s /^icons/breeze/ -xvzf breeze.tgz
rm breeze.tgz
rcc -project -o ../breeze.qrc
mv ../breeze.qrc .
rcc -binary -o breeze.rcc breeze.qrc
cd ..
cd ..

# clear old bundles
rm -rf /Applications/KDE/kate.app
rm -rf /Applications/KDE/kwrite.app

build_framework kate

# call patched macdeployqt for both bundles
# that needs patch from https://bugreports.qt.io/browse/QTBUG-48836
# deploy qt plugins as extra plugins, too, as e.g. iconengine will be missing otherwise ;)
for i in kwrite kate; do
    echo $i;
    cp -f src/iconres/breeze.rcc /Applications/KDE/$i.app/breeze.rcc
    cp -f $PREFIX/lib/libexec/kf5/kioslave /Applications/KDE/$i.app/Contents/MacOS
    cp -f $PREFIX/lib/libexec/kf5/kio_http_cache_cleaner /Applications/KDE/$i.app/Contents/MacOS
    macdeployqt /Applications/KDE/$i.app -executable=/Applications/KDE/$i.app/Contents/MacOS/kioslave -executable=/Applications/KDE/$i.app/Contents/MacOS/kio_http_cache_cleaner -extra-plugins=$PREFIX/lib/plugins -extra-plugins=$QTDIR/plugins -dmg
done
