#
# FUNKTION: Erstellung einer HTML-Datei für die Anzeige der Thumbnail-Bilder
#
INC_NAME_FILE_A='inc_create_html_file_a.txt'

# Variablen überprüfen
PICTURE_NUMBERS=${PICTURE_NUMBERS:=1}
CSS_DIRECTORY=${CSS_DIRECTORY:='EMPTY'}
THUMBNAILS_DIRECTORY_PATH=${THUMBNAILS_DIRECTORY_PATH:='EMPTY'}
GALLERY_FILE_NAME_STYLESHEET=${GALLERY_FILE_NAME_STYLESHEET:='EMPTY'}
PICTURE_GALLERY_NAME_TITLE_TAG=${PICTURE_GALLERY_NAME_TITLE_TAG:='Kobold'}
GALLERY_CHARSET=${GALLERY_CHARSET:='utf-8'}
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 wird vom Shellskript gesetzt
GALLERY_HTML_PAGE_NAME_B_PART_C=${GALLERY_HTML_PAGE_NAME_B_PART_C:='.htm'}
PICTURE_TOOL_TIP=${PICTURE_TOOL_TIP:='Picture'}
THUMBNAIL_TARGET_WIDTH=${THUMBNAIL_TARGET_WIDTH:=100}
THUMBNAIL_TARGET_HEIGHT=${THUMBNAIL_TARGET_HEIGHT:=75}
# LANGUAGE_NAME_PART_B -> inc_language_selection.txt
LANGUAGE_NAME_PART_B=${LANGUAGE_NAME_PART_B:='EMPTY'}
if [ "${CSS_DIRECTORY}" = 'EMPTY' ] || [ "${GALLERY_FILE_NAME_STYLESHEET}" = 'EMPTY' ] || [ "${THUMBNAILS_DIRECTORY_PATH}" = 'EMPTY' ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY CREATE-HTML-FILES-A 1-1 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FILE_A}\n"
exit 1
fi
if [ "${LANGUAGE_NAME_PART_B}" = 'EMPTY' ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY CREATE-HTML-FILES-A 1-2 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FILE_A}\n"
exit 1
fi
LANGUAGE_NAME_PART_B=$(echo ${LANGUAGE_NAME_PART_B} | tr [:upper:] [:lower:])
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: CREATE-GALLERY CREATE-HTML-FILES-A 1-2 \nSCRIPT-NAME: ${INC_NAME_FILE_A}\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 CREATE-HTML-FILES-A 1-3 \nSCRIPT-NAME: ${INC_NAME_FILE_A}\n"
  exit 1
  fi
if [ $(echo "${THUMBNAIL_TARGET_WIDTH}" | grep -c "^[1-9][0-9]\{1,2\}$") -ne 1 ] || [ $(echo "${THUMBNAIL_TARGET_HEIGHT}" | grep -c "^[1-9][0-9]\{1,2\}$") -ne 1 ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY CREATE-HTML-FILES-A 1-4 \nFILE: CONFIGURATION-FILE IS DAMAGED \nSCRIPT-NAME: ${INC_NAME_FILE_A}\n"
exit 1
fi
GALLERY_CHARSET=$(echo ${GALLERY_CHARSET} | tr [:upper:] [:lower:])
 if [ $? -ge 1 ]
 then
 echo -e "\nERROR-CODE: CREATE-GALLERY CREATE-HTML-FILES-A 1-5 \nSCRIPT-NAME: ${INC_NAME_FILE_A}\n"
 exit 1
 fi

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

### BEGINN: Erstellung der HTML-Datei für die Thumbnail-Bilder
GALLERY_FILE_NAME_A_PATH="../${GALLERY_HTML_PAGE_NAME_A}"
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: CREATE-GALLERY CREATE-HTML-FILES-A 3-1 \nFILE: »${GALLERY_FILE_NAME_STYLESHEET_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FILE_A}\n"
exit 1
fi
echo -e "<!DOCTYPE html>
<html>
<head>
<title>${PICTURE_GALLERY_NAME_TITLE_TAG} - ${LANGUAGE_SHORT_CODE} - ${GALLERY_HTML_PAGE_NAME_A}</title>
<meta charset=\"${GALLERY_CHARSET}\">
<script type=\"text/javascript\">
<!-- hide
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_a\">
<div id=\"div_center\">
  <table id=\"table_a\">" > ${GALLERY_FILE_NAME_A_PATH}
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: CREATE-GALLERY CREATE-HTML-FILES-A 3-2 \nFILE: CAN NOT WRITE FILE »${GALLERY_FILE_NAME_A_PATH}« \nSCRIPT-NAME: ${INC_NAME_FILE_A}\n"
  exit 1
  fi
Q=${Q:+0}
for k in $(find ${THUMBNAILS_DIRECTORY_PATH} -maxdepth 1 -type f -iregex "^.*\(\.jpg\|\.jpeg\)$" | sort)
do
Q=$[$Q+1]
GALLERY_FILE_NAME_B="./${GALLERY_HTML_PAGE_NAME_B_PART_A}${Q}${GALLERY_HTML_PAGE_NAME_B_PART_C}"
THUMBNAIL_CURRENT_HTML_PATH=${k#\.}
echo -e "    <tr>
      <td><a href=\"${GALLERY_FILE_NAME_B}\" target=\"gallerymain\" onFocus=\"if(this.blur)this.blur()\"><img src=\"${THUMBNAIL_CURRENT_HTML_PATH}\" width=\"${THUMBNAIL_TARGET_WIDTH}\" height=\"${THUMBNAIL_TARGET_HEIGHT}\" title=\"${PICTURE_TOOL_TIP}:${Q}/${PICTURE_NUMBERS}\" alt=\"${PICTURE_TOOL_TIP}:${Q}/${PICTURE_NUMBERS}\"></a></td>
    </tr>" >> ${GALLERY_FILE_NAME_A_PATH}
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: CREATE-GALLERY CREATE-HTML-FILES-A 3-3 \nFILE: CAN NOT WRITE FILE »${GALLERY_FILE_NAME_A_PATH}« \nSCRIPT-NAME: ${INC_NAME_FILE_A}\n"
  exit 1
  fi
done
echo -e "  </table>
</div>
</body>
</html>
" >> ${GALLERY_FILE_NAME_A_PATH}
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: CREATE-GALLERY CREATE-HTML-FILES-A 3-4 \nFILE: CAN NOT WRITE FILE »${GALLERY_FILE_NAME_A_PATH}« \nSCRIPT-NAME: ${INC_NAME_FILE_A}\n"
  exit 1
  fi

# Textkodierung prüfen und evtl. ändern
change_text_coding ${GALLERY_FILE_NAME_A_PATH} ${GALLERY_CHARSET}
### ENDE: Erstellung der HTML-Datei für die Thumbnail-Bilder
