#!/bin/bash

DIRECTORY="$(dirname "$(dirname "$(dirname "$0")")")"
function error {
  echo -e "\e[31m$1\e[39m"
  exit 1
}

# Get dependencies
"${DIRECTORY}/pkg-install" "rapidjson-dev libavdevice-dev libx11-xcb-dev abuse-lib abuse-sfx alsa-oss alsa-utils autoconf automake autotools-dev binfmt-support bison bluetooth bluez-tools build-essential ca-certificates check checkinstall cmake curl debhelper devscripts dh-autoreconf dh-systemd dialog dirmngr distcc dkms dos2unix dosfstools doxygen e2fsprogs exfat-fuse fbi fcitx-libs-dev feh flac flex fluid-soundfont-gm fontconfig fonts-freefont-ttf fonts-takao fonts-takao-gothic freepats g++ gawk gcc gcc-6 git golang graphviz gstreamer1.0-alsa gstreamer1.0-libav gstreamer1.0-plugins-good icu-devtools imagemagick insserv joystick jq kpartx lhasa libaldmb1-dev liballegro4-dev libao-dev libarchive-dev libasound2-dev libaudiofile-dev libavcodec-dev libavcodec-dev libavformat-dev libavfilter-dev libavformat-dev libavresample-dev libfontconfig1-dev libavutil-dev libbluetooth3 libbluetooth-dev libboost-all-dev libboost-dev libboost-filesystem-dev libboost-regex-dev libboost-system-dev libboost-thread-dev libbz2-dev libcaca-dev libcap-ng-dev libcurl4-openssl-dev libdbus-1-dev libdbus-glib-1-dev libdirectfb-dev libdrm-dev libdw-dev libegl1-mesa-dev libenet-dev libev-dev libevdev-dev libevdev-dev libexpat1-dev libevdev-tools libfaad-dev libflac++-dev libflac-dev libfluidsynth-dev libfontconfig1-dev libfreeimage-dev libfreetype6-dev libfreetype6-dev libcurl4-openssl-dev libfreetype6-dev libudev-dev libfreetype6-dev zlib1g-dev libgbm-dev libgl1-mesa-dev libgles2-mesa-dev libglew-dev libglm-dev libluajit-5.1-dev libglu1-mesa-dev libgme-dev libguichan-dev libibus-1.0-dev libical-dev libicu-dev libinput10 libjpeg-dev libjpeg-dev cmake libjpeg-dev libtiff5-dev liblua5.1-0-dev libluajit-5.1-dev liblzo2-dev libmad0-dev libmad0-dev libpng-dev libmbedtls-dev libmikmod-dev libminiupnpc-dev libmng-dev libmodplug-dev libmpeg2-4-dev libmpg123-dev libogg-dev libogg-dev libmodplug-dev libopenal-dev libopus-dev libpango1.0-dev libsndfile1-dev libpcap-dev libpciaccess-dev libphysfs-dev libpng-dev libpng-dev libtheora-dev libpthread-stubs0-dev libpulse-dev libraspberrypi-dev libraspberrypi-doc libreadline-dev librsvg2-bin libsamplerate0-dev libsdl1.2-dev libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-net-dev libsdl2-ttf-dev libsdl2-ttf-dev libopenal-dev libsdl2-ttf-dev libsdl2-image-dev libsdl-gfx1.2-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsfml-dev libsm-dev libvlc-dev libsnappy-dev libsndfile1-dev libsndio-dev libsoil-dev libsoundtouch-dev libspectrum-dev libspeexdsp-dev libsqlite3-dev libssl-dev libsubunit-dev libswscale-dev libtheora-dev libtinfo-dev libtolua++5.1-dev libtool libudev-dev libudev-dev libavahi-client-dev libuim-dev libusb-1.0-0-dev libusb-dev libvlccore-dev libvlc-dev libvorbis-dev libvorbisidec-dev libvpx-dev libvulkan-dev libwebp-dev libx11-dev libxext-dev libxi-dev libxinerama-dev libxkbcommon-dev libxkbcommon-x11-0 libxml2-dev libxrandr-dev libxt-dev libxv-dev libyaml-dev lynx-common lynx libzip-dev libzzip-dev lsb-release matchbox-window-manager mc meson ninja-build nodejs npm ntfs-3g omxplayer oss-compat p7zip-full pandoc parted patchelf pkg-config pkg-config libsdl2-dev policykit-1 portaudio19-dev pulseaudio-utils python2-dev python3 python3-dev python3-pip python3-pyudev python3-setuptools python-dbus python-dev python-gobject python-numpy python-pygame python-pyrex qt5-default qtbase5-private-dev raspberrypi-kernel-headers rename rsync scons sqlite3 squashfs-tools subversion tcl-dev texinfo timidity unzip uuid-dev virtualenv vlc wget x11proto-core-dev xa65 xmlstarlet xorg zenity zlib1g zlib1g-dev zlib1g-dev libjpeg-dev" "$(dirname "$0")" || exit 1

#Clone setup script
cd
git clone --depth=1 https://github.com/RetroPie/RetroPie-Setup.git

#Run setup
cd RetroPie-Setup
chmod +x retropie_setup.sh
sudo ./retropie_setup.sh

#Delete setup script
sudo rm -r ~/RetroPie-Setup
