#!/bin/bash

version=727
version2=1.2.5
base_ver="$(echo ${version} | sed -e 's/.$//' -e 's/.$//')"

install_packages xz-utils tar || exit 1
cd /tmp
wget -O reaper.tar.xz "https://www.reaper.fm/files/${base_ver}.x/reaper${version}_linux_armv7l.tar.xz" || error "Failed to download reaper!"
status "Extracting reaper..."
tar -xf reaper.tar.xz || error "Failed to extract reaper!"
cd reaper_linux_armv7l || error "Failed to cd into 'reaper_linux_armv7l'"
status_green "Done!"
status "Installing reaper..."
chmod +x install-reaper.sh || error "Failed to make 'install-reaper.sh' executable!"
sudo ./install-reaper.sh --install /opt || error "Failed to run 'sudo install-reaper.sh --install /opt'"
sudo rm -f /usr/local/bin/reaper &>/dev/null
sudo ln -s /opt/REAPER/reaper /usr/local/bin/reaper || error "Failed to link /opt/REAPER/reaper to /usr/local/bin/reaper!"
mkdir -p "$HOME/.local/share/icons/hicolor/64x64/apps/" || error "Failed to make icon directory!"
cp "${DIRECTORY}/apps/Reaper/icon-64.png" "$HOME/.local/share/icons/hicolor/64x64/apps/reaper.png" || error "Failed to copy icon!"
echo "[Desktop Entry]
Name=Reaper
Comment=Reaper is a complete Digital Audio Workstation, for computers, offering a full multitrack audio and MIDI recording, editing, processing, mixing and mastering toolset. 
Exec=reaper
Icon=$HOME/.local/share/icons/hicolor/64x64/apps/reaper.png
Terminal=false
Categories=AudioVideo;Audio;
Type=Application
" | tee "$HOME/.local/share/applications/reaper.desktop" || error "Failed to make menu entry!"
status_green "Done!"
status "Installing ReaPack (Reaper Package Manager)..."
mkdir -p "$HOME/.config/REAPER/UserPlugins" || error "Failed to create '$HOME/.config/REAPER/UserPlugins'!"
wget -O "$HOME/.config/REAPER/UserPlugins/reaper_reapack-armv7l.so" "https://github.com/cfillion/reapack/releases/download/v${version2}/reaper_reapack-armv7l.so" || error "Failed to download ReaPack!!!"
status_green "Done!"
status "You can use ReaPack by opening Reaper and navigating to Extentions -> ReaPack.\n"
status "Cleaning up..."
rm -rf reaper.tar.xz reaper_linux_armv7l
status_green "Done!"
