#! /usr/bin/env fish

# Extracts and Patches the amazing Berkeley Mono font

if test (count $argv) -ne 1
    echo "Usage: ron-patch-berkeley-mono <zip-file>"
    exit 1
end

set zip_path $argv[1]

if not test -f $zip_path
    echo "File not found: $zip_path"
    exit 1
end

if test (uname) = Darwin
    set system_fonts /Library/Fonts
else
    set system_fonts /usr/share/fonts/berkeley
end

set original_path (pwd)
set base_dir ~/tmp_nf_patch
set patcher_image nerdfonts/patcher

rm -rf $base_dir
mkdir -p $base_dir
sudo mkdir -p $system_fonts

cd (realpath (dirname $zip_path))
set extracted_dir (unzip -Z1 $zip_path | string match -r "^[^/]+" | path sort -u)
if test (count $extracted_dir) -ne 1
    echo "Expected exactly one top-level directory in zip, found: " $extracted_dir
    exit 1
end

unzip -o $zip_path
or exit 1

set font_dir $extracted_dir/TX-02-*
if test (count $font_dir) -ne 1
    echo "Expected exactly one TX-02-* dir, found: " $font_dir
    exit 1
end

cp -r $font_dir $base_dir/in
sudo cp -r $font_dir/* $system_fonts

cd $base_dir
mkdir $base_dir/out

function patch
    docker run --rm \
        -e PN=8 \
        -v $base_dir/in:/in \
        -v $base_dir/out:/out \
        $patcher_image \
        --complete $argv
    or exit 1
end

patch # Mono
patch --variable-width-glyphs # Propo

set patched_fonts $base_dir/out/*
if test (count $patched_fonts) -eq 0
    echo "Didn't generate any patched fonts"
    exit 1
end

sudo yes | cp $patched_fonts $system_fonts

rm -rf $base_dir
cd $original_path
