#!/bin/bash
Version=2.2
function print_centered {
     [[ $# == 0 ]] && return 1

     declare -i TERM_COLS="$(tput cols)"
     declare -i str_len="${#1}"
     [[ $str_len -ge $TERM_COLS ]] && {
          echo "$1";
          return 0;
     }

     declare -i filler_len="$(( (TERM_COLS - str_len) / 2 ))"
     [[ $# -ge 2 ]] && ch="${2:0:1}" || ch=" "
     filler=""
     for (( i = 0; i < filler_len; i++ )); do
          filler="${filler}${ch}"
     done

     printf "%s%s%s" "$filler" "$1" "$filler"
     [[ $(( (TERM_COLS - str_len) % 2 )) -ne 0 ]] && printf "%s" "${ch}"
     printf "\n"

     return 0
}

print_centered "$(tput setaf 3)"
print_centered "  _____        __               ";
print_centered " / ___/__ ____/ /  ___ _______ _";
print_centered "/ (_ / -_) __/ _ \/ -_) __/ _ \`/";
print_centered "\___/\__/_/ /_.__/\__/_/  \_,_/ ";
print_centered "                                ";
echo -n "$(tput setaf 7)"
print_centered "Gerbera UPNP installation script v$Version"
echo ""
sleep 3
sudo apt-get update
sudo apt-get install uuid-dev libsqlite3-dev libmysqlclient-dev libmagic-dev libexif-dev libcurl4-openssl-dev libspdlog-dev libpugixml-dev -y
sudo apt-get install libavutil-dev libavcodec-dev libavformat-dev libavdevice-dev libavfilter-dev libavresample-dev libswscale-dev libswresample-dev libpostproc-dev -y
sudo apt-get install libebml-dev ffmpeg ffmpegthumbnailer libffmpegthumbnailer-dev imagemagick fmtools libfmt-dev libmatroska-dev -y
echo -e ""
cmake_v="$(cmake --version | grep "version" | awk '{print $3}')"
function version {
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }';
}
if ! [ $(version $cmake_v) -ge $(version "3.18") ]
then
echo -e "Cmake update:"
version=3.21
build=4
mkdir ~/temp
cd ~/temp
sudo apt-get remove --purge cmake -y
wget https://cmake.org/files/v$version/cmake-$version.$build.tar.gz
tar -xzvf cmake-$version.$build.tar.gz
cd cmake-$version.$build/
./bootstrap
./configure
make -j$(nproc)
sudo make install
rm -r -f ~/temp
else
echo -e "Cmake version O.K\n"
fi

cd  ~/Build_Cube/options
if [ ! -d gerbera ]
then
git clone https://github.com/gerbera/gerbera.git
fi

cd ~/Build_Cube/options/gerbera/scripts
sudo ./install-pupnp.sh
cd ~/Build_Cube/options/gerbera/scripts
sudo ./install-fmt.sh
cd ~/Build_Cube/options/gerbera/scripts
sudo ./install-spdlog.sh
cd ~/Build_Cube/options/gerbera/scripts
sudo ./install-duktape.sh
cd ~/Build_Cube/options/gerbera/scripts
sudo ./install-lastfm.sh
cd ~/Build_Cube/options/gerbera/scripts
sudo ./install-pugixml.sh

sed -i -e "s/rm -R taglib-build/rm -R taglib-build ||true/g" install-taglib.sh
sudo ./install-taglib.sh
echo -e ""
cd ~/Build_Cube/options/gerbera
mkdir build
cd build
cmake .. -DWITH_MAGIC=1 -DWITH_MYSQL=0 -DWITH_CURL=1 -DWITH_JS=1 -DWITH_TAGLIB=1 -DWITH_AVCODEC=1 -DWITH_FFMPEGTHUMBNAILER=1 -DWITH_EXIF=1 -DWITH_LASTFM=0
make -j2
sudo make install

cd ~
cp ~/Build_Cube/pack/gerbera.tar.xz .
tar xvfJ gerbera.tar.xz
rm gerbera.tar.xz

echo -e "\n$(tput setaf 3)Done.$(tput setaf 7)\n"
