#!/bin/bash

status "Installing Dependencies"
install_packages rsync cmake zlib1g-dev libjpeg-dev libjsoncpp-dev build-essential libbz2-dev libasound2-dev libsdl2-dev ffmpeg libavcodec-dev libavdevice-dev libswscale-dev libavformat-dev libavutil-dev libswresample-dev libgl-dev libglew-dev libgtk-3-dev libjack-jackd2-dev libjsoncpp-dev libmad0-dev libpng-dev libpulse-dev libtomcrypt-dev libtommath-dev libudev-dev libva-dev libvorbis-dev libxinerama-dev libx11-dev libxrandr-dev libxtst-dev yasm || exit 1

status "Cloning repo"
git_clone https://github.com/stepmania/stepmania.git || error "Could not clone repo"
cd stepmania || error "Could not enter stepmania folder"
git checkout d55acb1ba26f1c5b5e3048d6d6c0bd116625216f || error "Could not checkout desired commit"

# add patch
# this format of writing to a file changes none of the output (does not mess up '', "", or any special character)
# unfortunatly VSCODE does not do well with it for auto formatting but too bad

cat << 'EOF' >> 0001-patch-upstream-for-ARM.patch
From e7b9c1db0067915211741d1be436d086fa1a1410 Mon Sep 17 00:00:00 2001
From: theofficialgman <28281419+theofficialgman@users.noreply.github.com>
Date: Sat, 4 Jun 2022 17:11:35 -0400
Subject: [PATCH] patch upstream for ARM

---
 extern/libpng/configure.ac                  | 4 ++++
 extern/libpng/pngpriv.h                     | 4 ++--
 src/arch/MovieTexture/MovieTexture_FFMpeg.h | 2 +-
 3 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/extern/libpng/configure.ac b/extern/libpng/configure.ac
index 52dba94..1b95047 100644
--- a/extern/libpng/configure.ac
+++ b/extern/libpng/configure.ac
@@ -363,17 +363,21 @@ AC_ARG_ENABLE([arm-neon],
    [case "$enableval" in
       no|off)
          # disable the default enabling on __ARM_NEON__ systems:
+         AC_DEFINE([PNG_USE_ARM_NEON], [], [ARM NEON support])
          AC_DEFINE([PNG_ARM_NEON_OPT], [0],
                    [Disable ARM Neon optimizations])
          # Prevent inclusion of the assembler files below:
          enable_arm_neon=no;;
       check)
+         AC_DEFINE([PNG_USE_ARM_NEON], [], [ARM NEON support])
          AC_DEFINE([PNG_ARM_NEON_CHECK_SUPPORTED], [],
                    [Check for ARM Neon support at run-time]);;
       api)
+         AC_DEFINE([PNG_USE_ARM_NEON], [], [ARM NEON support])
          AC_DEFINE([PNG_ARM_NEON_API_SUPPORTED], [],
                    [Turn on ARM Neon optimizations at run-time]);;
       yes|on)
+         AC_DEFINE([PNG_USE_ARM_NEON], [], [ARM NEON support])
          AC_DEFINE([PNG_ARM_NEON_OPT], [2],
                    [Enable ARM Neon optimizations])
          AC_MSG_WARN([--enable-arm-neon: please specify 'check' or 'api', if]
diff --git a/extern/libpng/pngpriv.h b/extern/libpng/pngpriv.h
index 583c26f..0293a75 100644
--- a/extern/libpng/pngpriv.h
+++ b/extern/libpng/pngpriv.h
@@ -127,8 +127,8 @@
     * associated assembler code, pass --enable-arm-neon=no to configure
     * or put -DPNG_ARM_NEON_OPT=0 in CPPFLAGS.
     */
-#  if (defined(__ARM_NEON__) || defined(__ARM_NEON)) && \
-   defined(PNG_ALIGNED_MEMORY_SUPPORTED)
+#  if defined(PNG_USE_ARM_NEON) && (defined(__ARM_NEON__) || \
+     defined(__ARM_NEON)) && defined(PNG_ALIGNED_MEMORY_SUPPORTED)
 #     define PNG_ARM_NEON_OPT 2
 #  else
 #     define PNG_ARM_NEON_OPT 0
diff --git a/src/arch/MovieTexture/MovieTexture_FFMpeg.h b/src/arch/MovieTexture/MovieTexture_FFMpeg.h
index c092b76..36a6cf1 100644
--- a/src/arch/MovieTexture/MovieTexture_FFMpeg.h
+++ b/src/arch/MovieTexture/MovieTexture_FFMpeg.h
@@ -14,7 +14,7 @@ namespace avcodec
 		#include <libswscale/swscale.h>
 		#include <libavutil/pixdesc.h>
 
-		#if LIBAVCODEC_VERSION_MAJOR >= 58
+		#if LIBAVCODEC_VERSION_MAJOR >= 57
 		#define av_free_packet av_packet_unref
 		#define PixelFormat AVPixelFormat
 		#define PIX_FMT_YUYV422 AV_PIX_FMT_YUYV422
-- 
2.25.1

EOF

cat << 'EOF' >> pre-7.1-ffmpeg.patch
diff --git a/src/arch/MovieTexture/MovieTexture_FFMpeg.cpp b/src/arch/MovieTexture/MovieTexture_FFMpeg.cpp
index 935ddf324b0..d4eed01d599 100644
--- a/src/arch/MovieTexture/MovieTexture_FFMpeg.cpp
+++ b/src/arch/MovieTexture/MovieTexture_FFMpeg.cpp
@@ -405,7 +405,7 @@ void MovieTexture_FFMpeg::RegisterProtocols()
 		return;
 	Done = true;
 
-#if !FF_API_NEXT
+#if LIBAVCODEC_VERSION_MAJOR < 58
 	avcodec::avcodec_register_all();
 	avcodec::av_register_all();
 #endif
@@ -508,7 +508,7 @@ RString MovieDecoder_FFMpeg::OpenCodec()
 	if( m_pStreamCodec->codec )
 		avcodec::avcodec_close( m_pStreamCodec );
 
-	avcodec::AVCodec *pCodec = avcodec::avcodec_find_decoder( m_pStreamCodec->codec_id );
+	const avcodec::AVCodec *pCodec = avcodec::avcodec_find_decoder( m_pStreamCodec->codec_id );
 	if( pCodec == nullptr )
 		return ssprintf( "Couldn't find decoder %i", m_pStreamCodec->codec_id );
 
diff --git a/src/arch/MovieTexture/MovieTexture_FFMpeg.h b/src/arch/MovieTexture/MovieTexture_FFMpeg.h
index c092b765fc2..99f5ffcb1be 100644
--- a/src/arch/MovieTexture/MovieTexture_FFMpeg.h
+++ b/src/arch/MovieTexture/MovieTexture_FFMpeg.h
@@ -13,6 +13,7 @@ namespace avcodec
 		#include <libavformat/avformat.h>
 		#include <libswscale/swscale.h>
 		#include <libavutil/pixdesc.h>
+		#include <libavcodec/avcodec.h>
 
 		#if LIBAVCODEC_VERSION_MAJOR >= 57
 		#define av_free_packet av_packet_unref

EOF

# minor edits from https://github.com/h-lunah/stepmania-ddr/commit/46ad6dae13d9c2786147a38bfbd3b3812573ad22.patch
cat << 'EOF' >> 7.1-ffmpeg.patch
diff --git a/src/CMakeData-arch.cmake b/src/CMakeData-arch.cmake
index 1c330a50bbc..6c3a6f24865 100644
--- a/src/CMakeData-arch.cmake
+++ b/src/CMakeData-arch.cmake
@@ -105,6 +105,7 @@ else() # Unix
                 "arch/MovieTexture/MovieTexture_FFMpeg.cpp")
     list(APPEND SMDATA_ARCH_MOVIE_TEXTURE_HPP
                 "arch/MovieTexture/MovieTexture_FFMpeg.h")
+    add_compile_definitions(FF_API_NEXT)
   endif()
 endif()
 
diff --git a/src/arch/MovieTexture/MovieTexture_FFMpeg.cpp b/src/arch/MovieTexture/MovieTexture_FFMpeg.cpp
index 935ddf324b0..a74e003193d 100644
--- a/src/arch/MovieTexture/MovieTexture_FFMpeg.cpp
+++ b/src/arch/MovieTexture/MovieTexture_FFMpeg.cpp
@@ -112,6 +112,7 @@ MovieDecoder_FFMpeg::MovieDecoder_FFMpeg()
 	m_swsctx = NULL;
 	m_avioContext = NULL;
 	m_buffer = NULL;
+	m_pStreamCodec = NULL;
 	m_fctx = nullptr;
 	m_pStream = nullptr;
 	m_iCurrentPacketOffset = -1;
@@ -166,6 +167,7 @@ void MovieDecoder_FFMpeg::Init()
 	{
 		avcodec::avcodec_free_context(&m_pStreamCodec);
 	}
+	m_pStreamCodec = NULL;
 #endif
 }
 
@@ -273,7 +275,7 @@ int MovieDecoder_FFMpeg::DecodePacket( float fTargetTime )
 		bool bSkipThisFrame =
 			fTargetTime != -1 &&
 			GetTimestamp() + GetFrameDuration() < fTargetTime &&
-			(m_pStreamCodec->frame_number % 2) == 0;
+			(m_pStreamCodec->frame_num % 2) == 0;
 
 		int iGotFrame;
 		int len;
@@ -508,7 +510,7 @@ RString MovieDecoder_FFMpeg::OpenCodec()
 	if( m_pStreamCodec->codec )
 		avcodec::avcodec_close( m_pStreamCodec );
 
-	avcodec::AVCodec *pCodec = avcodec::avcodec_find_decoder( m_pStreamCodec->codec_id );
+	const avcodec::AVCodec *pCodec = avcodec::avcodec_find_decoder( m_pStreamCodec->codec_id );
 	if( pCodec == nullptr )
 		return ssprintf( "Couldn't find decoder %i", m_pStreamCodec->codec_id );
 
diff --git a/src/arch/MovieTexture/MovieTexture_FFMpeg.h b/src/arch/MovieTexture/MovieTexture_FFMpeg.h
index c092b765fc2..99f5ffcb1be 100644
--- a/src/arch/MovieTexture/MovieTexture_FFMpeg.h
+++ b/src/arch/MovieTexture/MovieTexture_FFMpeg.h
@@ -13,6 +13,7 @@ namespace avcodec
 		#include <libavformat/avformat.h>
 		#include <libswscale/swscale.h>
 		#include <libavutil/pixdesc.h>
+		#include <libavcodec/avcodec.h>
 
 		#if LIBAVCODEC_VERSION_MAJOR >= 57
 		#define av_free_packet av_packet_unref

EOF

git apply 0001-patch-upstream-for-ARM.patch || error "Applying patch failed"

ffmpeg_version="$(package_latest_version ffmpeg | awk -F'[:.-]' '{print $2 $3}')"
if [ "$ffmpeg_version" -ge 71 ] ; then
  git apply 7.1-ffmpeg.patch || error "Applying patch failed"
else
  git apply pre-7.1-ffmpeg.patch || error "Applying patch failed"
fi

mkdir build && cd build || error "Could not create or enter build directory"
status "Running cmake"
cmake -DCMAKE_BUILD_TYPE=Release -DWITH_SDL=yes -DWITH_SYSTEM_GLEW=yes -DWITH_SYSTEM_TOMMATH=yes -DWITH_SYSTEM_MAD=yes -DWITH_SYSTEM_ZLIB=yes -DWITH_SYSTEM_JPEG=yes -DWITH_SYSTEM_OGG=yes -DWITH_SYSTEM_FFMPEG=yes -DWITH_SYSTEM_TOMCRYPT=yes -DWITH_FULL_RELEASE=yes -DWITH_CRASH_HANDLER=no -DWITH_NETWORKING=yes -WITH_MINIMAID=no .. || error "Cmake ended with error"

status "Starting build"
make -j$(nproc) || "error Failed to build"
# installs to /usr/local/stepmania-5.1 folder for everything
status "Installing stepmania"
sudo make install || error "Could not install"
cd .. || error "could not move back directory"
# install icons (the installer does not do this)
sudo rsync -a ./icons/ /usr/local/share/icons/
# add .desktop file (the one in the repo is not good and is not installed)
echo "[Desktop Entry]
Encoding=UTF-8
Name=StepMania
GenericName=Rhythm and dance game
Exec=/usr/local/stepmania-5.1/stepmania
Terminal=false
Icon=stepmania-ssc
Type=Application
Categories=Application;Game;ArcadeGame
Comment=A cross-platform rhythm video game." | sudo tee /usr/local/share/applications/stepmania.desktop || error "Could not created .desktop file"

status_green "Install finished"
