1#!/usr/bin/env bash
2# Nerd Fonts Version: 2.1.0
3# Script Version: 1.1.0
4# Iterates over all patched fonts directories
5# to generate ruby cask files for homebrew-fonts (https://github.com/caskroom/homebrew-fonts)
6# adds Windows versions of the fonts as well (casks files just won't download them)
7
8#set -x
9
10LINE_PREFIX="# [Nerd Fonts] "
11scripts_root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/"
12echo "dir $scripts_root_dir"
13outputdir=$scripts_root_dir../../archives
14
15cd "$scripts_root_dir/../../patched-fonts/" || {
16  echo >&2 "$LINE_PREFIX Could not find patched fonts directory"
17  exit 1
18}
19
20# limit archiving to given pattern (first script param) or entire root folder if no param given:
21if [ $# -eq 1 ]
22  then
23    pattern=$1
24    search_pattern="*$1*.zip"
25    echo "$LINE_PREFIX Limiting archive to pattern '$pattern'"
26else
27    pattern=".*"
28    search_pattern="*.zip"
29    echo "$LINE_PREFIX No limiting pattern given, will search entire folder"
30fi
31
32# clear out the directory zips
33find "${outputdir:?}" -name "$search_pattern" -type f -delete
34
35#find ./Hack -maxdepth 0 -type d | # uncomment to test 1 font
36#find ./ProFont -maxdepth 0 -type d | # uncomment to test 1 font
37# uncomment to test all fonts:
38find -- * -maxdepth 0 -iregex "$pattern" -type d |
39while read -r filename
40do
41
42  basename=$(basename "$filename")
43  searchdir=$filename
44
45  [[ -d "$outputdir" ]] || mkdir -p "$outputdir"
46
47  # -ic (ignore case not working)
48  zip -9 "$outputdir/$basename" -rj "$searchdir" -i '*.[o,t]tf' -i '*.[O,T]TF'
49  zipStatus=$?
50  if [ "$zipStatus" != "0" ]
51  then
52    echo "$LINE_PREFIX Could not create archive with the path junked (-j option) - likely same font names for different paths, zip status: $zipStatus"
53    echo "$LINE_PREFIX Retrying with full path"
54    zip -9 "$outputdir/$basename" -r "$searchdir" -i '*.[o,t]tf' -i '*.[O,T]TF'
55  fi;
56done
57