#!/bin/sh
OGRE_INSTALL_PATH=$(pwd)/ogre-sdk
BLENDER_INSTALL_PATH=$(pwd)/blender-base
QT_PATH=/usr/lib/qt
# -DQt5_ROOT=/Path/To/QtInstallationFolder/5.12.5
set -e
#rm -Rf ogre/build
#mkdir -p ogre/build
#cd ogre/build
#cmake .. -DCMAKE_BUILD_TYPE=Debug -DOGRE_BUILD_RENDERSYSTEM_VULKAN:BOOL=TRUE -DOGRE_STATIC:BOOL=TRUE -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=${OGRE_INSTALL_PATH}
#make -j8
#make install
#cd ../..
#rm -Rf assimp/build
#mkdir -p assimp/build
#cd assimp/build
#cmake .. -DCMAKE_INSTALL_PREFIX=${OGRE_INSTALL_PATH} -DCMAKE_PREFIX_PATH=${OGRE_INSTALL_PATH} -DASSIMP_BUILD_ASSIMP_TOOLS=ON -DBUILD_SHARED_LIBS=OFF
#make -j8
#make install
#cd ../..

build_cmake_stuff() {
	set -e
	EXTRA_FLAGS=
	BBLENDER=false
	BBLENDERSTATIC=false
	NOINSTALL=false
	BSTATIC=false
	BCLEAN=false
	BDEBUG=false
	BRELEASE=false
	EXTRA_FLAGS=""
	BTYPE=""
	INSTALL_PATH=${OGRE_INSTALL_PATH}

	while true; do
		case $1 in
			noinstall)
				shift
				NOINSTALL=true
				;;
			static)
				shift
				BSTATIC=true
				;;
			clean)
				shift
				BCLEAN=true
				;;
			debug)
				shift
				BDEBUG=true
				;;
			release)
				shift
				BRELEASE=true
				;;
			blender)
				shift
				BBLENDER=true
				;;
			blender-static)
				shift
				BBLENDERSTATIC=true
				;;
			--)
				shift
				break
				;;
			*)
				break
				;;
		esac
	done
	echo Package: $1 ...
	if [ x$BDEBUG = xtrue -a x$BRELEASE = xtrue ]; then
		BTYPE="-DCMAKE_BUILD_TYPE=RelWidthDebInfo"
	elif [ x$BDEBUG = xtrue ]; then
		BTYPE="-DCMAKE_BUILD_TYPE=Debug"
	elif [ x$BRELEASE = xtrue ]; then
		BTYPE="-DCMAKE_BUILD_TYPE=Release"
	fi
	BUILD_DIR=$1/build
	if [ x${BSTATIC} = xtrue ]; then
		BUILD_DIR=$1/build-static
		EXTRA_FLAGS="-DBUILD_SHARED_LIBS=OFF ${EXTRA_FLAGS}"
		INSTALL_PATH=${OGRE_INSTALL_PATH}-static
	fi
	if [ x${BBLENDERSTATIC} = xtrue ]; then
		BUILD_DIR=$1/build-blender
		EXTRA_FLAGS="-DBUILD_SHARED_LIBS=OFF ${EXTRA_FLAGS}"
		INSTALL_PATH=${BLENDER_INSTALL_PATH}
	fi
	if [ x${BBLENDER} = xtrue ]; then
		BUILD_DIR=$1/build-blender
		EXTRA_FLAGS="${EXTRA_FLAGS}"
		INSTALL_PATH=${BLENDER_INSTALL_PATH}
	fi
	if [ x$1 = xogre ]; then
		EXTRA_FLAGS="-DASSIMP_BUILD_ASSIMP_TOOLS=ON ${EXTRA_FLAGS}"
		if [ x$BSTATIC = xtrue ]; then
			EXTRA_FLAGS="-DOGRE_STATIC=TRUE ${EXTRA_FLAGS}"
		fi
	elif [ "$1" = "SPIRV-Tools" ]; then
		EXTRA_FLAGS="-DSPIRV-Headers_SOURCE_DIR=${INSTALL_PATH} ${EXTRA_FLAGS}"
	fi
	MDIR=$(pwd)
	if [ x$BCLEAN = xtrue ]; then
		rm -Rf ${BUILD_DIR}
	fi
	mkdir -p ${BUILD_DIR}
	cd ${BUILD_DIR}
	export PKG_CONFIG_PATH=${INSTALL_PATH}/lib/pkgconfig
	DISPLAY= cmake .. ${BTYPE} -DCMAKE_PREFIX_PATH=${INSTALL_PATH} -DCMAKE_INSTALL_PREFIX=${INSTALL_PATH} -DCMAKE_MODULE_DIR=${INSTALL_PATH} ${EXTRA_FLAGS} $2
	DISPLAY= make VERBOSE=1 -j10
	if [ x$NOINSTALL = xfalse ]; then
		DISPLAY= make install
	fi
	cd $MDIR
}

build_ffmpeg() {
	set -e
	EXTRA_FLAGS=
	NOINSTALL=false
	BSTATIC=false
	BCLEAN=false
	BBLENDER=false
	INSTALL_PATH=${OGRE_INSTALL_PATH}
	while true; do
		case $1 in
			noinstall)
				shift
				NOINSTALL=true
				;;
			static)
				shift
				BSTATIC=true
				;;
			clean)
				shift
				BCLEAN=true
				;;
			debug)
				shift
				;;
			release)
				shift
				;;
			blender)
				BBLENDER=true
				shift
				;;
			--)
				shift
				break
				;;
			*)
				break
				;;
		esac
	done
	echo Package: $1
	BUILD_DIR=$1/build
	if [ x$BSTATIC = xtrue ]; then
		BUILD_DIR=$1/build-static
		EXTRA_FLAGS="--disable-pic --enable-static --disable-shared ${EXTRA_FLAGS}"
		INSTALL_PATH=${OGRE_INSTALL_PATH}-static
	elif [ x$BBLENDER = xtrue ]; then
		BUILD_DIR=$1/build-blender
		EXTRA_FLAGS="--enable-pic --enable-shared --disable-static ${EXTRA_FLAGS}"
		INSTALL_PATH=${BLENDER_INSTALL_PATH}
	else
		EXTRA_FLAGS="--enable-pic --disable-shared --enable-static ${EXTRA_FLAGS}"
	fi
	MDIR=$(pwd)
	if [ x$BCLEAN = xtrue ]; then
		rm -Rf ${BUILD_DIR}
	fi
	mkdir -p ${BUILD_DIR}
	cd ${BUILD_DIR}
	export PKG_CONFIG_PATH=${INSTALL_PATH}/lib/pkgconfig
	../configure --prefix=${INSTALL_PATH} ${EXTRA_FLAGS} --disable-programs \
		--extra-libs="$(pkg-config wayland-client wayland-protocols x11 libdrm --libs)" 
	DISPLAY= make -j10
	if [ x$NOINSTALL = xfalse ]; then
		DISPLAY= make install
	fi
	cd $MDIR
}
CLEAN=false
while true; do
	case $1 in
		clean)
			shift
			CLEAN=true
			;;
		--)
			shift
			break
			;;
		*)
			break
			;;
	esac
done

if [ x$CLEAN = xtrue ]; then
	rm -Rf assimp/build assimp/build-static
	rm -Rf assimp/build assimp/build-static
	rm -Rf ogre/build ogre/build-static ogre-sdk ogre-sdk-static
	rm -Rf ogre-procedural/build ogre-procedural/build-static
	rm -Rf ogre-projects/world2/build ogre-projects/world2/build-static
	for d in zlib libjpeg-turbo libpng pugixml ffmpeg assimp \
		SDL SPIRV-Headers SPIRV-Tools glslang SPIRV-Cross \
		ogre ogre-procedural ogre-pagedgeometry ogrewater; do
		rm -Rf $d/build $d/build-static
	done
fi

CPU=$(uname -m)
BLENDER_DIST=prebuilts/Blender-3.6-unknown-linux-${CPU}.tar.xz

if [ ! -e blender-bin/bin/blender ]; then

	[ -f ${BLENDER_DIST} ] || ./build-blender.sh
	mkdir -p blender-bin
	tar xf ${BLENDER_DIST} -C blender-bin
	mkdir -p blender-bin/bin
	ln -sf ../Blender-3.6-unknown-linux-${CPU}/blender blender-bin/bin/blender
fi

build_cmake_stuff debug release zlib "-DZLIB_BUILD_STATIC=OFF -DZLIB_BUILD_SHARED=ON -DZLIB_BUILD_MINIZIP=ON -DMINIZIP_BUILD_SHARED=ON -DMINIZIP_BUILD_STATIC=OFF -DMINIZIP_ENABLE_BZIP2=OFF"
build_cmake_stuff debug release clean static zlib "-DZLIB_BUILD_STATIC=ON -DZLIB_BUILD_SHARED=OFF -DZLIB_BUILD_MINIZIP=OFF -DMINIZIP_BUILD_SHARED=OFF -DMINIZIP_BUILD_STATIC=ON -DMINIZIP_ENABLE_BZIP2=OFF"
build_cmake_stuff debug release clean static zlib "-DZLIB_BUILD_STATIC=ON -DZLIB_BUILD_SHARED=OFF -DZLIB_BUILD_MINIZIP=ON -DMINIZIP_BUILD_SHARED=OFF -DMINIZIP_BUILD_STATIC=ON -DMINIZIP_ENABLE_BZIP2=OFF"

build_cmake_stuff release debug libjpeg-turbo ""
build_cmake_stuff release debug static libjpeg-turbo ""
build_cmake_stuff release debug libpng "-DPNG_SHARED=ON"
build_cmake_stuff release debug static libpng "-DPNG_SHARED=OFF"
build_cmake_stuff release debug pugixml ""
build_cmake_stuff release debug static pugixml ""

build_ffmpeg ffmpeg
build_ffmpeg static ffmpeg

build_cmake_stuff debug release assimp "-DASSIMP_BUILD_ZLIB=ON -DBUILD_SHARED_LIBS=OFF"
build_cmake_stuff debug release static assimp "-DASSIMP_BUILD_ZLIB=ON -DBUILD_SHARED_LIBS=OFF"

build_cmake_stuff debug release SDL "-DCMAKE_BUILD_TYPE=RelWithDebInfo \
			-DSDL_SHARED=OFF -DSDL_STATIC=ON -DSDL_STATIC_PIC=ON -DSDL_X11=ON \
			-DSDL_X11_SHARED=OFF -DSDL_X11_XCURSOR=ON -DSDL_X11_XDBE=ON \
			-DSDL_X11_XINPUT=ON -DSDL_X11_XRANDR=ON -DSDL_X11_XSCRNSAVER=ON -DSDL_X11_XSHAPE=ON -DSDL_WAYLAND=ON \
			-DSDL_WAYLAND_SHARED=OFF -DSDL_WAYLAND_LIBDECOR=ON -DSDL_WAYLAND_LIBDECOR_SHARED=OFF -DSDL_KMSDRM=ON -DSDL_KMSDRM_SHARED=OFF -DSDL_OPENGL=ON \
			-DSDL_OPENGLES=ON -DSDL_VULKAN=ON-DSDL_ALSA=ON -DSDL_ALSA_SHARED=OFF -DSDL_PIPEWIRE=ON -DSDL_PIPEWIRE_SHARED=OFF \
			-DSDL_PULSEAUDIO=ON -DSDL_PULSEAUDIO_SHARED=OFF -DSDL_RPATH=OFF -DSDL_HIDAPI=ON -DSDL_HIDAPI_JOYSTICK=ON"
build_cmake_stuff debug release static SDL "-DCMAKE_BUILD_TYPE=RelWithDebInfo \
			-DSDL_SHARED=OFF -DSDL_STATIC=ON -DSDL_X11=ON \
			-DSDL_X11_SHARED=OFF -DSDL_X11_XCURSOR=ON -DSDL_X11_XDBE=ON \
			-DSDL_X11_XINPUT=ON -DSDL_X11_XRANDR=ON -DSDL_X11_XSCRNSAVER=ON -DSDL_X11_XSHAPE=ON -DSDL_WAYLAND=ON \
			-DSDL_WAYLAND_SHARED=OFF -DSDL_WAYLAND_LIBDECOR=ON -DSDL_WAYLAND_LIBDECOR_SHARED=OFF -DSDL_KMSDRM=ON -DSDL_KMSDRM_SHARED=OFF -DSDL_OPENGL=ON \
			-DSDL_OPENGLES=ON -DSDL_VULKAN=ON-DSDL_ALSA=ON -DSDL_ALSA_SHARED=OFF -DSDL_PIPEWIRE=ON -DSDL_PIPEWIRE_SHARED=OFF \
			-DSDL_PULSEAUDIO=ON -DSDL_PULSEAUDIO_SHARED=OFF -DSDL_RPATH=OFF -DSDL_HIDAPI=ON -DSDL_HIDAPI_JOYSTICK=ON"
build_cmake_stuff release SPIRV-Headers ""
build_cmake_stuff release SPIRV-Tools "-DBUILD_SHARED_LIBS=OFF -DSPIRV_TOOLS_BUILD_STATIC=ON -DSPIRV_TOOLS_LIBRARY_TYPE=STATIC"
cd glslang
python3 update_glslang_sources.py
cd ..
build_cmake_stuff release noinstall glslang "-DBUILD_SHARED_LIBS=OFF -DENABLE_HLSL=TRUE -DENABLE_SPIRV=TRUE"
build_cmake_stuff release noinstall static glslang "-DBUILD_SHARED_LIBS=OFF -DENABLE_HLSL=TRUE -DENABLE_SPIRV=TRUE"
install -m 755 -d ogre-sdk/bin
install -m 755 -d ogre-sdk-static/bin
install -m 755  -c glslang/build/StandAlone/glslangValidator ogre-sdk/bin/
install -m 755  -c glslang/build-static/StandAlone/glslangValidator ogre-sdk-static/bin/
build_cmake_stuff debug release SPIRV-Cross "-DBUILD_SHARED_LIBS=OFF"
build_cmake_stuff debug release static SPIRV-Cross "-DBUILD_SHARED_LIBS=OFF"
build_cmake_stuff ogre "-DOGRE_GLSUPPORT_USE_EGL:BOOL=FALSE -DOGRE_BUILD_RENDERSYSTEM_VULKAN:BOOL=TRUE -DOGRE_INSTALL_DEPENDENCIES:BOOL=TRUE"
build_cmake_stuff static ogre "-DOGRE_GLSUPPORT_USE_EGL:BOOL=FALSE -DOGRE_BUILD_RENDERSYSTEM_VULKAN:BOOL=TRUE -DOGRE_INSTALL_DEPENDENCIES:BOOL=TRUE"
build_cmake_stuff debug release ogre-procedural "-DOgreProcedural_BUILD_DOCS:BOOL=FALSE"
build_cmake_stuff debug release static ogre-procedural "-DOgreProcedural_BUILD_DOCS:BOOL=FALSE -DOgreProcedural_STATIC=TRUE -DOgreProcedural_BUILD_SAMPLES=FALSE"
build_cmake_stuff clean debug release ogre-pagedgeometry ""
build_cmake_stuff clean debug release static ogre-pagedgeometry ""
#build_cmake_stuff clean debug release OgreCrowd "-DBUILD_DEMO=OFF"
build_cmake_stuff debug release noinstall clean ogrewater ""
build_cmake_stuff debug release noinstall clean static ogrewater ""
build_cmake_stuff debug release flecs "-DFLECS_STATIC=ON -DFLECS_SHARED=OFF -DFLECS_PIC=ON -DFLECS_TESTS=OFF"
build_cmake_stuff debug release static flecs "-DFLECS_STATIC=ON -DFLECS_SHARED=OFF -DFLECS_PIC=ON -DFLECS_TESTS=OFF"
build_cmake_stuff debug noinstall clean ogre-projects/world2 ""
build_cmake_stuff debug noinstall clean static ogre-projects/world2 "-DOGRE_DYNAMIC=OFF"
exit 0

#rm -Rf ogre/build
mkdir -p ogre/build
cd ogre/build
#cmake .. -DCMAKE_BUILD_TYPE=Debug -DOGRE_BUILD_RENDERSYSTEM_VULKAN:BOOL=TRUE -DCMAKE_INSTALL_PREFIX=${OGRE_INSTALL_PATH}
cmake .. -DOGRE_GLSUPPORT_USE_EGL:BOOL=FALSE -DOGRE_BUILD_RENDERSYSTEM_VULKAN:BOOL=TRUE -DOGRE_INSTALL_DEPENDENCIES:BOOL=TRUE -DCMAKE_INSTALL_PREFIX=${OGRE_INSTALL_PATH}
make -j8
 make install
#cd ../..

#rm -Rf ogre-pagedgeometry/build
#mkdir -p ogre-pagedgeometry/build
#cd ogre-pagedgeometry/build
#cmake .. -DCMAKE_PREFIX_PATH=${OGRE_INSTALL_PATH} -DCMAKE_INSTALL_PREFIX=${OGRE_INSTALL_PATH}
#make -j8
#make install

#exit
cd ../..
#rm -Rf ${OGRE_INSTALL_PATH}/include/bullet
#cp -a ogre/build/Dependencies/include/bullet ${OGRE_INSTALL_PATH}/include/bullet

#rm -Rf mygui/build
#mkdir -p mygui/build
#cd mygui/build
#cmake .. -DMYGUI_STATIC=TRUE -DMYGUI_RENDER_SYSTEM=3 -DCMAKE_INSTALL_PREFIX=${OGRE_INSTALL_PATH} -DCMAKE_PREFIX_PATH=${OGRE_INSTALL_PATH}
#make -j8
#cd ../..

#rm -Rf ogitor/build
#mkdir -p ogitor/build
#cd ogitor/build
##cmake .. -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=${OGRE_INSTALL_PATH} # -DOGITOR_DIST:BOOL=TRUE
#cmake .. -DBUILD_SHARED_LIBS=OFF -DOGRE_DIR=${OGRE_INSTALL_PATH} -DCMAKE_PREFIX_PATH=${OGRE_INSTALL_PATH} -DCMAKE_INSTALL_PREFIX=${OGRE_INSTALL_PATH} #-DCMAKE_POLICY_VERSION_MINIMUM=3.5 # -DOGITOR_DIST:BOOL=TRUE
#make -j8
#make install
#cd ../..
#mkdir -p ogre-caelum/build
#cd ogre-caelum/build
#cmake .. -DCMAKE_PREFIX_PATH=${OGRE_INSTALL_PATH} -DCMAKE_INSTALL_PREFIX=${OGRE_INSTALL_PATH} -DCAELUM_BUILD_SAMPLES=TRUE #-DCMAKE_POLICY_VERSION_MINIMUM=3.5 # -DOGITOR_DIST:BOOL=TRUE
#make -j8
#make install
#cd ../..

build_cmake_stuff ogre-procedural "-DOgreProcedural_BUILD_DOCS:BOOL=FALSE"

mkdir -p ogrewater/build
cd ogrewater/build
cmake .. -DBUILD_SHARED_LIBS=ON -DOGRE_DIR=${OGRE_INSTALL_PATH} -DCMAKE_INSTALL_PREFIX=${OGRE_INSTALL_PATH} #-DCMAKE_POLICY_VERSION_MINIMUM=3.5 # -DOGITOR_DIST:BOOL=TRUE
make -j8
#make install
cd ../..

#build_cmake_stuff ogre-projects/world2 "-DCMAKE_BUILD_TYPE=Debug -DCMAKE_PREFIX_PATH=${OGRE_INSTALL_PATH}"

rm -Rf ogre-projects/world2/build
mkdir -p ogre-projects/world2/build
cd ogre-projects/world2/build
#cmake .. -DCMAKE_BUILD_TYPE=Debug -DOGRE_DIR=${OGRE_INSTALL_PATH} -DOGRE_STATIC:BOOL=TRUE -DCMAKE_PREFIX_PATH=${OGRE_INSTALL_PATH} #-DCMAKE_INSTALL_PREFIX=${OGRE_INSTALL_PATH} # -DOGITOR_DIST:BOOL=TRUE
#cmake .. -DOGRE_DIR=${OGRE_INSTALL_PATH} -DOGRE_STATIC:BOOL=TRUE -DCMAKE_PREFIX_PATH=${OGRE_INSTALL_PATH} #-DCMAKE_INSTALL_PREFIX=${OGRE_INSTALL_PATH} # -DOGITOR_DIST:BOOL=TRUE
cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_PREFIX_PATH=${OGRE_INSTALL_PATH} #-DCMAKE_INSTALL_PREFIX=${OGRE_INSTALL_PATH} # -DOGITOR_DIST:BOOL=TRUE
make -j8
#make install
#rm -f ../characters/female/vroid-normal-female.scene characters/male/vroid-normal-male.scene

#rm -Rf ../characters/female ../characters/male ../characters/shapes
#make import_vrm


