|
| 1 | +#!/bin/bash |
| 2 | +basedir=".." |
| 3 | +outputdir="output/squid" |
| 4 | +path="${basedir}/cache_domains.json" |
| 5 | +REGEX="^\\*\\.(.*)$" |
| 6 | + |
| 7 | +export IFS=' ' |
| 8 | + |
| 9 | +test=$(which jq); |
| 10 | +out=$? |
| 11 | +if [ $out -gt 0 ] ; then |
| 12 | + echo "This script requires jq to be installed." |
| 13 | + echo "Your package manager should be able to find it" |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +cachenamedefault="disabled" |
| 18 | + |
| 19 | +while read -r line; do |
| 20 | + name=$(jq -r ".cache_domains[\"${line}\"]" config.json) |
| 21 | + declare "cachename${line}"="${name}" |
| 22 | +done <<< $(jq -r '.cache_domains | to_entries[] | .key' config.json) |
| 23 | + |
| 24 | +rm -rf ${outputdir} |
| 25 | +mkdir -p ${outputdir} |
| 26 | +while read -r entry; do |
| 27 | + unset cachename |
| 28 | + key=$(jq -r ".cache_domains[$entry].name" $path) |
| 29 | + cachename="cachename${key}" |
| 30 | + if [ -z "${!cachename}" ]; then |
| 31 | + cachename="cachenamedefault" |
| 32 | + fi |
| 33 | + if [[ ${!cachename} == "disabled" ]]; then |
| 34 | + continue; |
| 35 | + fi |
| 36 | + while read -r fileid; do |
| 37 | + while read -r filename; do |
| 38 | + destfilename=$(echo ${!cachename}.txt) |
| 39 | + outputfile=${outputdir}/${destfilename} |
| 40 | + touch ${outputfile} |
| 41 | + while read -r fileentry; do |
| 42 | + # Ignore comments |
| 43 | + if [[ ${fileentry} == \#* ]] || [[ -z ${fileentry} ]]; then |
| 44 | + continue |
| 45 | + fi |
| 46 | + # Handle wildcards to squid wildcards |
| 47 | + parsed=$(echo ${fileentry} | sed -e "s/^\*\./\./") |
| 48 | + # If we have cdn.thing and *.cdn.thing in cache_domains |
| 49 | + # Squid requires ONLY cdn.thing |
| 50 | + if [[ ${fileentry} =~ $REGEX ]]; then |
| 51 | + grep "${BASH_REMATCH[1]}" ${basedir}/${filename} | grep -v "${fileentry}" > /dev/null |
| 52 | + if [[ $? -eq 0 ]]; then |
| 53 | + continue |
| 54 | + fi |
| 55 | + fi |
| 56 | + |
| 57 | + echo "${parsed}" >> "${outputfile}" |
| 58 | + done <<< $(cat ${basedir}/${filename} | sort); |
| 59 | + done <<< $(jq -r ".cache_domains[${entry}].domain_files[$fileid]" ${path}) |
| 60 | + done <<< $(jq -r ".cache_domains[${entry}].domain_files | to_entries[] | .key" ${path}) |
| 61 | +done <<< $(jq -r '.cache_domains | to_entries[] | .key' ${path}) |
| 62 | + |
| 63 | +cat << EOF |
| 64 | +Configuration generation completed. |
| 65 | +
|
| 66 | +Please copy the following files: |
| 67 | +- ./${outputdir}/*.txt to /etc/squid/domains/ |
| 68 | +EOF |
0 commit comments