#!/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 " they contain libraries (X, QT, KDE...) sources needed to compile from sources."
  echo "                     For example, if libmng cannot be found,"
  echo " please verify if libmng AND libmng-devel packages are installed on your system."
  echo "      See the reported errors to know which librarie need to be installed."
  echo ""
  echo $'\a' # Beep to avert user of the error
  exit 1
}

clear
echo ""
echo "    ========================================================================  "
echo "  ((                         BasKet quick installer                         ))"
echo "    ========================================================================  "
echo ""
echo ""
echo "                    Welcome to the BasKet installer script."
echo "                 It will configure, compile and install BasKet."
echo ""
echo ""
echo "                            What do you want to do?"
echo ""
echo "  1/ Install BasKet system wide (you will need the root password)"
echo "  2/ Install BasKet in your home directory"
#echo "  3/ Uninstall BasKet"
echo ""

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

if [ $WHAT_TO_DO == 2 ]; then
  SYS_WIDE=0
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=`kde-config --localprefix --expandvars`
else
  prefix=`kde-config --prefix --expandvars`
fi

echo :$WHAT_TO_DO:
echo :$SYS_WIDE:
echo :$prefix:

# Uninstall of BasKet:
if [ $WHAT_TO_DO == 3 ]; then
  # Search installed basket:
  prefix=`kde-config --localprefix --expandvars` # First loacally
  if [ -f $prefix/bin/basket ]; then
    SYS_WIDE=0
  else
    prefix=`kde-config --prefix --expandvars`    # And then system wide
    if [ -f $prefix/bin/basket ]; then
      SYS_WIDE=1
    else
      echo "BasKet doesn't seem to be installed."
      exit 1
    fi
  fi
  # At this stage, SYS_WIDE and prefix are set and uninstall can begin:
  if [ SYS_WIDE == 1 ]; then
    su
    make uninstall || ./configure --prefix=$prefix && make uninstall
    logout
  else
    make uninstall || ./configure --prefix=$prefix && make uninstall
  fi
  exit 0
fi

clear
echo ""
echo "    ========================================================================  "
echo "  ((                         BasKet quick installer                         ))"
echo "    ========================================================================  "
echo ""
echo ""
echo ""
echo ""
echo "       If you don't 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 "                             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 ""

echo "./configure --prefix=$prefix"
./configure --prefix=$prefix || error

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

echo "make"
make || (echo "======== ERROR: Recompiling Without Fam Support ========" && echo "" > src/have_fam.h && make) || error

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

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

if [ `echo "$SYS_WIDE"` == "n" ]; then
  # First make the bin directory to not copy basket file to bon file if bin/ doesn't exist
  mkdir $prefix/bin
  make install
else
  echo "  Please now enter the root password in order to install BasKet on the system."
  echo ""
  su -c "make install" || exit 1
  echo ""
  echo ""
fi

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

echo -n "Start/restart BasKet ? [y/n] (y) "
read -n 1 RESTART
echo ""

if [ `echo "$RESTART"` != "n" ]; then
  # Get the pid of the process basket, if currently running
  pid=`ps aux | awk '$11 == "basket" { print $2 }'`
  if [ pid != "" ]; then
    kill -s SIGTERM $pid
  fi

  if [ `echo "$SYS_WIDE"` == "n" ]; then
    echo "$prefix/bin/basket"
    $prefix/bin/basket
  else
    echo "basket"
    basket
  fi
fi

clear
echo ""
echo "    ========================================================================  "
echo "  ((       Congratulation : BasKet is now installed onto your system.       ))"
echo "    ========================================================================  "
echo ""
echo ""
echo "     In the future, if you want to uninstall BasKet, you will have to enter"
if [ `echo "$SYS_WIDE"` == "n" ]; then
  echo "                  the command make uninstall in this directory."
else
  echo "                  the command su -c 'make uninstall' in this directory."
fi
echo ""
echo ""
