#!/bin/bash
Version=3.4
XService=YES
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_ {
export TERM=xterm-256color
print_centered "$(tput setaf 10)"
print_centered "  _____        __               ";
print_centered " / ___/__ ____/ /  ___ _______ _";
print_centered "/ (_ / -_) __/ _ \/ -_) __/ _ \`/";
print_centered "\___/\__/_/ /_.__/\__/_/  \_,_/ ";
print_centered "                                ";
echo -n "$(tput setaf 11)"
print_centered "Gerbera UPNP installation script v$Version"
echo -n "$(tput sgr0)"
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_ {
pids=""
./tools/_cmake.sh & pids="$pids $!"
wait $pids
}

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 gerbera-1.12.1.tar.xz
fi
cd ~/Build_Cube/options/gerbera/scripts
sudo ./install-pupnp.sh
sudo ldconfig
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 ""
sudo ldconfig
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 -j6
sudo make install

cd ~
#sudo rm -r ~/Build_Cube/options/gerbera/

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_ {
sudo killall -9 gerbera > /dev/null 2>&1
if ! getent passwd gerbera > /dev/null 2>&1
then
sudo useradd --system gerbera
fi
if ! [ -d /etc/gerbera ]
then
sudo mkdir /etc/gerbera
sudo chown -Rv gerbera:gerbera /etc/gerbera
fi
sudo systemctl stop gerbera.service > /dev/null 2>&1
cat <<'EOF'>> gerbera.service
[Unit]
Description=Gerbera Media Server
After=network.target network-online.target
Wants=network-online.target

[Service]
Type=simple
User=wareck
Group=adm
ExecStart=/usr/local/bin/gerbera -c /home/wareck/.config/gerbera/config.xml
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF
sudo cp gerbera.service /lib/systemd/system/gerbera.service
sudo rm gerbera.service
sudo systemctl daemon-reload > /dev/null 2>&1
sudo systemctl start gerbera > /dev/null 2>&1
sudo systemctl enable gerbera > /dev/null 2>&1
echo "Done"
}

intro_
update_
cmake_
build_

echo -e "$(tput setaf 10)\nService optional :$(tput sgr0)"
if [ $XService = "YES" ]
then
 service_
fi
#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"
