#
# FUNKTION: Datei zur Bearbeitung, Änderung der Galerie-Titels
#
INC_NAME_GALLERY_TITLE='inc_tools_change_gallery_title.txt'

# Variablen überprüfen
GALLERY_TITLE_MAX_CHAR=${GALLERY_TITLE_MAX_CHAR:=128}
TOOLS_MENUE_CHANGE_GALLERY_TITEL_HEAD_LINE_A=${TOOLS_MENUE_CHANGE_GALLERY_TITEL_HEAD_LINE_A:='EMPTY'}
TOOLS_MENUE_VALUE_BREAK=${TOOLS_MENUE_VALUE_BREAK:='EMPTY'}
TOOLS_MENUE_PROGRAM_STOP=${TOOLS_MENUE_PROGRAM_STOP:='EMPTY'}
PICTURE_GALLERY_NAME_DEFAULT=${PICTURE_GALLERY_NAME_DEFAULT:='Picture-Gallery'}
# LANGUAGE_NAME_PART_B -> inc_language_selection.txt
LANGUAGE_NAME_PART_B=${LANGUAGE_NAME_PART_B:='EMPTY'}
TOOLS_MENUE_CHANGE_GALLERY_TITEL_VALUE_1=${TOOLS_MENUE_CHANGE_GALLERY_TITEL_VALUE_1:='EMPTY'}
TOOLS_MENUE_CHANGE_GALLERY_TITEL_FEED_LINE_A=${TOOLS_MENUE_CHANGE_GALLERY_TITEL_FEED_LINE_A:='EMPTY'}
TOOLS_MENUE_CHANGE_GALLERY_TITEL_FEED_LINE_B=${TOOLS_MENUE_CHANGE_GALLERY_TITEL_FEED_LINE_B:='EMPTY'}
TOOLS_MENUE_CHANGE_GALLERY_TITEL_FEED_LINE_C=${TOOLS_MENUE_CHANGE_GALLERY_TITEL_FEED_LINE_C:='EMPTY'}
# xxx -> inc_language_xx.txt
TOOLS_MENUE_CHANGE_GALLERY_TITEL_FEED_LINE_C=${TOOLS_MENUE_CHANGE_GALLERY_TITEL_FEED_LINE_C/xxx/${GALLERY_TITLE_MAX_CHAR}}
TEXT_FILE_DIRECTORY=${TEXT_FILE_DIRECTORY:='EMPTY'}
if [ "${TEXT_FILE_DIRECTORY}" = 'EMPTY' ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 1 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
exit 1
fi

# Dateinamen mit dem Galerie-Titel zusammenstellen
PICTURE_GALLERY_FILE_NAME_PART_A=${PICTURE_GALLERY_FILE_NAME_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: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 2-1 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
exit 1
fi
PICTURE_GALLERY_FILE_NAME_PART_B=${LANGUAGE_NAME_PART_B}
PICTURE_GALLERY_FILE_NAME_PART_C=${PICTURE_GALLERY_FILE_NAME_PART_C:='EMPTY'}
if [ "${PICTURE_GALLERY_FILE_NAME_PART_A}" = 'EMPTY' ] || [ "${PICTURE_GALLERY_FILE_NAME_PART_B}" = 'EMPTY' ] || [ "${PICTURE_GALLERY_FILE_NAME_PART_C}" = 'EMPTY' ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 2-2 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
exit 1
fi
LANGUAGE_SHORT_CODE=$(echo ${LANGUAGE_NAME_PART_B} | tr [:lower:] [:upper:])
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 2-3 \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
  exit 1
  fi
  
# Galerietitel aus einer Textdatei auslesen, falls vorhanden
TEXT_FILE_DIRECTORY_PATH="./${TEXT_FILE_DIRECTORY}"
PICTURE_GALLERY_FILE_NAME_PATH="${TEXT_FILE_DIRECTORY_PATH}/${PICTURE_GALLERY_FILE_NAME_PART_A}${PICTURE_GALLERY_FILE_NAME_PART_B}${PICTURE_GALLERY_FILE_NAME_PART_C}"
if [ -f ${PICTURE_GALLERY_FILE_NAME_PATH} ]
then
PICTURE_GALLERY_NAME="$(head -n 1 ${PICTURE_GALLERY_FILE_NAME_PATH})"
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 3 \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
  exit 1
  fi
else
PICTURE_GALLERY_NAME=''
fi

# Menü erstellen
clear 2> /dev/null
echo -e "\n\t\t${TOOLS_MENUE_CHANGE_GALLERY_TITEL_HEAD_LINE_A}\n\n"
echo -e "  ${TOOLS_MENUE_VALUE_BREAK} ... (0)"
echo -e "  ${TOOLS_MENUE_CHANGE_GALLERY_TITEL_VALUE_1} ${PICTURE_GALLERY_NAME_DEFAULT} ... (1)"
if [ "${PICTURE_GALLERY_NAME}" != '' ]
then
# Menü erweitern, falls ein Galerietitel schon gespeichert wurde
echo -e "  ${PICTURE_GALLERY_NAME} ... (2)"
TOOLS_MENUE_CHANGE_GALLERY_TITEL_FEED_LINE_A=${TOOLS_MENUE_CHANGE_GALLERY_TITEL_FEED_LINE_A/1/2}
fi
echo ""
echo -n "${TOOLS_MENUE_CHANGE_GALLERY_TITEL_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 CHANGE_GALLERY_TITEL_MENUE_VALUE
  if [ $(echo "${CHANGE_GALLERY_TITEL_MENUE_VALUE}" | grep -c "^[0]$") -eq 1 ] || [ "${CHANGE_GALLERY_TITEL_MENUE_VALUE}" = '' ]
  then
    echo -e "\n${TOOLS_MENUE_PROGRAM_STOP}\n"
    exit 1
  elif [ $(echo "${CHANGE_GALLERY_TITEL_MENUE_VALUE}" | grep -c "^.\{1,${GALLERY_TITLE_MAX_CHAR}\}$") -eq 1 ]
  then
    clear 2> /dev/null
    echo -e "\n\t\t${TOOLS_MENUE_CHANGE_GALLERY_TITEL_HEAD_LINE_A}\n\n"
    if [ $(echo "${CHANGE_GALLERY_TITEL_MENUE_VALUE}" | grep -c "^[1]$") -eq 1 ]
    then
      PICTURE_GALLERY_NAME_CURRENT=${PICTURE_GALLERY_NAME_DEFAULT}
    elif [ $(echo "${CHANGE_GALLERY_TITEL_MENUE_VALUE}" | grep -c "^[2]$") -eq 1 ]
    then
      PICTURE_GALLERY_NAME_CURRENT=${PICTURE_GALLERY_NAME}
    else
      PICTURE_GALLERY_NAME_CURRENT=${CHANGE_GALLERY_TITEL_MENUE_VALUE}
      echo "${PICTURE_GALLERY_NAME_CURRENT}" > ${PICTURE_GALLERY_FILE_NAME_PATH}
	if [ $? -ge 1 ]
	then
	echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 4-1 \nFILE: CAN NOT WRITE GALLERY-TITLE \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
	exit 1
	fi
    fi
    Q=${Q:+$STOP}
  else
    echo "${TOOLS_MENUE_CHANGE_GALLERY_TITEL_FEED_LINE_C}"
    if [ $Q -lt $STOP ]
    then
    echo -n "${TOOLS_MENUE_CHANGE_GALLERY_TITEL_FEED_LINE_A} "
    fi
    if [ ${Q} -ge ${STOP} ]
    then
    echo -e "\n${TOOLS_MENUE_PROGRAM_STOP}\n"
    exit 1
    fi
  fi
done    
# Datei für die Erstellung der HTML-Datei einbinden
DIR_PROGRAMS=${DIR_PROGRAMS:='EMPTY'}
CREATE_HTML_FILE_INDEX=${CREATE_HTML_FILE_INDEX:='EMPTY'}
NO_SCRIPT_CREATE_HTML_FILE_INDEX=${NO_SCRIPT_CREATE_HTML_FILE_INDEX:=0}
if [ ${NO_SCRIPT_CREATE_HTML_FILE_INDEX} -eq 0 ]
then
  if [ "${DIR_PROGRAMS}" != 'EMPTY' ] || [ ${CREATE_HTML_FILE_INDEX} != 'EMPTY' ]
  then
    if [ -f ${DIR_PROGRAMS}${CREATE_HTML_FILE_INDEX} ]
    then
      source ${DIR_PROGRAMS}${CREATE_HTML_FILE_INDEX}
	if [ $? -ge 1 ]
	then
	echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 4-2 \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
	exit 1
	fi
    else
    echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 4-3 \nFILE: »${DIR_PROGRAMS}${CREATE_HTML_FILE_INDEX}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
    exit 1
    fi
  else
  echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 4-4 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
  exit 1
  fi
  # evtl. Sparchenumstellung der Galerie überprüfen
  GALLERY_HTML_PAGE_NAME_A=${GALLERY_HTML_PAGE_NAME_A:='gallery_a.htm'}
  GALLERY_HTML_PAGE_NAME_B_PART_A=${GALLERY_HTML_PAGE_NAME_B_PART_A:='gallery_b_'}
  GALLERY_HTML_PAGE_NAME_B_PART_B='1'
  GALLERY_HTML_PAGE_NAME_B_PART_C=${GALLERY_HTML_PAGE_NAME_B_PART_C:='.htm'}
  GALLERY_HTML_PAGE_NAME_B="${GALLERY_HTML_PAGE_NAME_B_PART_A}${GALLERY_HTML_PAGE_NAME_B_PART_B}${GALLERY_HTML_PAGE_NAME_B_PART_C}"
  GALLERY_FILE_NAME_START_INDEX=${GALLERY_FILE_NAME_START_INDEX:='index.html'}
  GALLERY_HTML_PAGE_NAME_A_PATH="../${GALLERY_HTML_PAGE_NAME_A}"
  GALLERY_HTML_PAGE_NAME_B_PATH="../${GALLERY_HTML_PAGE_NAME_B}"
  GALLERY_FILE_NAME_START_INDEX_PATH="../../${GALLERY_FILE_NAME_START_INDEX}"
  GALLERY_LANGUAGE_CHANGE=0
  if [ -f ${GALLERY_HTML_PAGE_NAME_A_PATH} ]
  then
    if [ $(cat ${GALLERY_HTML_PAGE_NAME_A_PATH} | grep -ci "^.*<title>.*-.*${LANGUAGE_SHORT_CODE}.*-.*</title>.*$") -eq 0 ]
    then
    GALLERY_LANGUAGE_CHANGE=${GALLERY_LANGUAGE_CHANGE:+1}
    fi
  elif [ -f ${GALLERY_HTML_PAGE_NAME_B_PATH} ]
  then
    if [ $(cat ${GALLERY_HTML_PAGE_NAME_B_PATH} | grep -ci "^.*<title>.*-.*${LANGUAGE_SHORT_CODE}.*-.*</title>.*$") -eq 0 ]
    then
    GALLERY_LANGUAGE_CHANGE=${GALLERY_LANGUAGE_CHANGE:+1}
    fi
  elif [ -f ${GALLERY_FILE_NAME_START_INDEX_PATH} ]
  then
    if [ $(cat ${GALLERY_FILE_NAME_START_INDEX_PATH} | grep -ci "^.*<title>.*-.*${LANGUAGE_SHORT_CODE}.*-.*</title>.*$") -eq 0 ]
    then
    GALLERY_LANGUAGE_CHANGE=${GALLERY_LANGUAGE_CHANGE:+1}
    fi
  else
  GALLERY_LANGUAGE_CHANGE=${GALLERY_LANGUAGE_CHANGE:+1}
  fi
  if [ ${GALLERY_LANGUAGE_CHANGE} -eq 1 ]
  then
  echo -e "${GALLERY_MESSAGE_PLEASE_WAIT}\n"
  fi
  # bei Sprachenumstellung: Datei für die Erstellung der HTML-Datei (Thumbnail-Bilder) einbinden
  if [ ${GALLERY_LANGUAGE_CHANGE} -eq 1 ]
  then
    CREATE_HTML_FILE_A=${CREATE_HTML_FILE_A:='EMPTY'}
    if [ ${CREATE_HTML_FILE_A} != 'EMPTY' ]
    then
      if [ -f ${DIR_PROGRAMS}${CREATE_HTML_FILE_A} ]
      then
	source ${DIR_PROGRAMS}${CREATE_HTML_FILE_A}
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 5-1 \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
	  exit 1
	  fi
      else
      echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 5-2 \nFILE: »${DIR_PROGRAMS}${CREATE_HTML_FILE_A}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
      exit 1
      fi
    else
    echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 5-3 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
    exit 1
    fi
  fi
  # bei Sprachenumstellung: Datei für die Erstellung der HTML-Dateien (Galerie-Bilder, Hauptbilder) einbinden
  if [ ${GALLERY_LANGUAGE_CHANGE} -eq 1 ]
  then
    CREATE_HTML_FILES_B=${CREATE_HTML_FILES_B:='EMPTY'}
    if [ ${CREATE_HTML_FILES_B} != 'EMPTY' ]
    then
      if [ -f ${DIR_PROGRAMS}${CREATE_HTML_FILES_B} ]
      then
	source ${DIR_PROGRAMS}${CREATE_HTML_FILES_B}
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 5-4 \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
	  exit 1
	  fi
      else
      echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 5-5 \nFILE: »${DIR_PROGRAMS}${CREATE_HTML_FILES_B}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
      exit 1
      fi
    else
    echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 5-6 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
    exit 1
    fi
  fi
  # bei Sprachenumstellung: Datei für die Erstellung der HTML-Datei für Startdatei
  if [ ${GALLERY_LANGUAGE_CHANGE} -eq 1 ]
  then
    CREATE_HTML_FILE_START_INDEX=${CREATE_HTML_FILE_START_INDEX:='EMPTY'}
    if [ ${CREATE_HTML_FILE_START_INDEX} != 'EMPTY' ]
    then
      if [ -f ${DIR_PROGRAMS}${CREATE_HTML_FILE_START_INDEX} ]
      then
	source ${DIR_PROGRAMS}${CREATE_HTML_FILE_START_INDEX}
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 5-7 \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
	  exit 1
	  fi
      else
      echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 5-8 \nFILE: »${DIR_PROGRAMS}${CREATE_HTML_FILE_START_INDEX}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
      exit 1
      fi
    else
    echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CHANGE-GALLERY-TITLE 5-9 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_GALLERY_TITLE}\n"
    exit 1
    fi
  fi
  if [ ${GALLERY_LANGUAGE_CHANGE} -eq 1 ]
  then
  echo -e "${TOOLS_MENUE_CHANGE_GALLERY_TITEL_FEED_LINE_D}\n"
  else
  echo -e "  ${TOOLS_MENUE_CHANGE_GALLERY_TITEL_FEED_LINE_B}\n"
  fi
fi

