#!/bin/bash

#set -x # Set to help debug

show_usage()
{
   i=0;        usage[$i]="Usage: $0 [-p<path>] [-fsad]"
   i=$((i+1)); usage[$i]="Where:"
   i=$((i+1)); usage[$i]="f \t= Use patched NCURSES version, thread-safe is default"
   i=$((i+1)); usage[$i]="p \t= Use specific path instead of current directory"
   i=$((i+1)); usage[$i]="s \t= Set system-wide environment instead of just current user"
   i=$((i+1)); usage[$i]="a \t= Force setup of /etc/apt/sources.list and restore when done (requires sudo)"
   i=$((i+1)); usage[$i]="d \t= Dry run. Just show commands."

   for i in "${usage[@]}"; do
      echo -e $i
   done
}

# process options
while getopts "h?p:fsad" opt; do
   case $opt in
      p  ) target_dir=$OPTARG ;;
      f  ) patch_ncurses=true ;;
      s  ) system=true ;;
      a  ) handle_apt=true ;;
      d  ) dry="echo" ;;
      h | \
      \? ) show_usage && exit 1
           exit 1 ;;
   esac
done
shift $(($OPTIND - 1))

# remove previous install, if any
$dry rm -rf ncurses*

# Learn the OS so that we can handle non-Ubuntu "kind of" gracefully
if [ "$(cat /etc/os-release | grep "^ID=" | cut -d'=' -f2 | sed "s/\"//g")" = "rhel" ]; then
   is_rhel=true
fi

# get up to date version
if [[ "$handle_apt" == "true" ]]; then
   $dry cp /etc/apt/sources.list /tmp
   $dry sed -i '/^# deb-src /s/^# //' /etc/apt/sources.list
   $dry apt-get update
fi

# install ncurses source code for this version
if [ -z "$is_rhel" ]; then
   $dry apt-get -q -y source ncurses
else
   $dry yum install ncurses-devel
fi

if [[ "$handle_apt" == "true" ]]; then
   $dry mv /tmp/sources.list /etc/apt/
   $dry apt-get update
fi

# check if sources obtained
if [ $? -ne 0 ] && [ -z "$dry" ]
then
   echo "+------------------------------------------------------------------------------+"
   echo "|                  Unable to get NCURSES source tree!                          |"
   echo "|       Check the available packages list in /etc/apt/sources.list.            |"
   echo "+------------------------------------------------------------------------------+"
   exit
fi

# patching the source files
$dry pushd ncurses-6.* > /dev/null
if [[ "$patch_ncurses" == "true" ]]; then
#   $dry wget https://proj.goldencode.com/downloads/ncurses/ncurses_curses_h_in_v6.4+20240113.patch
#   $dry wget https://proj.goldencode.com/downloads/ncurses/ncurses_lib_getch_c_v6.4+20240113.patch
   $dry patch include/curses.h.in      ../libncurses_curses_h_in_v6.4+20240113.patch
   $dry patch ncurses/base/lib_getch.c ../libncurses_lib_getch_c_v6.4+20240113.patch
fi

# configure and build object libraries
if [[ "$patch_ncurses" == "true" ]]; then
   $dry ./configure --with-termlib CFLAGS='-fPIC -O2' --with-abi-version=6 --disable-widec
else
   $dry ./configure --with-termlib CFLAGS='-fPIC -O2' --with-abi-version=6 --with-pthread --enable-pthreads-eintr --disable-widec
fi
$dry make

# make valid reference to updated ncurses.h
$dry cd include
$dry ln -s curses.h ncurses.h
$dry cd ..

if [[ "$patch_ncurses" == "true" ]]; then
   echo "+------------------------------------------------------------------------------+"
   echo "|                 Patched NCURSES libraries update completed.                  |"
else
   echo "+------------------------------------------------------------------------------+"
   echo "|               Thread-safe NCURSES libraries build completed.                 |"
fi

# Position minimal files if path is specified
if [[ ! -z "$target_dir" ]]; then
   $dry mkdir -p $target_dir
   $dry cp -r include $target_dir
   $dry cp -r lib $target_dir
   $dry cd $target_dir
fi

# setting up system variable if not yet defined to use custom libraries location
if [ -z "$NCURSES_FWD_STATIC" ]
then
   if [[ "$system" == "true" ]]; then
      rc_filename='/etc/profile.d/10-ncurses.sh'
      msg="|         The environment has been changed! Reboot to activate changes         |"
      update="export NCURSES_FWD_STATIC=\"$PWD\""
      rm $rc_filename
   else
      rc_filename=$HOME'/.bashrc'
      msg="|   The .bashrc has been changed! Please open new console to activate setup!   |"
      update="export NCURSES_FWD_STATIC=$PWD"
   fi
   if [[ -z "$dry" ]]; then
      echo '' >> $rc_filename

      if [[ "$patch_ncurses" == "true" ]]; then
         echo '# set up patched NCURSES root location' >> $rc_filename
      else
         echo '# set up thread-safe NCURSES root location' >> $rc_filename
      fi
      echo $update >> $rc_filename
      if [[ "$patch_ncurses" != "true" ]]; then
         echo 'export NCURSES_FWD_PTHREADS=true' >> $rc_filename
      fi
   fi
   echo "|   ---------------------------   WARNING ! --------------------------------   |"
   echo "$msg"
   echo "|   ---------------------------   WARNING ! --------------------------------   |"
else
   if [ -z "$NCURSES_FWD_PTHREADS" ]
   then
      if [[ "$patch_ncurses" != "true" ]]; then
         if [[ "$system" == "true" ]]; then
            rc_filename='/etc/profile.d/10-ncurses.sh'
            msg="|         The environment has been changed! Reboot to activate changes         |"
            rm $rc_filename
         else
            rc_filename=$HOME'/.bashrc'
            msg="|   The .bashrc has been changed! Please open new console to activate setup!   |"
         fi
         if [[ -z "$dry" ]]; then
            echo 'export NCURSES_FWD_PTHREADS=true' >> $rc_filename
         fi
         echo "|   ---------------------------   WARNING ! --------------------------------   |"
         echo "$msg"
         echo "|   ---------------------------   WARNING ! --------------------------------   |"
      fi
   fi
fi

echo "|              Please do FULL FWD rebuild to activate the changes.             |"
echo "+------------------------------------------------------------------------------+"

# back to the starting directory
$dry popd > /dev/null
