Skip to content

Instantly share code, notes, and snippets.

@pl12133
Last active November 16, 2018 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pl12133/aadc10ad45be4952336b62b39c9e8c3a to your computer and use it in GitHub Desktop.
Save pl12133/aadc10ad45be4952336b62b39c9e8c3a to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#####
# File: import-icomoon.sh
# Author: pl12133
# Description: Import a font into zm-x-ui from an Icomoon.zip file
# Usage:
# 1. Set environment variable ZM_X_UI_DIR to point to your zm-x-ui directory; the default is "$HOME/github/Zimbra/zm-x-ui"
# $: export ZM_X_UI_DIR="$HOME/path/to/zm-x-ui"
# 2. Run script with path to icomoon.zip file as argument:
# $: ./import-icomoon.sh ./path/to/icomoon.zip
#####
set -e
# Mac does not have `realpath`, fake it: https://stackoverflow.com/questions/3572030/bash-script-absolute-path-with-osx
realpath() {
[[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
}
# Set ZM_X_UI_DIR to the directory with zm-x-ui
if [ -z $ZM_X_UI_DIR ]; then
ZM_X_UI_DIR="${HOME}/github/Zimbra/zm-x-ui";
fi
ZIP_FILE=$(realpath "$PWD/$1")
# Exit if we can't find dependant files
if [ -z "$1" ]; then
echo "No input file specified, please pass a .zip file as argument"
exit 1
fi
if [ ! -f "${ZM_X_UI_DIR}/icons.less" ]; then
echo "Could not find ${ZM_X_UI_DIR}/icons.less file. Please set ZM_X_UI_DIR."
exit 1
fi
if [ ! -f "$ZIP_FILE" ]; then
echo "Could not find input zip file ${ZIP_FILE}"
exit 1
fi
CWD=$PWD
TEMP_DIR=$(mktemp -d)
# Assume were good and go
cd "$TEMP_DIR"
echo "Unzipping $ZIP_FILE"
unzip "$ZIP_FILE" > /dev/null
mv fonts/* ${ZM_X_UI_DIR}/icons/
mv selection.json ${ZM_X_UI_DIR}/icons/
# Replace ".icon-" selectors with ".zimbra-icon"
sed -i 's|\.icon-|.zimbra-icon-|' style.css
# Use top half of icons.less and use bottom half of style.css
sed '/\.zimbra-icon-/q' "${ZM_X_UI_DIR}/icons.less" | head -n -1 > before.txt
sed -n '/\.zimbra-icon-/,$p' style.css > after.txt
# Fixup indentation
sed -i 's|^|\t|' after.txt
sed -i 's| |\t|g' after.txt
# Combine the two pieces to create the new icons stylesheet
cat before.txt after.txt > "${ZM_X_UI_DIR}/icons.less"
# Close ":global {" block
echo "}" >> "${ZM_X_UI_DIR}/icons.less"
echo "Import of $1 to $ZM_X_UI_DIR was successful"
# Cleanup
rm -rf "$TEMP_DIR"
cd $CWD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment