#!/bin/bash

#set -x # Set to help debug

show_usage()
{
   i=0;        usage[$i]="Usage: $0 [-p<path>] [-sd]"
   i=$((i+1)); usage[$i]="Where:"
   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]="d \t= Dry run. Just show commands."

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

# process options
while getopts "h?p:sd" opt; do
   case $opt in
      p  ) target_dir=$OPTARG ;;
      s  ) system=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*

# Special handling if in a docker container
[[ -e "/tmp/docker_build" ]] && in_docker=true

# 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 [[ "$in_docker" == "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 [[ "$in_docker" == "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
$dry wget https://proj.goldencode.com/downloads/ncurses/ncurses_curses_h_in_v6.1_20200708.patch
$dry wget https://proj.goldencode.com/downloads/ncurses/ncurses_lib_getch_c_v6.1_20200708.patch
$dry patch include/curses.h.in ncurses_curses_h_in_v6.1_20200708.patch
$dry patch ncurses/base/lib_getch.c ncurses_lib_getch_c_v6.1_20200708.patch

# configure and build object libraries
$dry ./configure --with-termlib CFLAGS='-fPIC -O2' --with-abi-version=6
$dry make

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

echo "+------------------------------------------------------------------------------+"
echo "|                 Patched NCURSES libraries update completed.                  |"

# 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/environment'
      msg="|         The environment has been changed! Reboot to activate changes         |"
      update="NCURSES_FWD_STATIC=\"$PWD\""
   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
      echo '# set up patched NCURSES root location' >> $rc_filename
      echo $update >> $rc_filename
   fi
   echo "|   ---------------------------   WARNING ! --------------------------------   |"
   echo "$msg"
   echo "|   ---------------------------   WARNING ! --------------------------------   |"
fi
echo "|              Please do FULL FWD rebuild to activate the changes.             |"
echo "+------------------------------------------------------------------------------+"

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