#
# FUNKTION: Datei für die Konvertierung LINUX/UNIX <-> DOS/WINDOWS der HTML-Dateien.
#
INC_NAME_CONVERT='inc_tools_convert_files.txt'

# Einträge des Menü's
TOOLS_MENUE_CONVERT_FILES_HEAD_LINE_A=${TOOLS_MENUE_CONVERT_FILES_HEAD_LINE_A:='EMPTY'}
TOOLS_MENUE_CONVERT_FILES_HEAD_LINE_B=${TOOLS_MENUE_CONVERT_FILES_HEAD_LINE_B:='EMPTY'}
TOOLS_MENUE_CONVERT_FILES_HEAD_LINE_C=${TOOLS_MENUE_CONVERT_FILES_HEAD_LINE_C:='EMPTY'}
TOOLS_MENUE_VALUE_BREAK=${TOOLS_MENUE_VALUE_BREAK:='EMPTY'}
TOOLS_MENUE_PROGRAM_STOP=${TOOLS_MENUE_PROGRAM_STOP:='EMPTY'}
TOOLS_MENUE_CONVERT_FILES_VALUE_1=${TOOLS_MENUE_CONVERT_FILES_VALUE_1:='EMPTY'}
TOOLS_MENUE_CONVERT_FILES_VALUE_2=${TOOLS_MENUE_CONVERT_FILES_VALUE_2:='EMPTY'}
TOOLS_MENUE_CONVERT_FILES_FEED_LINE_A=${TOOLS_MENUE_CONVERT_FILES_FEED_LINE_A:='EMPTY'}
TOOLS_MENUE_CONVERT_FILES_FEED_LINE_B=${TOOLS_MENUE_CONVERT_FILES_FEED_LINE_B:='EMPTY'}
TOOLS_MENUE_CONVERT_FILES_FEED_LINE_C=${TOOLS_MENUE_CONVERT_FILES_FEED_LINE_C:='EMPTY'}
TOOLS_MENUE_CONVERT_FILES_FEED_LINE_D=${TOOLS_MENUE_CONVERT_FILES_FEED_LINE_D:='EMPTY'}
TOOLS_MENUE_CONVERT_FILES_FEED_LINE_E=${TOOLS_MENUE_CONVERT_FILES_FEED_LINE_E:='EMPTY'}
TOOLS_MENUE_CONVERT_FILES_USER_INFO=${TOOLS_MENUE_CONVERT_FILES_USER_INFO:='EMPTY'}
CSS_DIRECTORY=${CSS_DIRECTORY:='EMPTY'}
EXTRA_DIRECTORY=${EXTRA_DIRECTORY:='EMPTY'}

clear 2> /dev/null
echo -e "\n\t\t${TOOLS_MENUE_CONVERT_FILES_HEAD_LINE_A}\n\n"
echo -e "  ${TOOLS_MENUE_VALUE_BREAK} ... (0)"
echo -e "  ${TOOLS_MENUE_CONVERT_FILES_VALUE_1} ... (1)"
echo -e "  ${TOOLS_MENUE_CONVERT_FILES_VALUE_2} ... (2)"
echo ""
echo -n "${TOOLS_MENUE_CONVERT_FILES_FEED_LINE_A} "

# Verzeichnisse überprüfen
CSS_DIRECTORY_PATH="../${CSS_DIRECTORY}"
if [ ! -d ${CSS_DIRECTORY_PATH} ]
then
echo -e "\nERROR-CODE: GALLERY TOOLS CONVERT-FILES 1-1 \nDIRECTORY: »${CSS_DIRECTORY_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_CONVERT}\n"
exit 1
fi
EXTRA_DIRECTORY_PATH="../${EXTRA_DIRECTORY}"
if [ ! -d ${EXTRA_DIRECTORY_PATH} ]
then
echo -e "\nERROR-CODE: GALLERY TOOLS CONVERT-FILES 1-2 \nDIRECTORY: »${EXTRA_DIRECTORY_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_CONVERT}\n"
exit 1
fi

# Gesamtzahl der HTML-Dateien ermitteln
HTML_FILES_NUMBERS=$(find ../ -maxdepth 1 -type f -iregex "^.*\(\.htm\|\.html\)$" | wc -l 2> /dev/null)
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: GALLERY TOOLS CONVERT-FILES 2 \nSCRIPT-NAME: ${INC_NAME_CONVERT}\n"
  exit 1
  fi

# 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 CONVERT_FILES_MENUE_VALUE
  if [ $(echo "${CONVERT_FILES_MENUE_VALUE}" | grep -c "^[1-2]$") -eq 1 ]
  then
    # Anzahl der HTML-Dateien prüfen
    if [ ${HTML_FILES_NUMBERS} -eq 0 ]
    then
    # keine HTML-Dateien gefunden
    clear 2> /dev/null
    echo -e "\n\t\t${TOOLS_MENUE_CONVERT_FILES_HEAD_LINE_A}\n\n"
    echo -e "  ${TOOLS_MENUE_CONVERT_FILES_FEED_LINE_D}\n"
    exit 1
    elif [ ${HTML_FILES_NUMBERS} -gt 99999 ]
    then
    # zu viele HTML-Dateien gefunden
    clear 2> /dev/null
    echo -e "\n\t\t${TOOLS_MENUE_CONVERT_FILES_HEAD_LINE_A}\n\n"
    echo -e "  ${TOOLS_MENUE_CONVERT_FILES_FEED_LINE_E}\n"
    exit 1
    fi 
    if [ ${CONVERT_FILES_MENUE_VALUE} -eq 1 ]
    then
    # Konvertierung ins DOS/WINDOWS-Format
    clear 2> /dev/null
    echo -e "\n\n\t\t ${TOOLS_MENUE_CONVERT_FILES_HEAD_LINE_B}\n"
    find ../ -maxdepth 1 -type f -iregex "^.*\(\.htm\|\.html\)$" -exec todos {} \; 2> /dev/null
      if [ $? -ge 1 ]
      then
      echo -e "\nERROR-CODE: GALLERY TOOLS CONVERT-FILES 3-1 \nFILE: CAN NOT CONVERT \nSCRIPT-NAME: ${INC_NAME_CONVERT}\n"
      exit 1
      fi
    # Dateien im Verzeichnis css ins DOS/WINDOWS-Format konvertieren
    find ${CSS_DIRECTORY_PATH} -maxdepth 1 -type f -iregex "^.*\(\.css\|\.txt\)$" -exec todos {} \; 2> /dev/null
      if [ $? -ge 1 ]
      then
      echo -e "\nERROR-CODE: GALLERY TOOLS CONVERT-FILES 3-2 \nDIRECTORY: »${CSS_DIRECTORY_PATH}« \nFILE: CAN NOT CONVERT \nSCRIPT-NAME: ${INC_NAME_CONVERT}\n"
      exit 1
      fi
    # Dateien im Verzeichnis extra ins DOS/WINDOWS-Format konvertieren
    find ${EXTRA_DIRECTORY_PATH} -maxdepth 2 -type f -iregex "^.*\(\.htm\|\.html\|\.css\|\.txt\)$" -exec todos {} \; 2> /dev/null
      if [ $? -ge 1 ]
      then
      echo -e "\nERROR-CODE: GALLERY TOOLS CONVERT-FILES 3-3 \nDIRECTORY: »${EXTRA_DIRECTORY_PATH}« \nFILE: CAN NOT CONVERT \nSCRIPT-NAME: ${INC_NAME_CONVERT}\n"
      exit 1
      fi
    # oberste Startdatei ins DOS/WINDOWS-Format konvertieren
    find ../../ -maxdepth 1 -type f -iregex "^.*\(index\.htm\|index\.html\)$" -exec todos {} \; 2> /dev/null
      if [ $? -ge 1 ]
      then
      echo -e "\nERROR-CODE: GALLERY TOOLS CONVERT-FILES 3-4 \nFILE: CAN NOT CONVERT \nSCRIPT-NAME: ${INC_NAME_CONVERT}\n"
      exit 1
      fi
      echo -e "  ${TOOLS_MENUE_CONVERT_FILES_FEED_LINE_B}\n"
    else
    # Konvertierung ins LINUX/UNIX-Format
    clear 2> /dev/null
    echo -e "\n\n\t\t ${TOOLS_MENUE_CONVERT_FILES_HEAD_LINE_C}\n"
    find ../ -maxdepth 1 -type f -iregex "^.*\(\.htm\|\.html\)$" -exec fromdos {} \; 2> /dev/null
      if [ $? -ge 1 ]
      then
      echo -e "\nERROR-CODE: GALLERY TOOLS CONVERT-FILES 4-1 \nFILE: CAN NOT CONVERT \nSCRIPT-NAME: ${INC_NAME_CONVERT}\n"
      exit 1
      fi
    # Dateien im Verzeichnis css ins LINUX/UNIX-Format konvertieren
    find ${CSS_DIRECTORY_PATH} -maxdepth 1 -type f -iregex "^.*\(\.css\|\.txt\)$" -exec fromdos {} \; 2> /dev/null
      if [ $? -ge 1 ]
      then
      echo -e "\nERROR-CODE: GALLERY TOOLS CONVERT-FILES 4-2 \nDIRECTORY: »${CSS_DIRECTORY_PATH}« \nFILE: CAN NOT CONVERT \nSCRIPT-NAME: ${INC_NAME_CONVERT}\n"
      exit 1
      fi
    # Dateien im Verzeichnis extra ins LINUX/UNIX-Format konvertieren
    find ${EXTRA_DIRECTORY_PATH} -maxdepth 2 -type f -iregex "^.*\(\.htm\|\.html\|\.css\|\.txt\)$" -exec fromdos {} \; 2> /dev/null
      if [ $? -ge 1 ]
      then
      echo -e "\nERROR-CODE: GALLERY TOOLS CONVERT-FILES 4-3 \nDIRECTORY: »${EXTRA_DIRECTORY_PATH}« \nFILE: CAN NOT CONVERT \nSCRIPT-NAME: ${INC_NAME_CONVERT}\n"
      exit 1
      fi
    # oberste Startdatei ins LINUX/UNIX-Format konvertieren
    find ../../ -maxdepth 1 -type f -iregex "^.*\(index\.htm\|index\.html\)$" -exec fromdos {} \; 2> /dev/null
      if [ $? -ge 1 ]
      then
      echo -e "\nERROR-CODE: GALLERY TOOLS CONVERT-FILES 4-4 \nFILE: CAN NOT CONVERT \nSCRIPT-NAME: ${INC_NAME_CONVERT}\n"
      exit 1
      fi
      echo -e "  ${TOOLS_MENUE_CONVERT_FILES_FEED_LINE_C}\n"
    fi
  Q=${Q:+$STOP}
  elif [ $(echo "${CONVERT_FILES_MENUE_VALUE}" | grep -c "^[0]$") -eq 1 ] || [ "${CONVERT_FILES_MENUE_VALUE}" = '' ]
  then
  echo -e "\n${TOOLS_MENUE_PROGRAM_STOP}\n"
  exit 1
  else
  echo "${TOOLS_MENUE_CONVERT_FILES_USER_INFO}"
    if [ $Q -lt $STOP ]
    then
    echo -n "${TOOLS_MENUE_CONVERT_FILES_FEED_LINE_A} "
    fi
    if [ ${Q} -ge ${STOP} ]
    then
    echo -e "\n${TOOLS_MENUE_PROGRAM_STOP}\n"
    exit 1
    fi
  fi
done
