#!/bin/bash

# Script to package the Culmus fonts for CTAN
#
# Copyright (C) 2012 Zvi Gilboa (zgilboa@virginia.edu)
# This script is hereby released to the public domain.
# You may use or modify it at your own risk in any way you see fit.
#
# Font files are generated from .sfd source files 
# Documentation files get copied to to all font folders
# README file gets generate by this script and copied to root and doc folders
#
# The helper scripts FontForgeGeneratePfa.pe and FontForgeGenerateTrueType.pe
# should be found in a directory that is included in the user's search path

# set primary parameters
FontFamily="culmus"
Version="_121"

DateFolder=$FontFamily"_"$Version"__generated_from_source__$(date +%Y_%m_%d__%H_%M_%S)"
mkdir $DateFolder

PackageDir=$DateFolder/$FontFamily
mkdir $PackageDir

mkdir $PackageDir/doc
mkdir $PackageDir/doc/fonts
mkdir $PackageDir/doc/fonts/$FontFamily
mkdir $PackageDir/source
mkdir $PackageDir/source/fonts
mkdir $PackageDir/source/fonts/$FontFamily
mkdir $PackageDir/fonts
mkdir $PackageDir/fonts/pfa
mkdir $PackageDir/fonts/pfa/public
mkdir $PackageDir/fonts/pfa/public/$FontFamily
mkdir $PackageDir/fonts/pfa/public/$FontFamily/tmp_otf_from_type1
mkdir $PackageDir/fonts/afm
mkdir $PackageDir/fonts/afm/public
mkdir $PackageDir/fonts/afm/public/$FontFamily
mkdir $PackageDir/fonts/tfm
mkdir $PackageDir/fonts/tfm/public
mkdir $PackageDir/fonts/tfm/public/$FontFamily
mkdir $PackageDir/fonts/type1
mkdir $PackageDir/fonts/type1/public
mkdir $PackageDir/fonts/type1/public/$FontFamily
mkdir $PackageDir/fonts/opentype
mkdir $PackageDir/fonts/opentype/public
mkdir $PackageDir/fonts/opentype/public/$FontFamily
mkdir $PackageDir/fonts/truetype
mkdir $PackageDir/fonts/truetype/public
mkdir $PackageDir/fonts/truetype/public/$FontFamily

# FontForgeGeneratePostScript.pe:           ScaleToEm(1000):  generates .afm, .pfa, and .otf
# FontForgeGenerateTrueType.pe:             ScaleToEm(2048)   generates .ttf
# FontForgeGenerateOpenTypeFromType1.pe:    ScaleToEm(1000):  generates .otf (and .afm that is discarded)

for i in sfd_src/*.sfd ; 
    do cp --preserve=all "$i" $PackageDir/source/fonts/$FontFamily ; 
done

for i in sfd_src/*.sfd ; 
    do FontForgeGeneratePostScript.pe "$i" $PackageDir/fonts/pfa/public/$FontFamily ; 
done

for i in sfd_src/*.sfd ; 
    do FontForgeGenerateTrueType.pe "$i" $PackageDir/fonts/truetype/public/$FontFamily ; 
done

# handle Type1 fonts for which no sfd source is provided
for i in pfa_afm_src/*.pfa ; 
    do FontForgeGenerateOpenTypeFromType1.pe "$i" $PackageDir/fonts/pfa/public/$FontFamily/tmp_otf_from_type1 ;
done

for i in pfa_afm_src/*.pfa ; 
    do cp --preserve=all -v -t $PackageDir/fonts/pfa/public/$FontFamily "$i" ;
done

for i in pfa_afm_src/*.afm ; 
    do cp --preserve=all -v -t $PackageDir/fonts/pfa/public/$FontFamily "$i" ;
done

for i in $PackageDir/fonts/pfa/public/$FontFamily/tmp_otf_from_type1/*.otf
    do cp --preserve=all -v -t $PackageDir/fonts/pfa/public/$FontFamily "$i" ;
done


# process the (generated or provided) afm, pfa, otf, and ttf files
for i in $PackageDir/fonts/pfa/public/$FontFamily/*.afm ; 
    do afm2tfm "$i" ; 
done

for i in $PackageDir/fonts/pfa/public/$FontFamily/*.pfa ; 
    do t1binary --output ${i//.pfa/.pfb} "$i" ; 
done

# to preserve file dates, do not mv; instead, cp --preserve=all and rm
for i in $PackageDir/fonts/pfa/public/$FontFamily/*.afm ; 
    do cp --preserve=all -t $PackageDir/fonts/afm/public/$FontFamily "$i" ; 
    rm "$i"
done

for i in $PackageDir/fonts/pfa/public/$FontFamily/*.tfm ; 
    do cp --preserve=all -t $PackageDir/fonts/tfm/public/$FontFamily "$i" ; 
    rm "$i"
done

for i in $PackageDir/fonts/pfa/public/$FontFamily/*.pfb ; 
    do cp --preserve=all -t $PackageDir/fonts/type1/public/$FontFamily "$i" ; 
    rm "$i"
done

for i in $PackageDir/fonts/pfa/public/$FontFamily/*.otf ; 
    do cp --preserve=all -t $PackageDir/fonts/opentype/public/$FontFamily "$i" ; 
    rm "$i"
done

# Keter is included in the sources of both Culmus and Taamey-Culmus.
# Since we will be packaging Keter with Taamey-Culmus,
# we need remove it from the current package.

find $PackageDir/ -name "Keter*" | xargs rm

# README
# ------
# to simplify future updates, create the README file from within this script
#
# create the file
touch $DateFolder/README
#
# append text to the file: simple-and-dirty bash scripting
# Retro scripting is the next big thing!
#
ReadmeText="-----------------------------------------------------------------------------------"
echo $ReadmeText >> $DateFolder/README
echo $ReadmeText >> $DateFolder/README

ReadmeText="+++ Culmus Fonts, version 0.121"
echo $ReadmeText >> $DateFolder/README

ReadmeText="-----------------------------------------------------------------------------------"
echo $ReadmeText >> $DateFolder/README

ReadmeText="+++ The Culmus Project"
echo $ReadmeText >> $DateFolder/README

ReadmeText="-----------------------------------------------------------------------------------"
echo $ReadmeText >> $DateFolder/README

ReadmeText="+++ culmus.sourceforge.net"
echo $ReadmeText >> $DateFolder/README

ReadmeText="-----------------------------------------------------------------------------------"
echo $ReadmeText >> $DateFolder/README

ReadmeText="+++ Fonts by Maxim Iorsh, Yoram Gnat, and individual contributors"
echo $ReadmeText >> $DateFolder/README

ReadmeText="-----------------------------------------------------------------------------------"
echo $ReadmeText >> $DateFolder/README

ReadmeText="+++ For Copyright Information and Terms of Use, please see the LICENSE file"
echo $ReadmeText >> $DateFolder/README

ReadmeText="-----------------------------------------------------------------------------------"
echo $ReadmeText >> $DateFolder/README
echo $ReadmeText >> $DateFolder/README

ReadmeText="+++ Font files in this CTAN package were generated from their .sfd source files."
echo $ReadmeText >> $DateFolder/README

ReadmeText="+++ The library date of the FontForge binary was 02-July-2011."
echo $ReadmeText >> $DateFolder/README

ReadmeText="+++ The source font files (.sfd) were obtained from culmus.sourceforge.net"
echo $ReadmeText >> $DateFolder/README

ReadmeText="+++ The CTAN package maintainer is Zvi Gilboa (zgilboa at virginia dot edu)"
echo $ReadmeText >> $DateFolder/README

ReadmeText="-----------------------------------------------------------------------------------"
echo $ReadmeText >> $DateFolder/README
echo $ReadmeText >> $DateFolder/README
#  README: ALL DONE

#  copy relevant documents to the various package folders
cp --preserve=all LICENSE LICENSE-BITSTREAM CHANGES GNU-GPL $DateFolder/README $PackageDir
cp --preserve=all LICENSE LICENSE-BITSTREAM CHANGES GNU-GPL $DateFolder/README $PackageDir/doc/fonts/$FontFamily
cp --preserve=all LICENSE LICENSE-BITSTREAM CHANGES GNU-GPL $PackageDir/source/fonts/$FontFamily
cp --preserve=all LICENSE LICENSE-BITSTREAM CHANGES GNU-GPL $PackageDir/fonts/pfa/public/$FontFamily
cp --preserve=all LICENSE LICENSE-BITSTREAM CHANGES GNU-GPL $PackageDir/fonts/afm/public/$FontFamily
cp --preserve=all LICENSE LICENSE-BITSTREAM CHANGES GNU-GPL $PackageDir/fonts/tfm/public/$FontFamily
cp --preserve=all LICENSE LICENSE-BITSTREAM CHANGES GNU-GPL $PackageDir/fonts/type1/public/$FontFamily
cp --preserve=all LICENSE LICENSE-BITSTREAM CHANGES GNU-GPL $PackageDir/fonts/opentype/public/$FontFamily
cp --preserve=all LICENSE LICENSE-BITSTREAM CHANGES GNU-GPL $PackageDir/fonts/truetype/public/$FontFamily

#  CTAN: .pfa & .sfd files are not needed
ExclusionTagFileName="NOT_FOR_CTAN.tag"
touch $PackageDir/fonts/pfa/$ExclusionTagFileName
touch $PackageDir/source/$ExclusionTagFileName

#  pre-archive clean-up
rm $DateFolder/README

#  create tar archive file
tar --directory $DateFolder/ --create --file $DateFolder/$FontFamily.tar.gz --gzip --verbose --exclude-tag-all=$ExclusionTagFileName $FontFamily/

# post-processing: copy afm files to the Sketch directory (used for work on CulmusX)
for i in $PackageDir/fonts/afm/public/$FontFamily/*.afm ;
    do cp --preserve=all -t /home/zgilboa/texmf/tex/Sketch/main/afm "$i" ;
done 
