#
# FUNKTION: Erstellung der HTML-Dateien für die Anzeige der Galerie-Bilder
#
INC_NAME_FILE_B='inc_create_html_files_b.txt'

# Variablen überprüfen
PICTURE_NUMBERS=${PICTURE_NUMBERS:=1}
PICTURE_DIRECTORY_PATH=${PICTURE_DIRECTORY_PATH:='EMPTY'}
TEXT_FILE_DIRECTORY=${TEXT_FILE_DIRECTORY:='EMPTY'}
CSS_DIRECTORY=${CSS_DIRECTORY:='EMPTY'}
PICTURE_GALLERY_NAME_TITLE_TAG=${PICTURE_GALLERY_NAME_TITLE_TAG:='Kobold'}
GALLERY_CHARSET=${GALLERY_CHARSET:='utf-8'}
PICTURE_TITLE_MAX_CHAR=${PICTURE_TITLE_MAX_CHAR:=60}
PICTURE_TOOL_TIP=${PICTURE_TOOL_TIP:='Picture'}
# LANGUAGE_NAME_PART_B -> inc_language_selection.txt
LANGUAGE_NAME_PART_B=${LANGUAGE_NAME_PART_B:='EMPTY'}
PICTURE_TARGET_WIDTH=${PICTURE_TARGET_WIDTH:=800}
PICTURE_TARGET_HEIGHT=${PICTURE_TARGET_HEIGHT:=600}
PICTURE_ORIGINAL_WIDTH_MAX=${PICTURE_WIDTH_MAX:=8000}
PICTURE_ORIGINAL_HEIGHT_MAX=${PICTURE_HEIGHT_MAX:=6000}
if [ "${PICTURE_DIRECTORY_PATH}" = 'EMPTY' ] || [ "${TEXT_FILE_DIRECTORY}" = 'EMPTY' ] || [ "${CSS_DIRECTORY}" = 'EMPTY' ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CREATE-HTML-FILES-B 1-1 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FILE_B}\n"
exit 1
fi
if [ "${LANGUAGE_NAME_PART_B}" = 'EMPTY' ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CREATE-HTML-FILES-B 1-2 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FILE_B}\n"
exit 1
fi
GALLERY_CHARSET=$(echo ${GALLERY_CHARSET} | tr [:upper:] [:lower:])
 if [ $? -ge 1 ]
 then
 echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CREATE-HTML-FILES-B 1-3 \nSCRIPT-NAME: ${INC_NAME_FILE_B}\n"
 exit 1
 fi
if [ $(echo "${PICTURE_TARGET_WIDTH}" | grep -c "^[1-9][0-9]\{2,3\}$") -ne 1 ] || [ $(echo "${PICTURE_TARGET_HEIGHT}" | grep -c "^[1-9][0-9]\{2,3\}$") -ne 1 ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CREATE-HTML-FILES-B 1-4 \nFILE: CONFIGURATION-FILE IS DAMAGED \nSCRIPT-NAME: ${INC_NAME_FILE_B}\n"
exit 1
fi
if [ ${PICTURE_TARGET_WIDTH} -gt ${PICTURE_ORIGINAL_WIDTH_MAX} ] || [ ${PICTURE_TARGET_HEIGHT} -gt ${PICTURE_ORIGINAL_HEIGHT_MAX} ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CREATE-HTML-FILES-B 1-5 \nFILE: CONFIGURATION-FILE IS DAMAGED \nSCRIPT-NAME: ${INC_NAME_FILE_B}\n"
exit 1
fi

# Dateinamen mit den Bilder-Titeln zusammenstellen; Länder-Kurzzeichen
PICTURE_TITLE_FILE_NAME_PART_A=${PICTURE_TITLE_FILE_NAME_PART_A:='EMPTY'}
# LANGUAGE_NAME_PART_B -> inc_language_selection.txt
PICTURE_TITLE_FILE_NAME_PART_B=${LANGUAGE_NAME_PART_B}
PICTURE_TITLE_FILE_NAME_PART_C=${PICTURE_TITLE_FILE_NAME_PART_C:='EMPTY'}
if [ "${PICTURE_TITLE_FILE_NAME_PART_A}" = 'EMPTY' ] || [ "${PICTURE_TITLE_FILE_NAME_PART_B}" = 'EMPTY' ] || [ "${PICTURE_TITLE_FILE_NAME_PART_C}" = 'EMPTY' ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CREATE-HTML-FILES-B 2-1 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FILE_B}\n"
exit 1
fi
LANGUAGE_SHORT_CODE=$(echo ${PICTURE_TITLE_FILE_NAME_PART_B} | tr [:lower:] [:upper:])
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CREATE-HTML-FILES-B 2-2 \nSCRIPT-NAME: ${INC_NAME_FILE_B}\n"
  exit 1
  fi

# Verzeichnisse überprüfen
TEXT_FILE_DIRECTORY_PATH="./${TEXT_FILE_DIRECTORY}"
if [ ! -d ${TEXT_FILE_DIRECTORY_PATH} ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CREATE-HTML-FILES-B 3-1 \nDIRECTORY: »${TEXT_FILE_DIRECTORY_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FILE_B}\n"
exit 1
fi
CSS_DIRECTORY_PATH="../${CSS_DIRECTORY}"
if [ ! -d ${CSS_DIRECTORY_PATH} ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CREATE-HTML-FILES-B 3-2 \nDIRECTORY: »${CSS_DIRECTORY_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FILE_B}\n"
exit 1
fi
if [ ! -d ${PICTURE_DIRECTORY_PATH} ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CREATE-HTML-FILES-B 3-3 \nDIRECTORY: »${PICTURE_DIRECTORY_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FILE_B}\n"
exit 1
fi

# Dateipfad der Datei mit den Bildtiteln zusammenstellen
PICTURE_TITLE_FILE_NAME_PATH="${TEXT_FILE_DIRECTORY_PATH}/${PICTURE_TITLE_FILE_NAME_PART_A}${PICTURE_TITLE_FILE_NAME_PART_B}${PICTURE_TITLE_FILE_NAME_PART_C}"

# vorhandene alte HTML-Dateien für die Anzeige der Hauptbilder löschen
GALLERY_FILE_NAME_INDEX=${GALLERY_FILE_NAME_INDEX:='index.htm'}
GALLERY_HTML_PAGE_NAME_A=${GALLERY_HTML_PAGE_NAME_A:='gallery_a.htm'}
HTML_FILE_COUNT=$(find ../ -maxdepth 1 -type f -iregex "^.*\(\.htm\|\.html\)$" | wc -l 2> /dev/null)
if [ ${HTML_FILE_COUNT} -gt 0 ]
then
find ../ -maxdepth 1 -type f -iregex "^.*\(\.htm\|\.html\)$" -not -iregex "^.*\(${GALLERY_FILE_NAME_INDEX}\|${GALLERY_HTML_PAGE_NAME_A}\)$" -exec rm -f {} \; 2> /dev/null
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: CREATE-GALLERY AND TOOLS CREATE-HTML-FILES-B 4 \nSCRIPT-NAME: ${INC_NAME_FILE_B}\n"
  exit 1
  fi
fi

### BEGINN: Erstellung der HTML-Dateien für die Galerie-Bilder (Hauptbilder)
GALLERY_FILE_NAME_STYLESHEET=${GALLERY_FILE_NAME_STYLESHEET:='EMPTY'}
GALLERY_FILE_NAME_STYLESHEET_PATH="../${CSS_DIRECTORY}/${GALLERY_FILE_NAME_STYLESHEET}"
GALLERY_FILE_NAME_STYLESHEET_HTML_PATH="./${CSS_DIRECTORY}/${GALLERY_FILE_NAME_STYLESHEET}"
if [ ! -f ${GALLERY_FILE_NAME_STYLESHEET_PATH} ]
then
echo -e "\nERROR-CODE: GALLERY CREATE-HTML-FILES-B 5-1 \nFILE: »${GALLERY_FILE_NAME_STYLESHEET_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FILE_B}\n"
exit 1
fi
GALLERY_HTML_PAGE_NAME_B_PART_A=${GALLERY_HTML_PAGE_NAME_B_PART_A:='gallery_b_'}
# GALLERY_HTML_PAGE_NAME_B_PART_B wird vom Shellskript gesetzt
GALLERY_HTML_PAGE_NAME_B_PART_C=${GALLERY_HTML_PAGE_NAME_B_PART_C:='.htm'}
Q=${Q:+0}
for l in $(find ${PICTURE_DIRECTORY_PATH} -maxdepth 1 -type f -iregex "^.*\(\.jpg\|\.jpeg\|\.png\)$" | sort)
do
Q=$[$Q+1]
GALLERY_FILE_NAME_CURRENT_B="${GALLERY_HTML_PAGE_NAME_B_PART_A}${Q}${GALLERY_HTML_PAGE_NAME_B_PART_C}"
GALLERY_FILE_NAME_CURRENT_B_PATH="../${GALLERY_FILE_NAME_CURRENT_B}"
GALLERY_PICTURE_CURRENT_B_PATH=${l#\.}
PICTURE_TITLE_FILE_NAME_PATH=${PICTURE_TITLE_FILE_NAME_PATH:='EMPTY'}
# -f 2-9 .. falls im Bildtitel ein Doppelpunkt (:) auftaucht
  if [ -f ${PICTURE_TITLE_FILE_NAME_PATH} ]
  then
  PICTURE_TITLE_VALUE_CURRENT_B=$(grep "^${Q}:" ${PICTURE_TITLE_FILE_NAME_PATH} | cut -d : -f 2-9)
  PICTURE_TITLE_VALUE_CURRENT_B=${PICTURE_TITLE_VALUE_CURRENT_B:0:$PICTURE_TITLE_MAX_CHAR}
    if [ "${PICTURE_TITLE_VALUE_CURRENT_B}" != '' ]
    then
    # Array mit den zu ersetzenden Zeichen durch den entsprechenden HTML-Code -> inc_function.txt
      START=${START:+0}
      STOP=${STOP:+$CHAR_REPLACE_ARRAY_COUNT}
      STEP=${STEP:+1}
      N=${N:+$START}
      while [ ${N} -lt $STOP ]
      do
      PICTURE_TITLE_VALUE_CURRENT_B=${PICTURE_TITLE_VALUE_CURRENT_B:+${PICTURE_TITLE_VALUE_CURRENT_B//${TEXT_CHAR_ARRAY[$N]}/${CHAR_REPLACE_ARRAY[$N]}}}
      N=$[$N+$STEP]
      done
    else
    PICTURE_TITLE_VALUE_CURRENT_B='&nbsp;'
    fi
  else
  PICTURE_TITLE_VALUE_CURRENT_B='&nbsp;'
  fi
  
  # Javaskript für die letzte HTML-Datei
  JAVASCRIPT_LAST_HTML_FILE_HEAD=''
  JAVASCRIPT_LAST_HTML_FILE_BODY_A=''
  JAVASCRIPT_LAST_HTML_FILE_BODY_B=''
  if [ ${Q} -eq ${PICTURE_NUMBERS} ]
  then
  JAVASCRIPT_TEXT_END=${JAVASCRIPT_TEXT_END:='EMPTY'}
  POSITION_END_A=$[$PICTURE_TARGET_WIDTH/2]
  POSITION_END_B=$[$POSITION_END_A-30]
  JAVASCRIPT_LAST_HTML_FILE_HEAD="var pos_x=-200;
var pos_x_a=-200;
var pos_y=180;
var timer;
var width_x=${POSITION_END_B};

function end_txt_a() {
if(document.getElementById) {
   pos_x++;
   eval(\"document.getElementById(\" + \"\\\"end\\\"\" + \").style.left=\" + \"\\\"\" + pos_x + \"px\\\"\");
   eval(\"document.getElementById(\" + \"\\\"end\\\"\" + \").style.top=\" + \"\\\"\" + pos_y + \"px\\\"\");
   clearTimeout(timer);
   if(pos_x > width_x) {
   timer=setTimeout(\"end_txt_b()\",10000);
   }
   else
   {
   timer=setTimeout(\"end_txt_a()\",50);
   }
 }
}

function end_txt_b() {
if(document.getElementById) {
   pos_x--;
   eval(\"document.getElementById(\" + \"\\\"end\\\"\" + \").style.left=\" + \"\\\"\" + pos_x + \"px\\\"\");
   eval(\"document.getElementById(\" + \"\\\"end\\\"\" + \").style.top=\" + \"\\\"\" + pos_y + \"px\\\"\");
   clearTimeout(timer);
   if(pos_x > pos_x_a) {timer=setTimeout(\"end_txt_b()\",50);}
 }
}
"
  JAVASCRIPT_LAST_HTML_FILE_BODY_A=" onLoad=\"setTimeout('end_txt_a()',5000);\""
  JAVASCRIPT_LAST_HTML_FILE_BODY_B="\n<div id=\"end\" class=\"div_end_b\">${JAVASCRIPT_TEXT_END}</div>"
  fi
# HTML-Datei erstellen
echo -e "<!DOCTYPE html>
<html>
<head>
<title>${PICTURE_GALLERY_NAME_TITLE_TAG} - ${LANGUAGE_SHORT_CODE} - ${GALLERY_FILE_NAME_CURRENT_B}</title>
<meta charset=\"${GALLERY_CHARSET}\">
<script type=\"text/javascript\">
<!-- hide
${JAVASCRIPT_LAST_HTML_FILE_HEAD}
function stoperror() {
return true
}
window.onerror=stoperror;
//-->
</script>
<link rel=\"stylesheet\" type=\"text/css\" href=\"${GALLERY_FILE_NAME_STYLESHEET_HTML_PATH}\">
</head>
<body id=\"body_b\"${JAVASCRIPT_LAST_HTML_FILE_BODY_A}>
<div id=\"div_top_b\">
  <table id="table_b">
    <tr>
      <td id=\"text_b_1\">${PICTURE_TITLE_VALUE_CURRENT_B}</td>
      <td id=\"text_b_2\">${Q}/${PICTURE_NUMBERS}</td>
      <td id=\"text_b_3\">&nbsp;</td>
    </tr>
  </table>
</div>
<div id=\"div_main_b\">
  <img src=\"${GALLERY_PICTURE_CURRENT_B_PATH}\" width=\"${PICTURE_TARGET_WIDTH}\" height=\"${PICTURE_TARGET_HEIGHT}\" title=\"${PICTURE_TOOL_TIP}:${Q}/${PICTURE_NUMBERS}\" alt=\"${PICTURE_TOOL_TIP}:${Q}/${PICTURE_NUMBERS}\">
</div>${JAVASCRIPT_LAST_HTML_FILE_BODY_B}
</body>
</html>
" > ${GALLERY_FILE_NAME_CURRENT_B_PATH}
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: GALLERY CREATE-HTML-FILES-B 5-2 \nFILE: CAN NOT WRITE FILE »${GALLERY_FILE_NAME_CURRENT_B_PATH}« \nSCRIPT-NAME: ${INC_NAME_FILE_B}\n"
  exit 1
  fi

# Textkodierung prüfen und evtl. ändern
change_text_coding ${GALLERY_FILE_NAME_CURRENT_B_PATH} ${GALLERY_CHARSET}
done
### ENDE: Erstellung der HTML-Dateien für die Galerie-Bilder (Hauptbilder)
