# This file is part of ctkarch-sysconfig.
#
# ctkarch-sysconfig is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

menu() {
  local items=('chwrap'       "$_wrap"
               'chmorespace'  "$_morespace"
               'chquickblank' "$_quickblank"
               'chscrolling'  "$_scrolling"
               'chsyntaxhl'   "$_syntaxhl")
  local item="${items[0]}" err
  while
    item="$(dialogmenu --default-item "$item" 2>&1>&3)"
    [[ "$item" != '' ]]
  do
    err="$("$item" 2>&1>&3)" || mydialog --msgbox "${err}" 10 72
  done
  return 0
}

defaults() {
  # Fix a trap: nano cuts lines in two in config files, breaking things
  setwrap no || echo 'setwrap FAILED' >&2

  # Wasting a line of space at the top of the screen? Fix that.
  setmorespace yes || echo 'setmorespace FAILED' >&2

  # Hide the status bar immediately (^C displays it)
  setquickblank yes || echo 'setquickblank FAILED' >&2

  # Avoid jumps of a full screen with auto scrolling (^V and ^Y still do that)
  setscrolling smooth || echo 'setscrolling FAILED' >&2

  # Enable syntax highlighting
  setsyntaxhl yes || echo 'setsyntaxhl FAILED' >&2
}

# Set an option in a nanorc file
# parm: set|unset option_name nanorc_file
setoption() {
  [ "$1" = 'set' -o "$1" = 'unset' ] || { echo "invalid parameter '$1'" >&2; return 1; }

  if grep -qE "^[#[:space:]]*(un)?set[[:space:]]+${2}[[:space:]]*(#|\$)" "$3";then
  # a line for this option exists.
    if ! grep -qE "^[[:space:]]*(un)?set[[:space:]]+${2}[[:space:]]*(#|\$)" "$3";then
    # there's no such uncommented line.
      # uncomment the first commented line found for the option
      sed -Ei "0,/^[[:space:]]*#+[[:space:]]*(un)?set[[:space:]]+${2}[[:space:]]*(#|\$)/s/^[[:space:]]*#+[[:space:]]*((un)?set[[:space:]]+${2}[[:space:]]*(#|\$))/\1/" "$3" || return 1
    fi
    # change the value in the uncommented line(s)
    sed -Ei "s/^([[:space:]]*)(un)?set([[:space:]]+${2}[[:space:]]*(#|\$))/\1${1}\3/" "$3" || return 1
  else
    # no line for this option exists, create it with the passed value
    echo "${1} ${2}" >> "$3" || return 1
  fi
}

chwrap() {
  local items=('no'  "$_wrap_no"
               'yes' "$_wrap_yes")
  local choice _title="$_wrap"
  if choice="$(dialogmenu 2>&1>&3)" ;then
    setwrap "$choice" || return 1
    mydialog --msgbox "$(printf "$_explain_option" wrap wrap)" 10 72
  fi
}

# Change the 'nowrap' parameter
# parm: no|yes
setwrap() {
  case "$1" in
    'no')  setoption set nowrap "${systemroot}/etc/nanorc" || return 1 ;;
    'yes') setoption unset nowrap "${systemroot}/etc/nanorc" || return 1 ;;
    *)     echo "invalid parameter '$1'" >&2; return 1; ;;
  esac
  echo "cut long lines: $1"
}

chmorespace() {
  local items=('yes' "$_morespace_yes"
               'no'  "$_morespace_no")
  local choice _title="$_morespace"
  if choice="$(dialogmenu 2>&1>&3)" ;then
    setmorespace "$choice" || return 1
    mydialog --msgbox "$(printf "$_explain_option" morespace morespace)" 10 72
  fi
}

# Change the 'morespace' parameter
# parm: no|yes
setmorespace() {
  case "$1" in
    'yes') setoption set morespace "${systemroot}/etc/nanorc" || return 1 ;;
    'no')  setoption unset morespace "${systemroot}/etc/nanorc" || return 1 ;;
    *)     echo "invalid parameter '$1'" >&2; return 1; ;;
  esac
  echo "save a line on top: $1"
}

chquickblank() {
  local items=('yes' "$_quickblank_yes"
               'no'  "$_quickblank_no")
  local choice _title="$_quickblank"
  if choice="$(dialogmenu 2>&1>&3)" ;then
    setquickblank "$choice" || return 1
    mydialog --msgbox "$(printf "$_explain_option" quickblank quickblank)" 10 72
  fi
}

# Change the 'quickblank' parameter
# parm: no|yes
setquickblank() {
  case "$1" in
    'yes') setoption set quickblank "${systemroot}/etc/nanorc" || return 1 ;;
    'no')  setoption unset quickblank "${systemroot}/etc/nanorc" || return 1 ;;
    *)     echo "invalid parameter '$1'" >&2; return 1; ;;
  esac
  echo "hide status bar quickly: $1"
}

chscrolling() {
  local items=('smooth' "$_scroll_smooth"
               'screen' "$_scroll_screen")
  local choice _title="$_scrolling"
  if choice="$(dialogmenu 2>&1>&3)" ;then
    setscrolling "$choice" || return 1
    mydialog --msgbox "$(printf "$_explain_option" smooth smooth)" 10 72
  fi
}

# Change the scrolling unit ('smooth' parameter)
# parm: smooth|screen
setscrolling() {
  case "$1" in
    'smooth') setoption set smooth "${systemroot}/etc/nanorc" || return 1 ;;
    'screen') setoption unset smooth "${systemroot}/etc/nanorc" || return 1 ;;
    *)        echo "invalid parameter '$1'" >&2; return 1; ;;
  esac
  echo "scrolling style: $1"
}

chsyntaxhl() {
  local items=('yes' "$_syntaxhl_yes"
               'no'  "$_syntaxhl_no")
  local choice _title="$_syntaxhl"
  if choice="$(dialogmenu 2>&1>&3)" ;then
    setsyntaxhl "$choice" || return 1
    mydialog --msgbox "$_explain_syntaxhl" 10 72
  fi
}

# Enable or disable syntax highlighting
# parm: yes|no
setsyntaxhl() {
  case "$1" in
    'yes') sed -i 's|[#[:space:]]*\(include[[:space:]]*"/usr/share/nano/\)|\1|' "${systemroot}/etc/nanorc" || return 1 ;;
    'no')  sed -i 's|[[:space:]]*\(include[[:space:]]*"/usr/share/nano/\)|# \1|' "${systemroot}/etc/nanorc" || return 1 ;;
    *)     echo "invalid parameter '$1'" >&2; return 1; ;;
  esac
  echo "syntax highlighting: $1"
}
