#
# FUNKTION: Datei für die Einfügung bzw. Löschung der Links zu den Kapiteln, Bildabschnitte.
#
INC_NAME_ADD_CHAPTER='inc_tools_add_del_chapter.txt'

# Variablen überprüfen
CHAPTER_LINK_COUNT_MAX=${CHAPTER_LINK_COUNT_MAX:=5}

# Einträge des Menü's
TOOLS_MENUE_CHAPTER_ADD_DEL_HEAD_LINE=${TOOLS_MENUE_CHAPTER_ADD_DEL_HEAD_LINE:='EMPTY'}
TOOLS_MENUE_VALUE_BREAK=${TOOLS_MENUE_VALUE_BREAK:='EMPTY'}
TOOLS_MENUE_PROGRAM_STOP=${TOOLS_MENUE_PROGRAM_STOP:='EMPTY'}
TOOLS_MENUE_CHAPTER_ADD_DEL_VALUE_1=${TOOLS_MENUE_CHAPTER_ADD_DEL_VALUE_1:='EMPTY'}
TOOLS_MENUE_CHAPTER_ADD_DEL_VALUE_2=${TOOLS_MENUE_CHAPTER_ADD_DEL_VALUE_2:='EMPTY'}
TOOLS_MENUE_CHAPTER_ADD_DEL_FEED_LINE=${TOOLS_MENUE_CHAPTER_ADD_DEL_FEED_LINE:='EMPTY'}
TOOLS_MENUE_CHAPTER_ADD_DEL_USER_INFO=${TOOLS_MENUE_CHAPTER_ADD_DEL_USER_INFO:='EMPTY'}
clear 2> /dev/null
echo -e "\n\t\t ${TOOLS_MENUE_CHAPTER_ADD_DEL_HEAD_LINE}\n\n"
echo -e "  ${TOOLS_MENUE_VALUE_BREAK} ... (0)"
echo -e "  ${TOOLS_MENUE_CHAPTER_ADD_DEL_VALUE_1} ... (1)"
echo -e "  ${TOOLS_MENUE_CHAPTER_ADD_DEL_VALUE_2} ... (2)"
echo ""
echo -n "${TOOLS_MENUE_CHAPTER_ADD_DEL_FEED_LINE} "

# Benutzereingaben entgegennehmen und überprüfen
START=${START:+0}
STOP=${STOP:+3}
STEP=${STEP:+1}
Q=${Q:+$START}
while [ $Q -lt $STOP ]
do
Q=$[$Q+$STEP]
read CHAPTER_MAIN_MENUE_VALUE
  if [ $(echo "${CHAPTER_MAIN_MENUE_VALUE}" | grep -c "^[1-2]$") -eq 1 ]
  then
  Q=${Q:+$STOP}
  elif [ $(echo "${CHAPTER_MAIN_MENUE_VALUE}" | grep -c "^[0]$") -eq 1 ] || [ "${CHAPTER_MAIN_MENUE_VALUE}" = '' ]
  then
  echo -e "\n${TOOLS_MENUE_PROGRAM_STOP}\n"
  exit 1
  else
    echo "${TOOLS_MENUE_CHAPTER_ADD_DEL_USER_INFO}"
    if [ $Q -lt $STOP ]
    then
    echo -n "${TOOLS_MENUE_CHAPTER_ADD_DEL_FEED_LINE} "
    fi
    if [ ${Q} -ge ${STOP} ]
    then
    echo -e "\n${TOOLS_MENUE_PROGRAM_STOP}\n"
    exit 1
    fi
  fi
done

# Kapitel-Links einfügen bzw. löschen
TEXT_FILE_DIRECTORY=${TEXT_FILE_DIRECTORY:='EMPTY'}
TEXT_FILE_NAME_CHAPTER_LIST_PART_A=${TEXT_FILE_NAME_CHAPTER_LIST_PART_A:='EMPTY'}
# LANGUAGE_NAME_PART_B -> inc_language_selection.txt
LANGUAGE_NAME_PART_B=${LANGUAGE_NAME_PART_B:='EMPTY'}
if [ "${LANGUAGE_NAME_PART_B}" = 'EMPTY' ]
then
echo -e "\nERROR-CODE: GALLERY TOOLS CHAPTER-ADD-DEL 1 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_ADD_CHAPTER}\n"
exit 1
fi
TEXT_FILE_NAME_CHAPTER_LIST_PART_B=${LANGUAGE_NAME_PART_B}
TEXT_FILE_NAME_CHAPTER_LIST_PART_C=${TEXT_FILE_NAME_CHAPTER_LIST_PART_C:='EMPTY'}
if [ "${TEXT_FILE_NAME_CHAPTER_LIST_PART_A}" = 'EMPTY' ] || [ "${TEXT_FILE_NAME_CHAPTER_LIST_PART_B}" = 'EMPTY' ] || [ "${TEXT_FILE_NAME_CHAPTER_LIST_PART_C}" = 'EMPTY' ]
then
echo -e "\nERROR-CODE: GALLERY TOOLS CHAPTER-ADD-DEL 2 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_ADD_CHAPTER}\n"
exit 1
fi
TEXT_FILE_NAME_CHAPTER_LIST_PATH="./${TEXT_FILE_DIRECTORY}/${TEXT_FILE_NAME_CHAPTER_LIST_PART_A}${TEXT_FILE_NAME_CHAPTER_LIST_PART_B}${TEXT_FILE_NAME_CHAPTER_LIST_PART_C}"
DIR_PROGRAMS=${DIR_PROGRAMS:='EMPTY'}
CREATE_HTML_FILE_INDEX=${CREATE_HTML_FILE_INDEX:='EMPTY'}
TOOLS_CHAPTER_MENUE_ADD_HEAD_LINE_A=${TOOLS_CHAPTER_MENUE_ADD_HEAD_LINE_A:='EMPTY'}
TOOLS_CHAPTER_MENUE_ADD_HEAD_LINE_B=${TOOLS_CHAPTER_MENUE_ADD_HEAD_LINE_B:='EMPTY'}
TOOLS_CHAPTER_MENUE_ADD_HEAD_LINE_C=${TOOLS_CHAPTER_MENUE_ADD_HEAD_LINE_C:='EMPTY'}
TOOLS_CHAPTER_MENUE_ADD_HEAD_LINE_D=${TOOLS_CHAPTER_MENUE_ADD_HEAD_LINE_D:='EMPTY'}
TOOLS_CHAPTER_MENUE_ADD_FEED_LINE_A=${TOOLS_CHAPTER_MENUE_ADD_FEED_LINE_A:='EMPTY'}
TOOLS_CHAPTER_MENUE_ADD_FEED_LINE_B=${TOOLS_CHAPTER_MENUE_ADD_FEED_LINE_B:='EMPTY'}
TOOLS_CHAPTER_MENUE_ADD_FEED_USER_INFO=${TOOLS_CHAPTER_MENUE_ADD_FEED_USER_INFO:='EMPTY'}
TOOLS_CHAPTER_MENUE_DEL_HEAD_LINE_A=${TOOLS_CHAPTER_MENUE_DEL_HEAD_LINE_A:='EMPTY'}
TOOLS_CHAPTER_MENUE_DEL_VALUE_1=${TOOLS_CHAPTER_MENUE_DEL_VALUE_1:='EMPTY'}
TOOLS_CHAPTER_MENUE_DEL_FEED_LINE_A=${TOOLS_CHAPTER_MENUE_DEL_FEED_LINE_A:='EMPTY'}
TOOLS_CHAPTER_MENUE_DEL_FEED_LINE_B=${TOOLS_CHAPTER_MENUE_DEL_FEED_LINE_B:='EMPTY'}
TOOLS_CHAPTER_MENUE_DEL_FEED_LINE_C=${TOOLS_CHAPTER_MENUE_DEL_FEED_LINE_C:='EMPTY'}
TOOLS_CHAPTER_MENUE_DEL_FEED_USER_INFO=${TOOLS_CHAPTER_MENUE_DEL_FEED_USER_INFO:='EMPTY'}
if [ ! -d "./${TEXT_FILE_DIRECTORY}" ]
then
mkdir ${TEXT_FILE_DIRECTORY}
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: GALLERY TOOLS CHAPTER-ADD-DEL 3 \nDIRECTORY: CAN NOT CREATE DIRECTORY »./${TEXT_FILE_DIRECTORY}« \nSCRIPT-NAME: ${INC_NAME_ADD_CHAPTER}\n"
  exit 1
  fi
fi
if [ ${CHAPTER_MAIN_MENUE_VALUE} -eq 1 ]
then
# Kapitel-Links einfügen
clear 2> /dev/null
echo -e "\n\t\t ${TOOLS_CHAPTER_MENUE_ADD_HEAD_LINE_A}\n\n"
echo -e "${TOOLS_CHAPTER_MENUE_ADD_HEAD_LINE_B}\n"
echo -e "\t${TOOLS_CHAPTER_MENUE_ADD_HEAD_LINE_C}\n\n"
echo -e "${TOOLS_CHAPTER_MENUE_ADD_HEAD_LINE_D}\n"
echo -e "  ${TOOLS_MENUE_VALUE_BREAK} ... (0)\n"
echo ""
echo -n "${TOOLS_CHAPTER_MENUE_ADD_FEED_LINE_A} "

  # Benutzereingaben entgegennehmen und überprüfen
  START=${START:+0}
  STOP=${STOP:+3}
  STEP=${STEP:+1}
  Q=${Q:+$START}
  N=${N:+$START}
  while [ $Q -lt $STOP ]
  do
  Q=$[$Q+$STEP]
  read CHAPTER_MENUE_ADD_VALUE
    if [ $(echo "${CHAPTER_MENUE_ADD_VALUE}" | grep -c "^[1-9][0-9]\{0,4\}:.\{1,25\}$") -eq 1 ]
    then
      N=$[$N+$STEP]
      if [ ${N} -le ${CHAPTER_LINK_COUNT_MAX} ]
      then
	# Kapitel-Links in eine Textdatei eintragen
	if [ ${N} -eq 1 ]
	then
	echo ${CHAPTER_MENUE_ADD_VALUE} > ${TEXT_FILE_NAME_CHAPTER_LIST_PATH}
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: GALLERY TOOLS CHAPTER-ADD-DEL 4-1 \nFILE: CAN NOT WRITE CHAPTER-NAME \nSCRIPT-NAME: ${INC_NAME_ADD_CHAPTER}\n"
	  exit 1
	  fi
	else
	echo ${CHAPTER_MENUE_ADD_VALUE} >> ${TEXT_FILE_NAME_CHAPTER_LIST_PATH}
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: GALLERY TOOLS CHAPTER-ADD-DEL 4-2 \nFILE: CAN NOT WRITE CHAPTER-NAME \nSCRIPT-NAME: ${INC_NAME_ADD_CHAPTER}\n"
	  exit 1
	  fi
	fi
	if [ ${N} -lt ${CHAPTER_LINK_COUNT_MAX} ]
	then
	echo -n "${TOOLS_CHAPTER_MENUE_ADD_FEED_LINE_A} "
	Q=$[$Q-$STEP]
	else
	echo -e "\n${TOOLS_CHAPTER_MENUE_ADD_FEED_LINE_B}\n"
	Q=${Q:+$STOP}
	fi
      else
      echo -e "\nERROR-CODE: GALLERY TOOLS CHAPTER-ADD-DEL 4-3 \nSCRIPT-NAME: ${INC_NAME_ADD_CHAPTER}\n"
      exit 1
      fi
    elif [ $(echo "${CHAPTER_MENUE_ADD_VALUE}" | grep -c "^[0]$") -eq 1 ] || [ "${CHAPTER_MENUE_ADD_VALUE}" = '' ]
    then
      if [ ${N} -ge 1 ]
      then
      echo -e "\n${TOOLS_CHAPTER_MENUE_ADD_FEED_LINE_B}\n"
      Q=${Q:+$STOP}
      else
      echo -e "\n${TOOLS_MENUE_PROGRAM_STOP}\n"
      exit 1
      fi
    else
      if [ ${Q} -ge ${STOP} ]
      then
      echo -e "\n${TOOLS_MENUE_PROGRAM_STOP}\n"
      exit 1
      fi
      echo "${TOOLS_CHAPTER_MENUE_ADD_FEED_USER_INFO}"
      echo -n "${TOOLS_CHAPTER_MENUE_ADD_FEED_LINE_A} "
    fi
  done
  source ${DIR_PROGRAMS}${CREATE_HTML_FILE_INDEX}
    if [ $? -ge 1 ]
    then
    echo -e "\nERROR-CODE: GALLERY TOOLS CHAPTER-ADD-DEL 4-4 \nSCRIPT-NAME: ${INC_NAME_ADD_CHAPTER}\n"
    exit 1
    fi
elif [ ${CHAPTER_MAIN_MENUE_VALUE} -eq 2 ]
then
# Kapitel-Links löschen
clear 2> /dev/null
echo -e "\n\t\t ${TOOLS_CHAPTER_MENUE_DEL_HEAD_LINE_A}\n\n"
echo -e "  ${TOOLS_MENUE_VALUE_BREAK} ... (0)"
echo -e "  ${TOOLS_CHAPTER_MENUE_DEL_VALUE_1} ... (1)\n"
echo ""
if [ ! -f ${TEXT_FILE_NAME_CHAPTER_LIST_PATH} ]
then
echo -e "${TOOLS_CHAPTER_MENUE_DEL_FEED_LINE_C}\n"
fi
echo -n "${TOOLS_CHAPTER_MENUE_DEL_FEED_LINE_A} "

# Benutzereingaben entgegennehmen und überprüfen
  START=${START:+0}
  STOP=${STOP:+3}
  STEP=${STEP:+1}
  Q=${Q:+$START}
  while [ $Q -lt $STOP ]
  do
  Q=$[$Q+$STEP]
  read CHAPTER_MENUE_DEL_VALUE
    if [ $(echo "${CHAPTER_MENUE_DEL_VALUE}" | grep -c "^[1]$") -eq 1 ]
    then
      if [ -f ${TEXT_FILE_NAME_CHAPTER_LIST_PATH} ]
      then
	rm -f ${TEXT_FILE_NAME_CHAPTER_LIST_PATH} 2> /dev/null
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: GALLERY TOOLS CHAPTER-ADD-DEL 5-1 \nFILE: CAN NOT TO DELETE FILE »${TEXT_FILE_NAME_CHAPTER_LIST_PATH}« \nSCRIPT-NAME: ${INC_NAME_ADD_CHAPTER}\n"
	  exit 1
	  fi
	source ${DIR_PROGRAMS}${CREATE_HTML_FILE_INDEX}
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: GALLERY TOOLS CHAPTER-ADD-DEL 5-2 \nSCRIPT-NAME: ${INC_NAME_ADD_CHAPTER}\n"
	  exit 1
	  fi
	echo -e "\n${TOOLS_CHAPTER_MENUE_DEL_FEED_LINE_B}\n"
      else
      echo -e "\n${TOOLS_CHAPTER_MENUE_DEL_FEED_LINE_C}\n"
      fi
      Q=${Q:+$STOP}
    elif [ $(echo "${CHAPTER_MENUE_DEL_VALUE}" | grep -c "^[0]$") -eq 1 ] || [ "${CHAPTER_MENUE_DEL_VALUE}" = '' ]
    then
      echo -e "\n${TOOLS_MENUE_PROGRAM_STOP}\n"
      exit 1
    else
      if [ ${Q} -ge ${STOP} ]
      then
      echo -e "\n${TOOLS_MENUE_PROGRAM_STOP}\n"
      exit 1
      fi
      echo "${TOOLS_CHAPTER_MENUE_DEL_FEED_USER_INFO}"
      echo -n "${TOOLS_CHAPTER_MENUE_DEL_FEED_LINE_A} "
    fi
  done
else
echo -e "\nERROR-CODE: GALLERY TOOLS CHAPTER-ADD-DEL 6 \nSCRIPT-NAME: ${INC_NAME_ADD_CHAPTER}\n"
exit 1
fi
