#
# FUNKTION: Enthält Funktionen zur Bearbeitung der Bilder-Galerie
#
INC_NAME_FUNCTION='inc_function.txt'

# Array mit einige zu ersetzenden Zeichen; Array mit den entsprechenden HTML-Code der zu ersetzenden Zeichen
# siehe: inc_create_html_files_b.txt, inc_index_html_file_gallery.txt
declare -a TEXT_CHAR_ARRAY
TEXT_CHAR_ARRAY=("&" '\\' "\"" "'" "\$" "ä" "ö" "ü" "Ä" "Ö" "Ü" "ß" "ç" "ñ" "æ" "á" "à" "é" "è" "í" "ì" "ó" "ò" "ú" "ù" "ë" "ï" "ê" "«" "»" "\%" "|" "<" ">" "EURO" "EUR")
declare -a CHAR_REPLACE_ARRAY
CHAR_REPLACE_ARRAY=("&amp;" "&#92;" "&quot;" "&#39;" "&#36;" "&auml;" "&ouml;" "&uuml;" "&Auml;" "&Ouml;" "&Uuml;" "&szlig;" "&ccedil;" "&ntilde;" "&aelig;" "&aacute;" "&agrave;" "&eacute;" "&egrave;" "&iacute;" "&igrave;" "&oacute;" "&ograve;" "&uacute;" "&ugrave;" "&euml;" "&iuml;" "&ecirc;" "&laquo;" "&raquo;" "&#37;" "&#124;" "&lt;" "&gt;" "&euro;" "&#8364;")
CHAR_REPLACE_ARRAY_COUNT=${#CHAR_REPLACE_ARRAY[@]}

# Bilddateien die nicht vom Typ JPEG oder PNG sind löschen; Parameter $1 übergibt den vollständigen Verzeichnispfad
delete_not_supported_picture () {
  PICTURE_DIR_PATH=$1
  PICTURE_DIR_PATH=${PICTURE_DIR_PATH:='EMPTY'}
  if [ "${PICTURE_DIR_PATH}" != 'EMPTY' ]
  then
    if [ -d ${PICTURE_DIR_PATH} ]
    then
      find ${PICTURE_DIR_PATH} -maxdepth 1 -type f -not -iregex "^.*\(\.jpg\|\.jpeg\|\.png\)$" -exec rm -f {} \; 2> /dev/null
	if [ $? -ge 1 ]
	then
	echo -e "\nERROR-CODE: GALLERY FUNCTION 1-1 \nFILE: NOT SUPPORTED PICTURE TYPE - CAN NOT DELETE \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
	exit 1
	fi
    else
    echo -e "\nERROR-CODE: GALLERY FUNCTION 1-2 \nDIRECTORY: »${PICTURE_DIR_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
    exit 1
    fi
  else
  echo -e "\nERROR-CODE: GALLERY FUNCTION 1-3 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
  exit 1
  fi
}

# Leerzeichen in den Bildnamen ersetzen; Parameter $1 übergibt den vollständigen Verzeichnispfad
rename_picture_file_name () {
  PICTURE_DIR_PATH=$1
  PICTURE_DIR_PATH=${PICTURE_DIR_PATH:='EMPTY'}
  if [ "${PICTURE_DIR_PATH}" != 'EMPTY' ]
  then
    if [ -d ${PICTURE_DIR_PATH} ]
    then
      for i in ${PICTURE_DIR_PATH}/*
      do
	if [ $(echo "${i}" | grep -c "^.*[[:blank:]].*$") -ge 1 ]
	then
	  mv "${i}" $(echo "${i}" | tr " " _)
	    if [ $? -ge 1 ]
	    then
	    echo -e "\nERROR-CODE: GALLERY FUNCTION 2-1 \nFILE: CAN NOT RENAME PICTURE-FILE »${i}« \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
	    exit 1
	    fi
	fi
      done
    else
    echo -e "\nERROR-CODE: GALLERY FUNCTION 2-2 \nDIRECTORY: »${PICTURE_DIR_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
    exit 1
    fi
  else
  echo -e "\nERROR-CODE: GALLERY FUNCTION 2-3 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
  exit 1
  fi
}

# Sicherungskopien (Endung: ~) löschen; Parameter $1 übergibt den vollständigen Verzeichnispfad
delete_backup_copies () {
  GALLERY_DIR_PATH=$1
  GALLERY_DIR_PATH=${GALLERY_DIR_PATH:='EMPTY'}
  if [ "${GALLERY_DIR_PATH}" != 'EMPTY' ]
  then
    if [ -d ${GALLERY_DIR_PATH} ]
    then
      find ${GALLERY_DIR_PATH} -type f -iregex "^.*\.*~$" -exec rm -f {} \; 2> /dev/null
	if [ $? -ge 1 ]
	then
	echo -e "\nERROR-CODE: GALLERY FUNCTION 3-1 \nFILE: CAN NOT DELETE BACKUP-FILES \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
	exit 1
	fi
    else
    echo -e "\nERROR-CODE: GALLERY FUNCTION 3-2 \nDIRECTORY: »${GALLERY_DIR_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
    exit 1
    fi
  else
  echo -e "\nERROR-CODE: GALLERY FUNCTION 3-3 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
  exit 1
  fi
}

# Textcodierungen die nicht in UTF-8 vorliegen ändern; Parameter $1 übergibt den vollständigen Dateipfad; Parameter $2 die Textkodierung
change_text_coding () {
  HTML_FILE_PATH=$1
  HTML_FILE_PATH=${HTML_FILE_PATH:='EMPTY'}
  TEXT_CODING_GALLERY_DEFAULT=$2
  TEXT_CODING_GALLERY_DEFAULT=${TEXT_CODING_GALLERY_DEFAULT:='UTF-8'}
  TEXT_CODING_GALLERY_DEFAULT=$(echo ${TEXT_CODING_GALLERY_DEFAULT} | tr [:lower:] [:upper:])
    if [ $? -ge 1 ]
    then
    echo -e "\nERROR-CODE: GALLERY FUNCTION 4-1 \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
    exit 1
    fi
  TEXT_CODING_SYSTEM=$(locale | grep "^LANG=" | cut -d . -f 2)
    if [ $? -ge 1 ]
    then
    echo -e "\nERROR-CODE: GALLERY FUNCTION 4-2 \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
    exit 1
    fi
  TEXT_CODING_SYSTEM==$(echo ${TEXT_CODING_SYSTEM} | tr [:lower:] [:upper:])
    if [ $? -ge 1 ]
    then
    echo -e "\nERROR-CODE: GALLERY FUNCTION 4-3 \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
    exit 1
    fi
  if [ "${TEXT_CODING_SYSTEM}" != ${TEXT_CODING_GALLERY_DEFAULT} ]
  then
    if [ "${HTML_FILE_PATH}" != 'EMPTY' ]
    then
      if [ -f ${HTML_FILE_PATH} ]
      then
      recode -f ${TEXT_CODING_SYSTEM}..${TEXT_CODING_GALLERY_DEFAULT} ${HTML_FILE_PATH}
	if [ $? -ge 1 ]
	then
	echo -e "\nERROR-CODE: GALLERY FUNCTION 4-4 \nFILE: »${HTML_FILE_PATH}« CAN NOT CHANGE TEXT-CODING \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
	exit 1
	fi
      else
      echo -e "\nERROR-CODE: GALLERY FUNCTION 4-5 \nFILE: »${HTML_FILE_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
      exit 1
      fi
    else
    echo -e "\nERROR-CODE: GALLERY FUNCTION 4-6 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
    exit 1
    fi
  fi
}

# Zugriffsrechte einiger Include-Dateien und der Shellskripte ändern; Parameter $1 übergibt den vollständigen Dateipfad
change_access_rights () {
  # sinnvolle Werte: 0400, 0600, 0644
  INCLUDE_ACCESS_RIGHT='0600'
  # sinnvolle Werte: 0500, 0700, 0755
  SCRIPTS_ACCESS_RIGHT='0700'
  INCLUDE_DIR_PATH=$1
  INCLUDE_DIR_PATH=${INCLUDE_DIR_PATH:='EMPTY'}
  SCRIPTS_DIR_PATH='./'
  if [ "${INCLUDE_DIR_PATH}" != 'EMPTY' ]
  then
    if [ -d ${INCLUDE_DIR_PATH} ]
    then
    # Zugriffsrechte der Include-Dateien ändern
    find ${INCLUDE_DIR_PATH} -maxdepth 1 -type f -iregex "^.*\(\.txt\)$" -exec chmod ${INCLUDE_ACCESS_RIGHT} {} \; 2> /dev/null
	if [ $? -ge 1 ]
	then
	echo -e "\nERROR-CODE: GALLERY FUNCTION 5-1 \nFILE: CAN NOT CHANGE ACCESS RIGHTS \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
	exit 1
	fi
    # Zugriffsrechte der Shellskripte im aktuellen Verzeichnis ändern
    find ${SCRIPTS_DIR_PATH} -maxdepth 1 -type f -iregex "^.*\(\.sh\)$" -exec chmod ${SCRIPTS_ACCESS_RIGHT} {} \; 2> /dev/null
	if [ $? -ge 1 ]
	then
	echo -e "\nERROR-CODE: GALLERY FUNCTION 5-2 \nFILE: CAN NOT CHANGE ACCESS RIGHTS \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
	exit 1
	fi
    else
    echo -e "\nERROR-CODE: GALLERY FUNCTION 5-3 \nDIRECTORY: »${SCRIPTS_DIR_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
    exit 1
    fi
  else
  echo -e "\nERROR-CODE: GALLERY FUNCTION 5-4 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_FUNCTION}\n"
  exit 1
  fi
}
