You are here

fix-archives.sh in Mini site 8

#!/usr/bin/env bash
##
# Cleanup ZIP files to use with Minisite Drupal module.
#
# Usage:
# ./fix-archives.sh dir/with/zips dir/to/output/fixed

set -e

# The directory with source ZIP files.
SRC_DIR="${1}"

# The directory to output processed files. If not provided - $SRC_DIR will be used.
DST_DIR="${2:-${1}}"

#The prefix to use for every processed file.
DST_PREFIX="${3:-}"

# ------------------------------------------------------------------------------

[ "$(command_exists zip)" == "1" ] && error "Please install Zip" && exit 1
[ "$(command_exists unzip)" == "1" ] && error "Please install Unzip" && exit 1


TMP_DIR="${SRC_DIR}/tmp"

mkdir -p "${DST_DIR}"

for zipfile in "$SRC_DIR"/*.zip; do
  echo "==> Processing ${zipfile}"

  rm -Rf "${TMP_DIR}" && mkdir -p "${TMP_DIR}"

  unzip -q "${zipfile}" -d "${TMP_DIR}"

  pushd "${TMP_DIR}" > /dev/null

  # Check that there is only one parent dir.
  if [ "$(ls|wc -l)" -gt 1 ]; then
    echo "ERROR: ${zipfile} does not contain top-level directory. Re-zip manually and repeat." && exit 1
  fi

  tmp_src="$(ls | head -n 1)"

  # Check that index.html is provided.
  if [ ! -f "${tmp_src}/index.html" ]; then
    echo "ERROR: Top-level directory in archive does not contain index.html file." && exit 1
  fi

  archive="${DST_PREFIX}${tmp_src}"

  zip -q -r "${DST_DIR}/${archive}" "${tmp_src}" -i "*.html" "*.htm" "*.js" "*.css" "*.png" "*.jpg" "*.gif" "*.svg" "*.pdf" "*.doc" "*.docx" "*.ppt" "*.pptx" "*.xls" "*.xlsx" "*.tif" "*.xml" "*.txt" "*.woff" "*.woff2" "*.ttf" "*.eot" "*.ico" "*.mp4"

  popd > /dev/null

  echo "==> Created new archive ${DST_DIR}/${archive}"
  echo

  rm -Rf "${TMP_DIR}"
done

File

fix-archives.sh
View source
  1. #!/usr/bin/env bash
  2. ##
  3. # Cleanup ZIP files to use with Minisite Drupal module.
  4. #
  5. # Usage:
  6. # ./fix-archives.sh dir/with/zips dir/to/output/fixed
  7. set -e
  8. # The directory with source ZIP files.
  9. SRC_DIR="${1}"
  10. # The directory to output processed files. If not provided - $SRC_DIR will be used.
  11. DST_DIR="${2:-${1}}"
  12. #The prefix to use for every processed file.
  13. DST_PREFIX="${3:-}"
  14. # ------------------------------------------------------------------------------
  15. [ "$(command_exists zip)" == "1" ] && error "Please install Zip" && exit 1
  16. [ "$(command_exists unzip)" == "1" ] && error "Please install Unzip" && exit 1
  17. TMP_DIR="${SRC_DIR}/tmp"
  18. mkdir -p "${DST_DIR}"
  19. for zipfile in "$SRC_DIR"/*.zip; do
  20. echo "==> Processing ${zipfile}"
  21. rm -Rf "${TMP_DIR}" && mkdir -p "${TMP_DIR}"
  22. unzip -q "${zipfile}" -d "${TMP_DIR}"
  23. pushd "${TMP_DIR}" > /dev/null
  24. # Check that there is only one parent dir.
  25. if [ "$(ls|wc -l)" -gt 1 ]; then
  26. echo "ERROR: ${zipfile} does not contain top-level directory. Re-zip manually and repeat." && exit 1
  27. fi
  28. tmp_src="$(ls | head -n 1)"
  29. # Check that index.html is provided.
  30. if [ ! -f "${tmp_src}/index.html" ]; then
  31. echo "ERROR: Top-level directory in archive does not contain index.html file." && exit 1
  32. fi
  33. archive="${DST_PREFIX}${tmp_src}"
  34. zip -q -r "${DST_DIR}/${archive}" "${tmp_src}" -i "*.html" "*.htm" "*.js" "*.css" "*.png" "*.jpg" "*.gif" "*.svg" "*.pdf" "*.doc" "*.docx" "*.ppt" "*.pptx" "*.xls" "*.xlsx" "*.tif" "*.xml" "*.txt" "*.woff" "*.woff2" "*.ttf" "*.eot" "*.ico" "*.mp4"
  35. popd > /dev/null
  36. echo "==> Created new archive ${DST_DIR}/${archive}"
  37. echo
  38. rm -Rf "${TMP_DIR}"
  39. done