Skip to content

Instantly share code, notes, and snippets.

@k0nsl
Forked from narolinus/fetcher.sh
Created April 16, 2017 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k0nsl/9fed793c4bfc2ec0e8d6f35a91460344 to your computer and use it in GitHub Desktop.
Save k0nsl/9fed793c4bfc2ec0e8d6f35a91460344 to your computer and use it in GitHub Desktop.
Mailcow: Fetching external resources and convert stylesheets accordingly.
#!/bin/bash
# reuires google-font-download by Clemens Lang
# https://github.com/neverpanic/google-font-download
# 1) Put both scripts in mailcow-dockerized home directory.
# 2) Reset data/web to default (only css, fonts an js).
# rm -rf data/web/{css,fonts,js} && git checkout -- data/web && git status
# 3) Run fetcher.sh
# configuration
THEMES="cerulean cosmo cyborg darkly flatly journal lumen paper readable sandstone simplex slate spacelab superhero united yeti"
# enter web directory
cd data/web
# extract versions from header.inc.php
VERSION_BOOTSTRAP=`sed -n "s/.*twitter-bootstrap\/\([0-9]*\.[0-9]*\.[0-9]*\)\/css.*/\1/p" inc/header.inc.php`
VERSION_JQUERY=`sed -n "s/.*jquery\/\([0-9]*\.[0-9]*\.[0-9]*\)\/jquery\.min\.js.*/\1/p" inc/header.inc.php`
VERSION_RESPOND=`sed -n "s/.*respond\/\([0-9]*\.[0-9]*\.[0-9]*\)\/respond\.min\.js.*/\1/p" inc/header.inc.php`
VERSION_SHIV=`sed -n "s/.*html5shiv\/\([0-9]*\.[0-9]*\.[0-9]*\)\/html5shiv\.min\.js.*/\1/p" inc/header.inc.php`
# fetch javascript libraries
cd js
wget "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/${VERSION_BOOTSTRAP}/js/bootstrap.min.js" -O bootstrap.min.js
wget "https://oss.maxcdn.com/html5shiv/${VERSION_SHIV}/html5shiv.min.js" -O html5shiv.min.js
wget "https://oss.maxcdn.com/respond/${VERSION_RESPOND}/respond.min.js" -O respond.min.js
wget "https://cdnjs.cloudflare.com/ajax/libs/jquery/${VERSION_JQUERY}/jquery.min.js" -O jquery.min.js
cd ..
# fetch stylesheets
cd css
wget "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/${VERSION_BOOTSTRAP}/css/bootstrap.min.css" -O bootstrap.min.css
# fetch themes
for theme in $THEMES;
do
mkdir $theme
cd $theme
wget "https://cdnjs.cloudflare.com/ajax/libs/bootswatch/${VERSION_BOOTSTRAP}/${theme}/bootstrap.min.css" -O bootstrap.min.css
cd ..
done
# fetch glyph-font
mkdir fonts
cd fonts
echo "Downloading glyph-font..."
wget "https://cdnjs.cloudflare.com/ajax/libs/bootswatch/${VERSION_BOOTSTRAP}/fonts/glyphicons-halflings-regular.woff" -O glyphicons-halflings-regular.woff
wget "https://cdnjs.cloudflare.com/ajax/libs/bootswatch/${VERSION_BOOTSTRAP}/fonts/glyphicons-halflings-regular.woff2" -O glyphicons-halflings-regular.woff2
wget "https://cdnjs.cloudflare.com/ajax/libs/bootswatch/${VERSION_BOOTSTRAP}/fonts/glyphicons-halflings-regular.eot" -O glyphicons-halflings-regular.eot
wget "https://cdnjs.cloudflare.com/ajax/libs/bootswatch/${VERSION_BOOTSTRAP}/fonts/glyphicons-halflings-regular.ttf" -O glyphicons-halflings-regular.ttf
wget "https://cdnjs.cloudflare.com/ajax/libs/bootswatch/${VERSION_BOOTSTRAP}/fonts/glyphicons-halflings-regular.svg" -O glyphicons-halflings-regular.svg
cd ../..
# collect all needed fonts from stylesheets
pwd
echo "Scanning stylesheets for external fonts..."
fontlist=`grep -r 'https://fonts.googleapis' css/ | sed "s/.*family=\(.*\)\".*/\1/g"`
echo "list of external fonts: $fontlist"
uniquefontlist=`for flfont in $fontlist; do fontname=${flfont%:*}; families=${flfont#*:}; families=${families//,/ }; for family in $families; do echo $fontname:$family; done; done | sort -u`
echo "list of *unique* external fonts: $uniquefontlist"
# fetch all needed fonts
mkdir fonts
cd fonts
echo "Downloading fonts to `pwd`..."
for font in $uniquefontlist; do ../../../google-font-download -f "woff woff2 svg eot" -o "${font//[:+]/_}.css" "${font//+/ }"; done
# clone glyph-font
cp ../css/fonts/glyphicons-halflings-regular.* .
# patch stylesheets
cd ../css
stylelist=`grep -r 'https://fonts.googleapis' . | sed "s/:.*//g"`
for style in $stylelist; do
echo "Patching $style..."
# add local stylesheets
stylefontlist=`grep -r 'https://fonts.googleapis' $style | sed "s/.*family=\(.*\)\".*/\1/g"`
for stylefont in $stylefontlist; do
stylefontname=${stylefont%:*}
stylefontname=${stylefontname//+/_}
stylefontfamilies=${stylefont#*:}
stylefontfamilies=${stylefontfamilies//,/ }
for stylefontfamily in $stylefontfamilies; do
echo "1i@import url(\"/fonts/${stylefontname}_${stylefontfamily}.css\"); >> $style"
sed -i "1i@import url(\"/fonts/${stylefontname}_${stylefontfamily}.css\");" $style
done
done
# remove Google stylesheets
sed -i "s/@import url(\"https:\/\/fonts\.google.*\");//" $style
done
# patch header.inc
cd ..
sed -i "s/https:\/\/oss\.maxcdn\.com\/html5shiv\/${VERSION_SHIV}/\/js/" inc/header.inc.php
sed -i "s/https:\/\/oss\.maxcdn\.com\/respond\/${VERSION_RESPOND}/\/js/" inc/header.inc.php
sed -i "s/<script src=\"https:\/\/cdnjs\.cloudflare\.com\/ajax\/libs\/jquery\/${VERSION_JQUERY}\/jquery.min.js\".*></<script src=\"\/js\/jquery\.min\.js\"></" inc/header.inc.php
sed -i "s/\/\/cdnjs\.cloudflare\.com\/ajax\/libs\/twitter-bootstrap\/${VERSION_BOOTSTRAP}//" inc/header.inc.php
sed -i "s/\/\/cdnjs\.cloudflare\.com\/ajax\/libs\/bootswatch\/${VERSION_BOOTSTRAP}/\/css/" inc/header.inc.php
# patch footer.inc
sed -i "s/\/\/cdnjs\.cloudflare\.com\/ajax\/libs\/twitter-bootstrap\/${VERSION_BOOTSTRAP}//" inc/footer.inc.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment