%define gsversion %(ghostscript --version)

%define xfontdir	%{_datadir}/fonts/%{name}
## for 7.07
#% define gsfontdir	%{_datadir}/ghostscript/Resource/CIDFont
## for 8.xx
%define gsfontdir	%{_datadir}/ghostscript/%{gsversion}/Resource/CIDFont
%define gsfontspecdir	%{_datadir}/ghostscript/%{gsversion}/Resource/Font
%define texfontdir	%{_datadir}/texmf/fonts/opentype/screen/hiragino

## use some macros for %post, %posttrans
%define touch_run(%1)	%{expand:( %__mkdir_p /var/run/texlive; touch /var/run/texlive/run-%1; )
}
%define exec_upddeffont	[ -x %{_sbindir}/update-defaultfont ] && \\\
    %{_sbindir}/update-defaultfont 2> /dev/null
%define exec_texhash	[ -e /var/run/texlive/run-texhash ] && ( \\\
    [ -x %{_bindir}/texhash ] && \\\
    echo -n "    "; echo -n "Running texhash... "; \\\
    %{_bindir}/texhash 2> /dev/null; \\\
    echo "done." ) && %__rm -f /var/run/texlive/run-texhash;
%define exec_mtxrun	[ -e /var/run/texlive/run-mtxrun ] && ( \\\
    [ -x %{_bindir}/mtxrun ] && \\\
    echo -n "    "; echo -n "Running mtxrun... "; \\\
    export TEXMF=%{texmf}-dist TEXMFCNF=%{texmf}/web2c TEXMFCACHE=%{texmfsysvar}; \\\
    %{_bindir}/mtxrun --generate &> /dev/null; \\\
    echo "done." ) && %__rm -f /var/run/texlive/run-mtxrun; 
%define exec_fmtutil	[ -e /var/run/texlive/run-fmtutil ] && ( \\\
    [ -x %{_bindir}/fmtutil-sys ] && \\\
    echo -n "    "; echo -n "Running fmtutil-sys... "; \\\
    %{_bindir}/fmtutil-sys --all &> /dev/null; \\\
    echo "done." ) && %__rm -f /var/run/texlive/run-fmtutil; 
%define exec_updmap	[ -e /var/run/texlive/run-updmap ] && ( \\\
    [ -x %{_bindir}/updmap-sys ] && \\\
    echo -n "    "; echo -n "Running updmap-sys... "; \\\
    %{_bindir}/updmap-sys --nohash --quiet &> /dev/null; \\\
    echo "done." ) && %__rm -f /var/run/texlive/run-updmap; 

%define FontList \\\
    HiraKakuPro-W3,Japan \\\
    HiraKakuPro-W6,Japan \\\
    HiraKakuStd-W8,Japan \\\
    HiraMaruPro-W4,Japan \\\
    HiraMinPro-W3,Japan \\\
    HiraMinPro-W6,Japan \\\
    HiraKakuProN-W3,Japan \\\
    HiraKakuProN-W6,Japan \\\
    HiraKakuStdN-W8,Japan \\\
    HiraMaruProN-W4,Japan \\\
    HiraMinProN-W3,Japan \\\
    HiraMinProN-W6,Japan \\\
    %{?extras: \\\
      HiraginoSansGB-W3,GB \\\
      HiraginoSansGB-W6,GB \\\
      HiraginoSansCNS-W3,CNS \\\
      HiraginoSansCNS-W6,CNS \\\
    }

%define EncodeList_Japan \\\
    78-EUC-H \\\
    78-EUC-V \\\
    78-H \\\
    78-RKSJ-H \\\
    78-RKSJ-V \\\
    78-V \\\
    78ms-RKSJ-H \\\
    78ms-RKSJ-V \\\
    83pv-RKSJ-H \\\
    90ms-RKSJ-H \\\
    90ms-RKSJ-V \\\
    90msp-RKSJ-H \\\
    90msp-RKSJ-V \\\
    90pv-RKSJ-H \\\
    90pv-RKSJ-V \\\
    Add-H \\\
    Add-RKSJ-H \\\
    Add-RKSJ-V \\\
    Add-V \\\
    Adobe-Japan1-0 \\\
    Adobe-Japan1-1 \\\
    Adobe-Japan1-2 \\\
    Adobe-Japan1-3 \\\
    Adobe-Japan1-4 \\\
    Adobe-Japan1-5 \\\
    Adobe-Japan1-6 \\\
    EUC-H \\\
    EUC-V \\\
    Ext-H \\\
    Ext-RKSJ-H \\\
    Ext-RKSJ-V \\\
    Ext-V \\\
    H \\\
    Hankaku \\\
    Hiragana \\\
    Identity-H \\\
    Identity-V \\\
    Katakana \\\
    NWP-H \\\
    NWP-V \\\
    RKSJ-H \\\
    RKSJ-V \\\
    Roman \\\
    UniJIS-UCS2-H \\\
    UniJIS-UCS2-HW-H \\\
    UniJIS-UCS2-HW-V \\\
    UniJIS-UCS2-V \\\
    UniJIS-UTF16-H \\\
    UniJIS-UTF16-V \\\
    UniJIS-UTF32-H \\\
    UniJIS-UTF32-V \\\
    UniJIS-UTF8-H \\\
    UniJIS-UTF8-V \\\
    UniJIS2004-UTF16-H \\\
    UniJIS2004-UTF16-V \\\
    UniJIS2004-UTF32-H \\\
    UniJIS2004-UTF32-V \\\
    UniJIS2004-UTF8-H \\\
    UniJIS2004-UTF8-V \\\
    UniJISPro-UCS2-HW-V \\\
    UniJISPro-UCS2-V \\\
    UniJISPro-UTF8-V \\\
    UniJISX0213-UTF32-H \\\
    UniJISX0213-UTF32-V \\\
    UniJISX02132004-UTF32-H \\\
    UniJISX02132004-UTF32-V \\\
    V \\\
    WP-Symbol

%define EncodeList_GB \\\
    Adobe-GB1-0 \\\
    Adobe-GB1-1 \\\
    Adobe-GB1-2 \\\
    Adobe-GB1-3 \\\
    Adobe-GB1-4 \\\
    Adobe-GB1-5 \\\
    GB-EUC-H \\\
    GB-EUC-V \\\
    GB-H \\\
    GB-RKSJ-H \\\
    GB-V \\\
    GBK-EUC-H \\\
    GBK-EUC-V \\\
    GBK2K-H \\\
    GBK2K-V \\\
    GBKp-EUC-H \\\
    GBKp-EUC-V \\\
    GBT-EUC-H \\\
    GBT-EUC-V \\\
    GBT-H \\\
    GBT-RKSJ-H \\\
    GBT-V \\\
    GBTpc-EUC-H \\\
    GBTpc-EUC-V \\\
    GBpc-EUC-H \\\
    GBpc-EUC-V \\\
    Identity-H \\\
    Identity-V \\\
    UniGB-UCS2-H \\\
    UniGB-UCS2-V \\\
    UniGB-UTF16-H \\\
    UniGB-UTF16-V \\\
    UniGB-UTF32-H \\\
    UniGB-UTF32-V \\\
    UniGB-UTF8-H \\\
    UniGB-UTF8-V

%define EncodeList_CNS \\\
    Adobe-CNS1-0 \\\
    Adobe-CNS1-1 \\\
    Adobe-CNS1-2 \\\
    Adobe-CNS1-3 \\\
    Adobe-CNS1-4 \\\
    Adobe-CNS1-5 \\\
    Adobe-CNS1-6 \\\
    B5-H \\\
    B5-V \\\
    B5pc-H \\\
    B5pc-V \\\
    CNS-EUC-H \\\
    CNS-EUC-V \\\
    CNS1-H \\\
    CNS1-V \\\
    CNS2-H \\\
    CNS2-V \\\
    ETHK-B5-H \\\
    ETHK-B5-V \\\
    ETen-B5-H \\\
    ETen-B5-V \\\
    ETenms-B5-H \\\
    ETenms-B5-V \\\
    HKdla-B5-H \\\
    HKdla-B5-V \\\
    HKdlb-B5-H \\\
    HKdlb-B5-V \\\
    HKgccs-B5-H \\\
    HKgccs-B5-V \\\
    HKm314-B5-H \\\
    HKm314-B5-V \\\
    HKm471-B5-H \\\
    HKm471-B5-V \\\
    HKscs-B5-H \\\
    HKscs-B5-V \\\
    Identity-H \\\
    Identity-V \\\
    UniCNS-UCS2-H \\\
    UniCNS-UCS2-V \\\
    UniCNS-UTF16-H \\\
    UniCNS-UTF16-V \\\
    UniCNS-UTF32-H \\\
    UniCNS-UTF32-V \\\
    UniCNS-UTF8-H \\\
    UniCNS-UTF8-V

%define EncodeList_Korea \\\
    Adobe-Korea1-0 \\\
    Adobe-Korea1-1 \\\
    Adobe-Korea1-2 \\\
    Identity-H \\\
    Identity-V \\\
    KSC-EUC-H \\\
    KSC-EUC-V \\\
    KSC-H \\\
    KSC-Johab-H \\\
    KSC-Johab-V \\\
    KSC-RKSJ-H \\\
    KSC-V \\\
    KSCms-UHC-H \\\
    KSCms-UHC-HW-H \\\
    KSCms-UHC-HW-V \\\
    KSCms-UHC-V \\\
    KSCpc-EUC-H \\\
    KSCpc-EUC-V \\\
    UniKS-UCS2-H \\\
    UniKS-UCS2-V \\\
    UniKS-UTF16-H \\\
    UniKS-UTF16-V \\\
    UniKS-UTF32-H \\\
    UniKS-UTF32-V \\\
    UniKS-UTF8-H \\\
    UniKS-UTF8-V

Summary: Hiragino Japanese/English OpenType Font Basic 6 fonts
Summary(ja): 日本語 ヒラギノ OpenType フォント(基本6書体)
Name: OpenType-hiragino
Version: 10.7
Release: 3%{?_dist_release}
BuildArch: noarch

Source0: DUMMY
# Source0: HiraKakuPro-W3.otf
# Source1: HiraKakuPro-W6.otf
# Source2: HiraKakuStd-W8.otf
# Source3: HiraMaruPro-W4.otf
# Source4: HiraMinPro-W3.otf
# Source5: HiraMinPro-W6.otf
# Source10: HiraKakuProN-W3.otf
# Source11: HiraKakuProN-W6.otf
# Source12: HiraKakuStdN-W8.otf
# Source13: HiraMaruProN-W4.otf
# Source14: HiraMinProN-W3.otf
# Source15: HiraMinProN-W6.otf
# %if %{?extras:1} %{!?extras:0}
# Source20: HiraginoSansGB-W3.otf
# Source21: HiraginoSansGB-W6.otf
# Source22: HiraginoSansCNS-W3.otf
# Source23: HiraginoSansCNS-W6.otf
# %endif

%if %{?nosrc:1} %{!?nosrc:0}
NoSource: 0
# NoSource: 1
# NoSource: 2
# NoSource: 3
# NoSource: 4
# NoSource: 5
# NoSource: 10
# NoSource: 11
# NoSource: 12
# NoSource: 13
# NoSource: 14
# NoSource: 15
# %if %{?extras:1} %{!?extras:0}
# NoSource: 20
# NoSource: 21
# NoSource: 22
# NoSource: 23
# %endif
%endif

License: DAINIPPON SCREEN MFG. CO., LTD.
URL: http://www.screen.co.jp/
Group: User Interface/X
Buildroot: %{_tmppath}/%{name}-%{version}-%{release}-root

Obsoletes: OTF-Hiragino

Requires(post): fontconfig ghostscript
Requires(postun): fontconfig ghostscript 

Distribution: Vine Linux
Vendor: Project Vine

%description
Hiragino basic 6 Japanese OpenType Font using in MacOS X 10.7. 
If you want to use Hiragion OpneType Font on X and TeX Live
((p)xdvi, (p)dvips, dvipdfmx), Ghostscript, 
you build and install this package.
To build this package, you copy Hiragino from MacOS X (or package 
which you bought) to ~/rpm/SOURCES and rename the fonts name to 
"HiraKakuPro-W3.otf" "HiraKakuPro-W6.otf" "HiraKakuStd-W8.otf" 
"HiraMaruPro-W4.otf" "HiraMinPro-W3.otf" "HiraMinPro-W6.otf"
"HiraKakuProN-W3.otf" "HiraKakuProN-W6.otf" "HiraKakuStdN-W8.otf" 
"HiraMaruProN-W4.otf" "HiraMinProN-W3.otf" "HiraMinProN-W6.otf" 
("HiraginoSansGB-W3.otf" "HiraginoSansGB-W6.otf"
"HiraginoSansCNS-W3.otf" "HiraginoSansCNS-W6.otf"). 
Then you type 
$ rpm -bb ~/rpm/SPEC/%{name}-vl.spec
(If you have HiraginoSansGB and HiraginoSansCNS fonts, 
$ rpm -bb --define extras:1 ~/rpm/SPEC/%{name}-vl.spec)
$ sudo apt-get install ~/rpm/RPMS/noarch/%{name}-%{version}-%{release}.noarch.rpm


%description -l ja
MacOS X 10.7 に付属する、或は購入したパッケージのヒラギノ基本6書体 
日本語 OpenType フォントを X や TeX Live ((p)xdvi, (p)dvips, dvipdfmx)、
Ghostscript で使用したい場合は
このパッケージを構築してインストールして下さい。
このパッケージを構築するには MacOS X からヒラギノを 
~/rpm/SOURCES にコピーしてフォント名を
"HiraKakuPro-W3.otf" "HiraKakuPro-W6.otf" "HiraKakuStd-W8.otf"
"HiraMaruPro-W4.otf" "HiraMinPro-W3.otf" "HiraMinPro-W6.otf"
"HiraKakuProN-W3.otf" "HiraKakuProN-W6.otf" "HiraKakuStdN-W8.otf" 
"HiraMaruProN-W4.otf" "HiraMinProN-W3.otf" "HiraMinProN-W6.otf" 
("HiraginoSansGB-W3.otf" "HiraginoSansGB-W6.otf"
"HiraginoSansCNS-W3.otf" "HiraginoSansCNS-W6.otf")
に変更して下さい。
次に、
$ rpm -bb ~/rpm/SPEC/%{name}-vl.spec
(もし HiraginoSansGB, HiraginoSansCNS フォントを持っている場合、 
$ rpm -bb --define extras:1 ~/rpm/SPEC/%{name}-vl.spec)
$ sudo apt-get install ~/rpm/RPMS/noarch/%{name}-%{version}-%{release}.noarch.rpm
とタイプして下さい。


%prep

%build

%install
%__rm -rf %{buildroot}
%__mkdir -p %{buildroot}%{xfontdir}
%__mkdir -p %{buildroot}%{gsfontdir}
%__mkdir -p %{buildroot}%{gsfontspecdir}
%__mkdir -p %{buildroot}%{texfontdir}
%__install -p -m 644 \
    %{SOURCE0} %{SOURCE1} %{SOURCE2} %{SOURCE3} %{SOURCE4} %{SOURCE5} \
    %{SOURCE10} %{SOURCE11} %{SOURCE12} %{SOURCE13} %{SOURCE14} %{SOURCE15} \
    %if %{?extras:1} %{!?extras:0}
        %{SOURCE20} %{SOURCE22} %{SOURCE22} %{SOURCE23} \
    %endif
    %{buildroot}%{xfontdir}


## mkgsfontspec [fontname] [encode] > [fontspec]
mkgsfontspec(){
    local fontname=$1
    local encode=$2
	cat <<EOT
%%!PS-Adobe-3.0 Resource-Font
%%%%DocumentNeededResources: ${encode} (CMap)
%%%%IncludeResource: ${encode} (CMap)
%%%%BeginResource: Font (${fontname}-${encode})
(${fontname}-${encode})
(${encode}) /CMap findresource
[(${fontname}) /CIDFont findresource]
composefont
pop
%%%%EndResource
%%%%EOF
EOT
}

for i in %{FontList}; do
    fnt=$(echo $i | %__sed -e "s/,.*$//")
    enc=$(echo $i | %__sed -e "s/^.*,//")

    case $enc in
    	 Japan)	enclist="%{EncodeList_Japan}";;
	 GB)	enclist="%{EncodeList_GB}";;
	 CNS)	enclist="%{EncodeList_CNS}";;
	 Korea)	enclist="%{EncodeList_Korea}";;
	 *)	exit 1;;
    esac

    %__ln_s -f %{xfontdir}/${fnt}.otf %{buildroot}%{texfontdir}/${fnt}.otf
    %__ln_s -f %{xfontdir}/${fnt}.otf %{buildroot}%{gsfontdir}/${fnt}

    for j in $enclist; do
    	mkgsfontspec ${fnt} ${j} > %{buildroot}%{gsfontspecdir}/${fnt}-${j}
    done
done


%post
%__font_install_post %{xfontdir}

%touch_run texhash
exit 0

%postun
%__font_install_postun %{xfontdir}

if [ "$1" = 0 ]; then
   %touch_run texhash
fi
exit 0

%posttrans
%{exec_texhash}
exit 0

%clean
%__rm -rf %{buildroot}

%files
%defattr(-,root,root)
#%doc README*
%{xfontdir}/*
%{gsfontdir}/*
%{gsfontspecdir}/*
%{texfontdir}/*

%changelog
* Sat Nov 24 2012 Munehiro Yamamoto <munepi@vinelinux.org> 10.7-3
- TeX Live 2012
- change %%texfontdir

* Thu Mar  1 2012 Munehiro Yamamoto <munepi@vinelinux.org> 10.7-2
- dropped Requires(post): chkfontpath

* Sun Nov 27 2011 Munehiro Yamamoto <munepi@vinelinux.org> 10.7-1
- TeX Live 2009

* Sat Jun 11 2011 Munehiro Yamamoto <munepi@vinelinux.org> 10.6-1
- supported Vine Linux 6 + TeX Live 2009
- added Obsoletes: OTF-Hiragino < 10.6
- supported HiraginoSansGB, HiraginoSansCNS

* Sun Jun 07 2009 Munehiro Yamamoto <munepi@vinelinux.org> 10.5-2
- setup for gsversion 8.71

* Sun Jun 07 2009 Munehiro Yamamoto <munepi@cg8.so-net.ne.jp> 10.5-1
- spec in utf8
- applied new versioning policy
- s/Copyright/License/
- corrected %%description
- setup for Ghostscript 8.64
  - created many font spec files

* Sat Jun 18 2005 KOBAYASHI Taizo <tkoba@vinelinux.org> 10.4-0vl1
- modified for teTeX3

* Thu Apr 08 2004 KOBAYASHI R. Taizo <tkoba@vinelinux.org> 10.3-0vl4
- fix postun

* Fri Apr 02 2004 KOBAYASHI R. Taizo <tkoba@ike-dyn.ritsumei.ac.jp> 10.3-0vl3
- added target "auto" for updmap-otf v0.2

* Sun Mar 28 2004 KOBAYASHI R. Taizo <tkoba@ike-dyn.ritsumei.ac.jp> 10.3-0vl2
- use updmap-otf

* Wed Feb 05 2004 KOBAYASHI R. Taizo <tkoba@ike-dyn.ritsumei.ac.jp> 10.3-0vl1
- modified for Vine

* Sat Jul 05 2003 KOBAYASHI R. Taizo <tkoba@ike-dyn.ritsumei.ac.jp> 10.2-0il1
- initial release for Hiragino OpenType Font.