#!/bin/bash
Version=2.6
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
}
# function to set terminal title
function set-title() {
  if [[ -z "$ORIG" ]]; then
    ORIG=$PS1
  fi
  TITLE="\[\e]2;$*\a\]"
  PS1=${ORIG}${TITLE}
}

function intro_ {
print_centered "$(tput setaf 165)"
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
}

function update_ {
echo -e "$(tput setaf 10)\nUpdate/install packages:$(tput sgr0)"
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 ""
}

function version {
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }';
}

function cmake_ {
echo -e "$(tput setaf 10)\nInstall cmake:$(tput sgr0)"
if [ -x "$(command -v cmake)" ];then flag=1; else flag=0;fi
case $flag in
0)
	wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/cmake_3.25.2-1_amd64.deb
	sudo dpkg -i cmake_3.25.2-1_amd64.deb
	rm cmake_3.25.2-1_amd64.deb
	;;
1)
	cmake_v="$(cmake --version | grep "version" | awk '{print $3}')"
	echo "cmake version : $cmake_v"
	sleep 2
	if ! [ $(version $cmake_v) -ge $(version "3.18") ]
	then
	echo -e "cmake need update=>"
	sudo apt-get -qq remove --purge cmake -y
	wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/cmake_3.25.2-1_amd64.deb
	sudo dpkg -i cmake_3.25.2-1_amd64.deb
	rm cmake_3.25.2-1_amd64.deb
	else
	echo -e "cmake version ok."
	sleep 2
	fi
	;;
*)	echo "Error";;
esac
}

function cmake_source {
echo -e "$(tput setaf 10)\nInstall cmake:$(tput sgr0)"
if [ -x "$(command -v cmake)" ];then flag=1; else flag=0;fi
case $flag in
0)
	wget -c https://github.com/Kitware/CMake/releases/download/v3.25.2/cmake-3.25.2.tar.gz
	tar xvfz cmake-3.25.2.tar.gz
	cd cmake-3.25.2
	./bootstrap
	make
	sudo make install
	cd ..
	rm -r -f cmake-3.25.2
	rm cmake-3.25.2.tar.gz
        ;;
1)
        cmake_v="$(cmake --version | grep "version" | awk '{print $3}')"
        echo "cmake version : $cmake_v"
        sleep 2
        if ! [ $(version $cmake_v) -ge $(version "3.18") ]
        then
        echo -e "cmake need update =>:"
	wget -c https://github.com/Kitware/CMake/releases/download/v3.25.2/cmake-3.25.2.tar.gz
        tar xvfz cmake-3.25.2.tar.gz
        cd cmake-3.25.2
        ./bootstrap
        make
        sudo make install
        cd ..
        rm -r -f cmake-3.25.2
        rm cmake-3.25.2.tar.gz
        else
        echo -e "cmake version ok."
        sleep 2
        fi
        ;;
*)      echo "Error";;
esac
}

function build_ {
echo -e "$(tput setaf 10)\nBuild Gerbera:$(tput sgr0)"

cd  ~/Build_Cube/options
if [ ! -d gerbera ]
then
#git clone https://github.com/gerbera/gerbera.git
wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/gerbera-1.12.1.tar.xz
tar xvfJ gerbera-1.12.1.tar.xz
mv gerbera-1.12.1 gerbera
rm xvfJ gerbera-1.12.1.tar.xz
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
cd ~/Build_Cube/options/gerbera/scripts
sudo ./install-taglib.sh
echo -e ""
cd ~/Build_Cube/options/gerbera
mkdir build
cd build
cmake .. -DWITH_MAGIC=1 -DWITH_CURL=1 -DWITH_JS=1 -DWITH_TAGLIB=1 -DWITH_AVCODEC=1 -DWITH_FFMPEGTHUMBNAILER=1 -DWITH_EXIF=1 -DWITH_LASTFM=0 -DWITH_MYSQL=0
make -j4
sudo make install

cd ~
wget -c http://folivier.homelinux.org/cube/pack/gerbera.tar.xz
tar xvfJ gerbera.tar.xz
rm gerbera.tar.xz

echo -e "\n\e[97mEnd of process...\e[0m"
}

function service_ {
cd /home/wareck
if [ -f gerbera.service ];then rm gerbera.service;fi
cat <<'EOF'>> gerbera.service
#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          gerbera
# Required-Start:    networking
# Default-Start:     3 4 5
# Default-Stop:      0 6
### END INIT INFO
case "$1" in
        start)
        su wareck -c "screen -dmS gerbera /usr/local/bin/gerbera"
        ;;
        stop)
        sudo killall -9 gerbera | true
        ;;
        *)
        echo "Usage: /etc/init.d/gerbera.service {start|stop}"
        exit 1
        ;;
esac

exit 0
EOF
chmod +x gerbera.service
sudo cp /home/wareck/gerbera.service /etc/init.d/
sudo update-rc.d gerbera.service defaults
sudo rm /home/wareck/gerbera.service
sudo rm /lib/systemd/system/gerbera.service
echo "Done"
}

intro_
update_
cmake_
build_

echo -e "$(tput setaf 10)\nService optional :$(tput sgr0)"

while true; do
read -p "Do you want to install service (y/n) ? " yn

case $yn in
        [Yy] ) service_ ;
		exit 1;;
        [Nn] ) echo exiting...;
                exit;;
        * ) echo invalid response;
                exit 1;;
esac
done

echo -e "\n\e[97mEnd of process...\e[0m"
