#!/bin/bash

echo "Setting up dependencies..."
apt-get update && apt-get -y install \
    libgtest-dev \
    libsfml-dev \
    libglew-dev \
    qt5-default \
    libopenal-dev \
    libjpeg-dev \
    libpng-dev \
    libfreetype6-dev \
    libvorbis-dev \
    libfaad-dev \
    libfmt3-dev \
    build-essential \
    cmake \
    libssl-dev \
    libmpg123-dev \
    gcc g++ gcc-multilib gcc-arm-none-eabi

echo "The libraries will be configured in /opt/freeshop !"
mkdir /opt/freeshop
cd /opt/freeshop
echo "Setting up devkitARM_r46."
echo "Starting wget download of devkitARM."
wget https://sourceforge.net/projects/devkitpro/files/devkitARM/devkitARM_r46/devkitARM_r46-x86_64-linux.tar.bz2/download -O /opt/freeshop/devkitARM_r46-x86_64-linux.tar.bz2 -q
echo "Decompressing devkitARM_r46."
tar xvjf devkitARM_r46-x86_64-linux.tar.bz2
echo "Setting up libctru 1.2.1"
echo "Starting wget download of libctru 1.2.1"
mkdir /opt/freeshop/libctru
cd /opt/freeshop/libctru
wget https://github.com/smealum/ctrulib/releases/download/v1.2.1/libctru-1.2.1.tar.bz2 -O /opt/freeshop/libctru/libctru-1.2.1.tar.bz2 -q
echo "Decompressing libctru 1.2.1"
tar xvjf libctru-1.2.1.tar.bz2
echo "Setting up citro3d"
echo "Starting wget download of citro3d"
wget https://sourceforge.net/projects/devkitpro/files/citro3d/1.2.0/citro3d-1.2.0.tar.bz2/download -O /opt/freeshop/libctru/citro3d-1.2.0.tar.bz2 -q
tar xvjf citro3d-1.2.0.tar.bz2
cd /opt/freeshop

echo "Adding devkitARM to path..."
echo "export DEVKITPRO=/opt/freeshop" >> /home/vagrant/.bashrc
echo "export DEVKITARM=\$DEVKITPRO/devkitARM" >> /home/vagrant/.bashrc
echo "Setting up CPP3DS (note CPP3DS is complicated to compile, this _may_ fail."
git clone https://github.com/Naxann/cpp3ds.git
cd cpp3ds

echo "Downloading 3DS portlibs..."
wget -q https://github.com/cpp3ds/3ds_portlibs/releases/download/r5/portlibs-3ds-r5.tar.xz -O portlibs.tar.xz && \
    tar -xaf portlibs.tar.xz && \
    rm portlibs.tar.xz && \
    ln -s $(pwd)/portlibs $DEVKITPRO/portlibs && \
    ln -s $DEVKITPRO/portlibs/3ds $DEVKITPRO/portlibs/armv6k

echo "Building libarchive..."
git clone https://github.com/Cruel/3ds_portlibs.git
cd 3ds_portlibs
make libarchive
make install
cd ../
rm -rf 3ds_portlibs

echo "Building libmpg123"
wget -O libmpg123-dev.tar.gz  https://notabug.org/attachments/216a6d61-f167-4f65-84dc-fa98c2247fc1
tar -xaf libmpg123-dev.tar.gz -C $DEVKITPRO/portlibs/3ds
rm libmpg123-dev.tar.gz

echo "Downloading 3DS tools..."
wget -q https://github.com/cpp3ds/3ds-tools/releases/download/r6/3ds-tools-linux-r6.tar.gz -O tools.tar.gz && \
    tar -xaf tools.tar.gz && \
    cp 3ds-tools/* $DEVKITARM/bin && \
    rm tools.tar.gz

echo "Building googletest"
cd /usr/src/googletest/googletest
cmake . && \
    make -j4 && \
    cp *.a /usr/lib && \
make clean

echo "Exporting the CPP3DS environment variable."
echo "export CPP3DS=/opt/freeshop/cpp3ds/" >> ~/.bashrc

echo "Building CPP3DS"
cd $DEVKITPRO/cpp3ds
mkdir build && \
    cd build && \
    cmake -DBUILD_EMULATOR=ON -DENABLE_OGG=ON -DENABLE_AAC=ON -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=ON .. && \
    make -j4 && \
    mv lib .. && \
    cd .. && \
./bin/tests