#!/bin/bash

function error
{
  echo ""
  echo "    ========================================================================  "
  echo "  ((                      An Error has Been Encountered                     ))"
  echo "    ========================================================================  "
  echo ""
  echo ""
  echo "        In the most cases, some libraries are missing or cannot be found."
  echo "      Think to install the *-dev or *-devel packages of your distribution."
  echo "                     For example, if libmng cannot be found,"
  echo "  please check if libmng AND libmng-devel packages are installed on your system."
  echo "      See the reported errors to know which libraries need to be installed."
  echo "      If it complains about not being able to find the file FindGpgme.cmake, "
  echo "                 you will need the -devel package of kdepimlibs."
  echo "         Consult the INSTALL document  for a full list of the requirements."
  echo ""
  echo $'\a' # Beep to avert user of the error
  exit 1
}

function prefix_cmd
{
	if test -n "$1"; then
		echo '-DCMAKE_INSTALL_PREFIX='$1''
	fi
}

function qtplugin_prefix_cmd
{
	if test -n "$1"; then
		echo '-DKDE_INSTALL_QTPLUGINDIR='$1''
	fi
}

function do_warning
{
	warn_backup=yes
	if test "yes" = "$warn_backup"; then
		echo "                                ==========="
		echo "                                  WARNING"
		echo "                                ==========="
		echo " You are installing an early development version of BasKet; please back up "
		echo " your baskets before using it if you have a previous version of BasKet "
		echo " installed."
	fi
}

##echo "  ((                      An error has been encountered                     ))"
##echo "  ((654321 987654321 987654321 98765432  23456789 123456789 123456789 123456))"
##echo" ((654321 987654321 987654321 98765432  23456789 123456789 123456789 123456))"

clear
echo ""
echo "    ========================================================================  "
echo "  ((                    BasKet Note Pads Quick Installer                    ))"
echo "    ========================================================================  "
echo ""
echo ""
echo "              Welcome to the BasKet Note Pads installer assistant."
echo "             It will configure, build and install BasKet Note Pads."
echo ""
echo ""
echo "                             What do you want to do?"
echo ""
echo "       You can specify the number of threads to use when building basket by"
echo "        passing in a number when running this script. (ie. ./installer 4 )"
echo ""
echo "  1/ Install BasKet Note Pads system wide (you will need root access)"
echo "  2/ Install BasKet Note Pads in your user folder"
#echo "  3/ Uninstall BasKet Note Pads (you should not: it is so fantastic ;-) )"
echo ""

echo -n "Your choice (default is 1): "
read -n 1 WHAT_TO_DO
echo ""

if [ "$WHAT_TO_DO" == "2" ]; then
  SYS_WIDE=0
  WHAT_TO_DO=2 # To transform it from string to int
else
#  if [ $WHAT_TO_DO == 3 ]; then
#    SYS_WIDE=1
#  else  # [ WHAT_TO_DO == 1 ] or default
    SYS_WIDE=1
    WHAT_TO_DO=1 # If user entered another number than those he should
#  fi
fi

if [ $SYS_WIDE == 0 ]; then
  prefix=$HOME/basket
else
  prefix=`kf5-config --prefix --expandvars`
fi

isDevel=alpha

clear
echo ""
echo "    ========================================================================  "
echo "  ((                    BasKet Note Pads Quick Installer                    ))"
echo "    ========================================================================  "
echo ""
echo ""
do_warning
echo ""
echo ""
echo "      If you do not know what they are, you can ignore errors or warnings."
echo ""
echo "                      The process can take several minutes."
echo "                  A beep will notify you when it will be done."
echo ""
echo "                              Press ENTER to start."
echo ""
echo ""

read -s -n 1 # read 1 character without echo, ended by ENTER

clear
echo ""
echo "    ========================================================================  "
echo "  ((                       Step 1 / 3 : Configuration                       ))"
echo "    ========================================================================  "
echo ""
echo ""

# No one is going to have a file named "build" here, right?
if test ! -d build ; then
	mkdir build
fi

cd build

if [ "_$isDevel" == "_" ]; then
  cmake .. $(prefix_cmd $prefix) || error
else
  cmake .. -DCMAKE_BUILD_TYPE=debugfull $(qtplugin_prefix_cmd `qtpaths --plugin-dir`) $(prefix_cmd $prefix) || error
fi

clear
echo ""
echo "    ========================================================================  "
echo "  ((                        Step 2 / 3 : Compilation                        ))"
echo "    ========================================================================  "
echo ""
echo ""

threads=${1:-2}
if [ $threads -le 0 ]; then
    threads=2
fi
echo "make -j${threads}"
make -j${threads} || error

clear
echo ""
echo "    ========================================================================  "
echo "  ((                        Step 3 / 3 : Installation                       ))"
echo "    ========================================================================  "
echo ""
echo ""
echo "                 The application has successfully been compiled."
echo ""
echo ""

echo $'\a' # Beep to avert user the configuration/compilation is ended and he must act to enter the password

if [ "$SYS_WIDE" == 0 ]; then
  # First make the bin folder to not copy basket file to bin file if bin/ doesn't exist
  make install
else
  echo "            Please now enter the password in order to install "
  echo "                        BasKet Note Pads on the system."
  echo ""
  echo 'Installing in "'$prefix'"...'
  if [ "$(type -p sudo)" ]; then
    sudo make install || exit 1
  else
    su -c "make install" || exit 1
  fi
  echo ""
  echo ""
fi

clear
echo ""
echo "    ========================================================================  "
echo "  ((                  End of BasKet Note Pads Installation                  ))"
echo "    ========================================================================  "
echo ""
echo ""
echo "                    Do you want to run BasKet Note Pads now?"
echo "       If an older version is currently running it will be exited before."
echo ""
echo "   Notice that you won't have to exit BasKet : it will be restored each time"
echo "                               you log in to KDE."
echo ""
echo ""

echo -n "Start or restart BasKet Note Pads? [y/n] (default is y): "
read -n 1 RESTART
if [ "$RESTART" != "y" ]; then # If pressed Enter, a new line was already print
  echo ""
fi

if [ "$RESTART" == "y" -o  "$RESTART" == "" ]; then
echo "Restart applicatoin"
  # Get the pid of the process basket, if currently running
  pid=`ps aux | ps aux | awk '$11 ~ /basket$/ { print $2 }'`
  if [ "$pid" == "" ]; then
    dummy=0
  else
    kill -s SIGTERM $pid
  fi

  if [ "$SYS_WIDE" == 1 ]; then
    echo "$prefix/bin/basket"
    $prefix/bin/basket
  else
    base=$HOME/basket
    if test -d $base ; then
      echo "${base}bin/basket"  
      ${base}bin/basket
    else
      echo "Could not find your kde installation."  
    fi
  fi
fi

clear
echo ""
echo "    ========================================================================  "
echo "  ((   Congratulation: BasKet Note Pads is now Installed onto your System   ))"
echo "    ========================================================================  "
echo ""
echo ""
echo "   In the future, if you want to uninstall BasKet Note Pads, you will have to"
if [ "$SYS_WIDE" == 0 ]; then
  echo "                enter the command make uninstall in this folder."
else
  echo "            enter the command su -c 'make uninstall' in this folder."
fi
echo ""
echo ""
