|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +declare htaccess_config_default="htaccess.conf"; |
| 4 | +declare htaccess_output_default="./.htaccess" |
| 5 | +declare repo_root |
| 6 | +repo_root=$(dirname "$(dirname "$0")") |
| 7 | + |
| 8 | +# ---------------------------------------------------------------------- |
| 9 | +# | Helper functions | |
| 10 | +# ---------------------------------------------------------------------- |
| 11 | + |
| 12 | +create_htaccess() { |
| 13 | + local file="${1}" |
| 14 | + local config="${2}" |
| 15 | + |
| 16 | + local version |
| 17 | + version=$(grep version < "${repo_root}/package.json" | \ |
| 18 | + head -1 | awk -F: '{ print $2 }' | sed 's/[",\t ]//g') |
| 19 | + |
| 20 | + insert_line "# Apache Server Configs v$version | MIT License" "$file" |
| 21 | + insert_line "# https://github.com/h5bp/server-configs-apache" "$file" |
| 22 | + insert_line "" "$file" |
| 23 | + insert_line "# (!) Using \`.htaccess\` files slows down Apache, therefore, if you have" "$file" |
| 24 | + insert_line "# access to the main server configuration file (which is usually called" "$file" |
| 25 | + insert_line "# \`httpd.conf\`), you should add this logic there." "$file" |
| 26 | + insert_line "#" "$file" |
| 27 | + insert_line "# https://httpd.apache.org/docs/current/howto/htaccess.html" "$file" |
| 28 | + insert_line "" "$file" |
| 29 | + |
| 30 | + |
| 31 | + while IFS=$" " read -r keyword filename; do |
| 32 | + |
| 33 | + # Skip lines which |
| 34 | + [[ "${keyword}" =~ ^[[:space:]]*# ]] && continue |
| 35 | + [ -z "${keyword}" ] && continue |
| 36 | + |
| 37 | + # Remove quotes surrounding |
| 38 | + filename="${filename%\"}" |
| 39 | + filename="${filename#\"}" |
| 40 | + |
| 41 | + # Evaluate |
| 42 | + case "${keyword}" in |
| 43 | + "title") |
| 44 | + insert_header "${filename}" "$file" |
| 45 | + insert_line "" "$file" |
| 46 | + ;; |
| 47 | + "enable") |
| 48 | + if [ ! -f "${filename}" ]; then |
| 49 | + filename="${repo_root}/${filename}" |
| 50 | + fi |
| 51 | + |
| 52 | + if [ ! -f "${filename}" ]; then |
| 53 | + print_error ".htaccess partial '${filename}' does not exist." |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | + |
| 57 | + insert_file "${filename}" "$file" |
| 58 | + insert_line "" "$file" |
| 59 | + ;; |
| 60 | + "disable") |
| 61 | + if [ ! -f "${filename}" ]; then |
| 62 | + filename="${repo_root}/${filename}" |
| 63 | + fi |
| 64 | + |
| 65 | + if [ ! -f "${filename}" ]; then |
| 66 | + print_error ".htaccess partial '${filename}' does not exist." |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | + |
| 70 | + insert_file_comment_out "${filename}" "$file" |
| 71 | + insert_line "" "$file" |
| 72 | + ;; |
| 73 | + "omit") |
| 74 | + # noop |
| 75 | + ;; |
| 76 | + *) |
| 77 | + print_error "Invalid keyword '${keyword}' for entry '${filename}'" |
| 78 | + exit 1 |
| 79 | + ;; |
| 80 | + esac |
| 81 | + |
| 82 | + done < "${config}" |
| 83 | + |
| 84 | + apply_pattern "$file" |
| 85 | +} |
| 86 | + |
| 87 | +insert_line() { |
| 88 | + printf "$1\\n" >> "$2" |
| 89 | +} |
| 90 | + |
| 91 | +insert_file() { |
| 92 | + cat "$1" >> "$2" |
| 93 | +} |
| 94 | + |
| 95 | +insert_file_comment_out() { |
| 96 | + printf "%s\\n" "$(sed -E 's/^([^#])(.+)$/# \1\2/g' < "$1")" >> "$2" |
| 97 | +} |
| 98 | + |
| 99 | +insert_header() { |
| 100 | + local title |
| 101 | + title=$(printf "$1" | tr '[:lower:]' '[:upper:]') |
| 102 | + |
| 103 | + insert_line "# ######################################################################" "$2" |
| 104 | + insert_line "# # $title $(insert_space "$title") #" "$2" |
| 105 | + insert_line "# ######################################################################" "$2" |
| 106 | +} |
| 107 | + |
| 108 | +insert_space() { |
| 109 | + total=65 |
| 110 | + occupied=$(printf "$1" | wc -c) |
| 111 | + difference=$((total - occupied)) |
| 112 | + printf '%0.s ' $(seq 1 $difference) |
| 113 | +} |
| 114 | + |
| 115 | +apply_pattern() { |
| 116 | + sed -e "s/%FilesMatchPattern%/$( \ |
| 117 | + sed '/^#/d' < "${repo_root}/src/files_match_pattern" | \ |
| 118 | + tr -s '[:space:]' '|' | \ |
| 119 | + sed 's/|$//' \ |
| 120 | + )/g" -i "" "$1" |
| 121 | +} |
| 122 | + |
| 123 | +print_error() { |
| 124 | + # Print output in red |
| 125 | + printf "\\e[0;31m [✖] $1 $2\\e[0m\\n" |
| 126 | +} |
| 127 | + |
| 128 | +print_info() { |
| 129 | + # Print output in purple |
| 130 | + printf "\\n\\e[0;35m $1\\e[0m\\n\\n" |
| 131 | +} |
| 132 | + |
| 133 | +print_success() { |
| 134 | + # Print output in green |
| 135 | + printf "\\e[0;32m [✔] $1\\e[0m\\n" |
| 136 | +} |
| 137 | + |
| 138 | +# ---------------------------------------------------------------------- |
| 139 | +# | Main | |
| 140 | +# ---------------------------------------------------------------------- |
| 141 | + |
| 142 | +main() { |
| 143 | + local htaccess_output="${1}" |
| 144 | + local htaccess_config="${2}" |
| 145 | + local htaccess_output_directory |
| 146 | + htaccess_output_directory=$(dirname "${htaccess_output}") |
| 147 | + |
| 148 | + if [ -z "${htaccess_config}" ]; then |
| 149 | + if [ -f "${PWD}/${htaccess_config_default}" ]; then |
| 150 | + htaccess_config="${PWD}/${htaccess_config_default}" |
| 151 | + else |
| 152 | + htaccess_config="${repo_root}/${htaccess_config_default}" |
| 153 | + fi; |
| 154 | + fi |
| 155 | + |
| 156 | + if [ ! -f "${htaccess_config}" ]; then |
| 157 | + print_error "'${htaccess_config}' does not exist." |
| 158 | + exit 1 |
| 159 | + fi |
| 160 | + |
| 161 | + mkdir -p "${htaccess_output_directory}" |
| 162 | + |
| 163 | + if [ -f "${htaccess_output}" ]; then |
| 164 | + cp "${htaccess_output}" "${htaccess_output}.old" |
| 165 | + print_info "File already exist, create backup" |
| 166 | + fi |
| 167 | + |
| 168 | + rm -f "${htaccess_output}" |
| 169 | + create_htaccess "${htaccess_output}" "${htaccess_config}" |
| 170 | + |
| 171 | + if [ $? ]; then # Success |
| 172 | + print_success "Build ${htaccess_output}" |
| 173 | + else |
| 174 | + print_error "Error while building ${htaccess_output}" |
| 175 | + if [ -f "${htaccess_output}.old" ]; then |
| 176 | + cp "${htaccess_output}.old" "${htaccess_output}" |
| 177 | + fi |
| 178 | + fi |
| 179 | +} |
| 180 | + |
| 181 | +main "${1:-$htaccess_output_default}" "${2}" |
0 commit comments