#
# FUNKTION: Menü für die Auswahl der unterstützten Sprachen
#
INC_NAME_LANGUAGE='inc_language_selection.txt'
LANGUAGE_DIRECTORY=${LANGUAGE_DIRECTORY:='EMPTY'}
LANGUAGE_SUPPORTED=${LANGUAGE_SUPPORTED:='EMPTY'}
LANGUAGE_INFO_FILE_PATH="./${LANGUAGE_DIRECTORY}/${LANGUAGE_SUPPORTED}"
# Info-Datei für die unterstützten Sprachen einbinden
if [ -f ${LANGUAGE_INFO_FILE_PATH} ]
then
source ${LANGUAGE_INFO_FILE_PATH}
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS LANGUAGE 1-1 \nSCRIPT-NAME: ${INC_NAME_LANGUAGE}\n"
  exit 1
  fi
else
echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS LANGUAGE 1-2 \nFILE: »${LANGUAGE_INFO_FILE_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_LANGUAGE}\n"
exit 1
fi
# unterstützte und eingetragene Sprachen ermitteln
# Variablen 1. Sprache
COUNTRY_SHORT_CODE_1=${COUNTRY_SHORT_CODE_1:='EMPTY'}
LANG_MENUE_VALUE_1=${LANG_MENUE_VALUE_1:='EMPTY'}
# Variablen 2. Sprache
COUNTRY_SHORT_CODE_2=${COUNTRY_SHORT_CODE_2:='EMPTY'}
LANG_MENUE_VALUE_2=${LANG_MENUE_VALUE_2:='EMPTY'}
# Variablen 3. Sprache
COUNTRY_SHORT_CODE_3=${COUNTRY_SHORT_CODE_3:='EMPTY'}
LANG_MENUE_VALUE_3=${LANG_MENUE_VALUE_3:='EMPTY'}
# Variablen 4. Sprache
COUNTRY_SHORT_CODE_4=${COUNTRY_SHORT_CODE_4:='EMPTY'}
LANG_MENUE_VALUE_4=${LANG_MENUE_VALUE_4:='EMPTY'}
# Variablen 5. Sprache
COUNTRY_SHORT_CODE_5=${COUNTRY_SHORT_CODE_5:='EMPTY'}
LANG_MENUE_VALUE_5=${LANG_MENUE_VALUE_5:='EMPTY'}

# Array mit den Länderkurzzeichen
declare -a COUNTRY_SHORT_CODE_ARRAY
# Array mit den Menüeinträgen
declare -a LANG_MENUE_VALUE_ARRAY
N=${N:+0}
STEP=${STEP:+1}
# 1. Eintrag der ARRAYS
if [ "${COUNTRY_SHORT_CODE_1}" != 'EMPTY' ] && [ "${LANG_MENUE_VALUE_1}" != 'EMPTY' ]
then
COUNTRY_SHORT_CODE_ARRAY[0]=${COUNTRY_SHORT_CODE_1}
LANG_MENUE_VALUE_ARRAY[0]=${LANG_MENUE_VALUE_1}
N=$[$N+$STEP]
else
echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS LANGUAGE 2 \nFILE: »${LANGUAGE_INFO_FILE_PATH}« IS DAMAGED \nSCRIPT-NAME: ${INC_NAME_LANGUAGE}\n"
exit 1
fi
# 2. Eintrag der ARRAYS
if [ ${N} -eq 1 ] && [ "${COUNTRY_SHORT_CODE_2}" != 'EMPTY' ] && [ "${LANG_MENUE_VALUE_2}" != 'EMPTY' ]
then
COUNTRY_SHORT_CODE_ARRAY[1]=${COUNTRY_SHORT_CODE_2}
LANG_MENUE_VALUE_ARRAY[1]=${LANG_MENUE_VALUE_2}
N=$[$N+$STEP]
fi
# 3. Eintrag der ARRAYS
if [ ${N} -eq 2 ] && [ "${COUNTRY_SHORT_CODE_3}" != 'EMPTY' ] && [ "${LANG_MENUE_VALUE_3}" != 'EMPTY' ]
then
COUNTRY_SHORT_CODE_ARRAY[2]=${COUNTRY_SHORT_CODE_3}
LANG_MENUE_VALUE_ARRAY[2]=${LANG_MENUE_VALUE_3}
N=$[$N+$STEP]
fi
# 4. Eintrag der ARRAYS
if [ ${N} -eq 3 ] && [ "${COUNTRY_SHORT_CODE_4}" != 'EMPTY' ] && [ "${LANG_MENUE_VALUE_4}" != 'EMPTY' ]
then
COUNTRY_SHORT_CODE_ARRAY[3]=${COUNTRY_SHORT_CODE_4}
LANG_MENUE_VALUE_ARRAY[3]=${LANG_MENUE_VALUE_4}
N=$[$N+$STEP]
fi
# 5. Eintrag der ARRAYS
if [ ${N} -eq 4 ] && [ "${COUNTRY_SHORT_CODE_5}" != 'EMPTY' ] && [ "${LANG_MENUE_VALUE_5}" != 'EMPTY' ]
then
COUNTRY_SHORT_CODE_ARRAY[4]=${COUNTRY_SHORT_CODE_5}
LANG_MENUE_VALUE_ARRAY[4]=${LANG_MENUE_VALUE_5}
N=$[$N+$STEP]
fi

# Default-Sprachdatei einbinden
LANGUAGE_NAME_PART_A=${LANGUAGE_NAME_PART_A:='EMPTY'}
LANGUAGE_NAME_PART_B=${COUNTRY_SHORT_CODE_ARRAY[0]}
LANGUAGE_NAME_PART_C=${LANGUAGE_NAME_PART_C:='EMPTY'}
LANGUAGE_DEFAULT_PATH="./${LANGUAGE_DIRECTORY}/${LANGUAGE_NAME_PART_A}${LANGUAGE_NAME_PART_B}${LANGUAGE_NAME_PART_C}"
if [ -f ${LANGUAGE_DEFAULT_PATH} ]
then
source ${LANGUAGE_DEFAULT_PATH}
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS LANGUAGE 3-1 \nSCRIPT-NAME: ${INC_NAME_LANGUAGE}\n"
  exit 1
  fi
else
echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS LANGUAGE 3-2 \nFILE: »${LANGUAGE_DEFAULT_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_LANGUAGE}\n"
exit 1
fi

# Menü mit Default-Sprache erstellen, falls mehr als eine Sprache zur Verfügung steht
COUNTRY_SHORT_CODE_ARRAY_COUNT=${#COUNTRY_SHORT_CODE_ARRAY[@]}
if [ ${COUNTRY_SHORT_CODE_ARRAY_COUNT} -gt 1 ]
then
N=${N:+0}
LANG_MENUE_HEAD_LINE=${LANG_MENUE_HEAD_LINE:='EMPTY'}
MENUE_VALUE_BREAK=${MENUE_VALUE_BREAK:='EMPTY'}
LANG_MENUE_FEED_LINE=${LANG_MENUE_FEED_LINE:='EMPTY'}
# xxx wird durch die Anzahl der Arrayelemente ersetzt -> inc_language_XX.txt -> LANG_MENUE_FEED_LINE
LANG_MENUE_FEED_LINE=${LANG_MENUE_FEED_LINE/xxx/$COUNTRY_SHORT_CODE_ARRAY_COUNT}
clear 2> /dev/null
echo -e "\n\t\t ${LANG_MENUE_HEAD_LINE} \n\n"
echo -e "  ${MENUE_VALUE_BREAK} ... (${N})"
  START=${START:+0}
  STOP=${STOP:+$COUNTRY_SHORT_CODE_ARRAY_COUNT}
  STEP=${STEP:+1}
  Q=${Q:+$START}
  N=$[$N+$STEP]
  while [ $Q -lt $STOP ]
  do
  echo -e "  ${LANG_MENUE_VALUE_ARRAY[$Q]} (${COUNTRY_SHORT_CODE_ARRAY[$Q]}) ... (${N})"
  Q=$[$Q+$STEP]
  N=$[$N+$STEP]
  done
echo ""
echo -n "${LANG_MENUE_FEED_LINE} "

# Benutzereingaben überprüfen
MENUE_PROGRAM_STOP=${MENUE_PROGRAM_STOP:='EMPTY'}
LANG_MENUE_USER_INFO=${LANG_MENUE_USER_INFO:='EMPTY'}
LANGUAGE_NAME_PART_B=${LANGUAGE_NAME_PART_B:+''}
  START=${START:+0}
  STOP=${STOP:+3}
  STEP=${STEP:+1}
  Q=${Q:+$START}
  while [ $Q -lt $STOP ]
  do
  Q=$[$Q+$STEP]
  read LANGUAGE_CURRENT
    if [ $(echo "${LANGUAGE_CURRENT}" | grep -c "^[1-5]$") -eq 1 ]
    then
      if [ ${LANGUAGE_CURRENT} -ge ${COUNTRY_SHORT_CODE_ARRAY_COUNT} ]
      then
      LANGUAGE_CURRENT=$[$COUNTRY_SHORT_CODE_ARRAY_COUNT-$STEP]
      else
      LANGUAGE_CURRENT=$[$LANGUAGE_CURRENT-$STEP]
      fi
      LANGUAGE_NAME_PART_B=${COUNTRY_SHORT_CODE_ARRAY[$LANGUAGE_CURRENT]}
    Q=${Q:+$STOP}
    elif [ $(echo "${LANGUAGE_CURRENT}" | grep -c "^[0]$") -eq 1 ]
    then
    echo -e "\n${MENUE_PROGRAM_STOP}\n"
    exit 1
    elif [ "${LANGUAGE_CURRENT}" = '' ]
    then
    LANGUAGE_NAME_PART_B=${COUNTRY_SHORT_CODE_ARRAY[0]}
    Q=${Q:+$STOP}
    else
    echo "${LANG_MENUE_USER_INFO}"
      if [ $Q -lt $STOP ]
      then
      echo -n "${LANG_MENUE_FEED_LINE} "
      fi
      if [ ${Q} -ge ${STOP} ]
      then
      echo -e "\n${MENUE_PROGRAM_STOP}\n"
      exit 1
      fi
    fi
  done

# die vom Benutzer gewählte Sprachendatei einbinden
LANGUAGE_NAME_PART_A=${LANGUAGE_NAME_PART_A:='EMPTY'}
LANGUAGE_NAME_PART_B=${LANGUAGE_NAME_PART_B:='EMPTY'}
LANGUAGE_NAME_PART_C=${LANGUAGE_NAME_PART_C:='EMPTY'}
LANGUAGE_CURRENT_PATH="./${LANGUAGE_DIRECTORY}/${LANGUAGE_NAME_PART_A}${LANGUAGE_NAME_PART_B}${LANGUAGE_NAME_PART_C}"
  if [ -f ${LANGUAGE_CURRENT_PATH} ]
  then
  source ${LANGUAGE_CURRENT_PATH}
    if [ $? -ge 1 ]
    then
    echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS LANGUAGE 4-1 \nSCRIPT-NAME: ${INC_NAME_LANGUAGE}\n"
    exit 1
    fi
  else
  echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS LANGUAGE 4-2 \nFILE: »${LANGUAGE_CURRENT_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_LANGUAGE}\n"
  exit 1
  fi
fi
