#!/bin/bash
# tlpdb2rpmspec
# 
# Copyright 2010-2017 Munehiro Yamamoto <munepi@vinelinux.org>
# This file is licensed under the GNU General Public License version 2
# or any later version.

Usage(){
    cat<<EOF
$(basename $0) 2017
Usage:	$(basename $0) [option] [pkgname]

TeX Live Package Database (a file named texlive.tlpdb) to RPM spec converter
This script generates a RPM spec file of CTAN and collection-* packages 
contained in TeX Live 2017. 

Options:
	--name:			return [pkgname]
	--category:		return the category of [pkgname]
	--revision:		return the revision of [pkgname]
	--depend:		return dependencies of [pkgname]
	--shortdesc:		return the short description of [pkgname]
	--longdesc:		return the description of [pkgname]
	--execute:		return post processe of [pkgname]
	--catalogue-ctan:	return the locate of [pkgname]
	--catalogue-date:	return the last update of [pkgname]
	--catalogue-license:	return the license of [pkgname]
	--catalogue-version:	return the version of [pkgname]
	--filelist:		return the filelist of [pkgname]
	--help:			show this help

Supoort collection-* packages: 
$(egrep "^name collection-" $TLPDB | sed -e "s,name ,,g" | grep -v -e "texworks" -e "wintools")
EOF
}

check-parameter(){
    [ -z "$*" ] && Usage && return 1

    while [ ! -z "$*" ]; do
	case $1 in
	    --name|--category|--revision|--depend|--shortdesc|--longdesc|--execute|--catalogue-ctan|--catalogue-date|--catalogue-license|--catalogue-version|--filelist)
		[ $with_option -eq 1 ] && \
		    $__echo "E: you only give one option" && return 1
		with_option=1
		;;
	    --help)
		Usage
		return 1
		;;
	    --minimal-collections|--standard-collections|--full-collections)
		;;
	    *)
		[ -z "$(egrep -n "^name $1$" $TLPDB)" ] && \
		    $__echo "E: unknown option or package: $1" && return 1
		;;
	esac
	shift
    done

    return 0
}


## tlpkg4a [--name|--category|--revision|--depend|--shortdesc|--longdesc|--execute|--catalogue-ctan|--catalogue-date|--catalogue-license|--catalogue-version|--filelist] [pkgname]
tlpkg4a(){
    [ $# -eq 2 ] || return 1
    local opt=$1
    local pkg=$2
    local fieldname=$($__echo $opt | sed -e "s,--,,")

    ## check param
    case $opt in
	--name|--category|--revision|--depend|--shortdesc|--longdesc|--execute|--catalogue-ctan|--catalogue-date|--catalogue-license|--catalogue-version|--filelist)
	    ;;
	*)
	    $__echo "E: unknown option: $opt"
	    exit 1
	    ;;
    esac

    ## get the head line of $pkg record
    pkg_LINE=$(egrep -n "^name $pkg$" $TLPDB | cut -d":" -f 1)

    ## read tlpdb
    is_pkg=0
    (
    	[ "$opt" = "--filelist" ] && \
    	    tail -n $(( $TLPDB_MAXLINE - $pkg_LINE + 1 )) $TLPDB || \
    	    tail -n $(( $TLPDB_MAXLINE - $pkg_LINE + 1 )) $TLPDB | egrep -v -e "^ "
    ) | \
	while read field; do
	    ## find the record of $pkg
	    $__echo "$field" | egrep -q "^name $pkg" && is_pkg=1
	    [ $is_pkg -eq 0 ] && continue
	    
	    ## return the values of its field name
	    if [ "$opt" = "--filelist" ]; then
	    ## NOTE: we only need texmf-dist directories
	    ##       we replace RELOC with texmf-dist
		$__echo "$field" | \
		    egrep -e "^texmf-dist/" -e "^RELOC/" | \
		    sed -e "s,^RELOC,texmf-dist," \
		        -e "s, details=.*,," -e "s, language=.*,,"
	    else
		$__echo "$field" | \
		    egrep "^${fieldname} " | \
		    sed -e "s,${fieldname} ,,"
	    fi
	    
	    ## end of the record of $pkg
	    $__echo "$field" | egrep -q "^[[:blank:]]*$" && break
        done
    
    return 0
}

## tlpkg2speclicense [pkgname]
tlpkg2speclicense(){
    [ $# -eq 1 ] || return 1
    local pkg=$1

    case $(tlpkg4a --catalogue-license $pkg) in
	gpl3) $__echo "GPLv3+";;
	gpl2) $__echo "GPLv2+";;
	gpl) $__echo "GPL+";;
	lppl1.3) $__echo "LPPL 1.3";;
	lppl1.2) $__echo "LPPL 1.2";;
	lppl|lppl1) $__echo "LPPL";;
	other-free) $__echo "Freely redistributable without restriction";;
	pd) $__echo "Public Domain";;
	noinfo) $__echo "No Info";;
	lgpl|lgpl2.1) $__echo "LGPLv2+";;
	gfsl) $__echo "LPPL";;
	bsd) $__echo "BSD";;
	knuth) $__echo "Knuth";;
	unknown) $__echo "Unknown";;
	gfl) $__echo "LPPL";;
	artistic2) $__echo "Artistic 2.0";;
	fdl) $__echo "GFDL";;
	collection) $__echo "Public Domain";;
	artistic) $__echo "Artistic";;
	other-nonfree) $__echo "Other";;
	other) $__echo "Other";;
	ofl) $__echo "OFSFLD";;
	apache2) $__echo "ASL 2.0";;
	nosource) $__echo "No Source";;
	nosell) $__echo "No Sell";;
	nocommercial) $__echo "Non-commercial";;
	*) return 1;;
    esac
    return 0
}

## tlpkg2archivelist [pkgname]
tlpkg2archivelist(){
    [ $# -eq 1 ] || return 1
    local pkg=$1
    local pkgdeps="$(tlpkg4a --depend $pkg)"
    local i=

    ## if $pkg is not collection-*, only return the filelist of $pkg
    $__echo $pkg | egrep -q "^collection-" || return 0

    ##!! we need pure filelist of $pkg; remove collection-*
    pkgdeps=$($__echo $pkgdeps | sed -e "s,collection-[-A-Za-z0-9]*,,g")

    [ -z "$pkgdeps" ] && return 0
    for i in $pkgdeps; do 
    	ls ${TLNETROOT}/archive/${i}.tar.xz ${TLNETROOT}/archive/${i}.*.tar.xz 2>/dev/null | grep -v -e alpha-linux -e amd64-freebsd -e amd64-kfreebsd -e amd64-netbsd -e armel-linux -e armhf-linux -e i386-cygwin -e i386-freebsd -e i386-kfreebsd -e i386-linux -e i386-netbsd -e i386-solaris -e mips-irix -e mipsel-linux -e powerpc-linux -e sparc-solaris -e universal-darwin -e win32 -e x86_64-cygwin -e x86_64-darwin -e x86_64-linux -e x86_64-solaris | sed -e "s,.*/,,g"
    done

    return 0
}

## tlpkg2manifest [pkgname]
tlpkg2manifest(){
    [ $# -eq 1 ] || return 1
    local pkg=$1
    local pkgdeps="$(tlpkg4a --depend $pkg)"
    local i=

    tlpkg4a --filelist $pkg || return 1

    ## if $pkg is not collection-*, only return the filelist of $pkg
    $__echo $pkg | egrep -q "^collection-" || return 0

    ##!! we need pure filelist of $pkg; remove collection-*
    pkgdeps=$($__echo $pkgdeps | sed -e "s,collection-[-A-Za-z0-9]*,,g")

    [ -z "$pkgdeps" ] && return 0
    for i in $pkgdeps; do 
    	tlpkg4a --filelist $i || return 1 
    done

    return 0
}

## tlpkg2maplist [pkgname]
tlpkg2maplist(){
    [ $# -eq 1 ] || return 1
    local pkg=$1
    local pkgdeps="$(tlpkg4a --depend $pkg)"
    local i=

    tlpkg4a --execute $pkg | grep -e "Map" | sed -e "s,^add,,g"

    ##!! we need pure filelist of $pkg; remove collection-*
    pkgdeps=$($__echo $pkgdeps | sed -e "s,collection-[-A-Za-z0-9]*,,g")

    [ -z "$pkgdeps" ] && return 0
    for i in $pkgdeps; do 
    	tlpkg4a --execute $i | grep -e "Map" | sed -e "s,^add,,g"
    done

    return 0
}

## tlpkg2inilist [pkgname]
tlpkg2inilist(){
    [ $# -eq 1 ] || return 1
    local pkg=$1
    local pkgdeps="$(tlpkg4a --depend $pkg)"
    local i=

    ## search AddFormat, AddHypen
    tlpkg4a --execute $pkg | grep -e "Add"

    ##!! we need pure filelist of $pkg; remove collection-*
    pkgdeps=$($__echo $pkgdeps | sed -e "s,collection-[-A-Za-z0-9]*,,g")

    [ -z "$pkgdeps" ] && return 0
    for i in $pkgdeps; do 
    	tlpkg4a --execute $i | grep -e "Add"
    done

    return 0
}

## mkrpmspec [pkgname]
mkrpmspec(){
    [ $# -eq 1 ] || return 1
    local pkg=$1
    local i=

    RPM_SUMMARY="TeX Live: $(tlpkg4a --shortdesc $pkg)"

    ## Requires tag for texlive
    RPM_REQUIRES=$(for i in $(tlpkg4a --depend $pkg | egrep "^collection-"); do \
	$__echo "Requires: texlive-$i = %{version}"; \
	done)

    ## Requires tag for external dependencies
    [ ! -z "$ASYMPTOTE_PACKAGE" ] && \
	[ ! -z "$(tlpdb2rpmspec --depend $pkg | egrep "^asymptote$")" ] && \
	RPM_REQUIRES="${RPM_REQUIRES}
Requires: $ASYMPTOTE_PACKAGE"
    [ ! -z "$DETEX_PACKAGE" ] && \
	[ ! -z "$(tlpdb2rpmspec --depend $pkg | egrep "^detex$")" ] && \
	RPM_REQUIRES="${RPM_REQUIRES}
Requires: $DETEX_PACKAGE"
    [ ! -z "$DVIPNG_PACKAGE" ] && \
	[ ! -z "$(tlpdb2rpmspec --depend $pkg | egrep "^dvipng$")" ] && \
	RPM_REQUIRES="${RPM_REQUIRES}
Requires: $DVIPNG_PACKAGE"
    [ ! -z "$LATEXMK_PACKAGE" ] && \
	[ ! -z "$(tlpdb2rpmspec --depend $pkg | egrep "^latexmk$")" ] && \
	RPM_REQUIRES="${RPM_REQUIRES}
Requires: $LATEXMK_PACKAGE"
    [ ! -z "$LCDF_TYPETOOLS_PACKAGE" ] && \
	[ ! -z "$(tlpdb2rpmspec --depend $pkg | egrep "^lcdftypetools$")" ] && \
	RPM_REQUIRES="${RPM_REQUIRES}
Requires: $LCDF_TYPETOOLS_PACKAGE"
    [ ! -z "$PSUTILS_PACKAGE" ] && \
	[ ! -z "$(tlpdb2rpmspec --depend $pkg | egrep "^psutils$")" ] && \
	RPM_REQUIRES="${RPM_REQUIRES}
Requires: $PSUTILS_PACKAGE"
    [ ! -z "$T1UTILS_PACKAGE" ] && \
	[ ! -z "$(tlpdb2rpmspec --depend $pkg | egrep "^t1utils$")" ] && \
	RPM_REQUIRES="${RPM_REQUIRES}
Requires: $T1UTILS_PACKAGE"

    ## License tag
    RPM_LICENSE="$(tlpkg2speclicense $pkg),"
    for i in $(tlpkg4a --depend $pkg | egrep -v "^collection-"); do 
	tmp=$(tlpkg2speclicense $i)
	$__echo "$RPM_LICENSE" | grep -q "${tmp},"
	[ $? -eq 1 ] && RPM_LICENSE="${RPM_LICENSE} ${tmp},"
    done
    RPM_LICENSE=$($__echo "$RPM_LICENSE" | sed -e "s/,$//" | sed -e "s/^, //")
    [ -z "$RPM_LICENSE" ] && RPM_LICENSE=distributable

    PKG_SHORTDESC=$(tlpkg4a --shortdesc $pkg)
    PKG_LONGDESC=$(tlpkg4a --longdesc $pkg)

    PKG_CTANPKGSLIST=$(for i in $(tlpkg4a --depend $pkg); do \
	if [ -z "$($__echo "$i" | grep "collection-")" ]; then \
            $__echo -n "$i: "; \
            tmp=$(tlpkg4a --shortdesc $i); \
            [ -z "${tmp}" ] && $__echo || $__echo "$tmp"; \
	fi
	done)

    PKG_ARCHIVELIST=$(tlpkg2archivelist $pkg)
    [ -z "${PKG_ARCHIVELIST}" ] && \
	$__echo "W: empty archive list: $pkg" >&2

    ## check whether or not $pkg is collection-lang*****
    with_colllang=0
    $__echo $pkg | egrep -q "^collection-lang" && \
        with_colllang=1

    ## check to need the subpackage %{name}-doc
    with_docpkg=0
    for i in ${PKG_ARCHIVELIST}; do 
	[ ! -z "$($__echo "$i" | egrep "\.doc\.")" ] && \
	    with_docpkg=1 && break
    done

    with_maplist=0
    MAPLIST="$(tlpkg2maplist $pkg)"
    [ ! -z "$MAPLIST" ] && with_maplist=1
    ##$__echo $with_maplist && $__echo "$MAPLIST" && exit

    with_inilist=0
    INILIST="$(tlpkg2inilist $pkg)"
    [ ! -z "$INILIST" ] && with_inilist=1
    ##$__echo $with_inilist && $__echo "$INILIST" && exit

cat<<EOF
## -*- coding: utf-8-unix -*-
## NOTE: This spec file is generated by $(basename $0) ${VERSION}-${RELEASE}: 
## $(basename $0) $pkg

%global _use_internal_dependency_generator 0
%global __find_provides %{nil}
%global __find_requires %{nil}

Summary: ${RPM_SUMMARY}
Summary(ja): ${RPM_SUMMARY}
Name: texlive-$pkg
Version: ${VERSION}
Release: ${RELEASE}%{?_dist_release}
License: ${RPM_LICENSE}
Group: Applications/Publishing
URL:http://www.tug.org/texlive/

$(x=(${PKG_ARCHIVELIST}); for (( i=0; i < ${#x[@]}; ++i )); do $__echo Source${i}: ${x[$i]}; done)

Requires: texlive = %{version}
$RPM_REQUIRES

Requires(post):		texlive = %{version}
Requires(postun):	texlive = %{version}

BuildArch:	noarch
Buildroot:	%{_tmppath}/%{name}-%{version}-root

Vendor:		${RPM_VENDOR}
Distribution:	${RPM_DISTRIBUTION}
Packager:	${RPM_PACKAGER}

%description
The TeX Live software distribution offers a complete TeX system for a
variety of Unix, Macintosh, Windows and other platforms. It
encompasses programs for editing, typesetting, previewing and printing
of TeX documents in many different languages, and a large collection
of TeX macros and font libraries.

The distribution includes extensive general documentation about TeX,
as well as the documentation for the included software packages.

This package is a collection of ${PKG_SHORTDESC}: 
${PKG_LONGDESC}

This package contains the following CTAN packages: 
${PKG_CTANPKGSLIST}

%description -l ja
TeX Live ソフトウェアディストリビューションは、
さまざまな Unix, Macintosh, Windows、および
他のプラットホームに対して完全な TeX システムを提供します。
多くの異なった言語を含む TeX ドキュメントの
編集、組版、閲覧、印刷するためのプログラム、
そして、TeX マクロやフォントライブラリの大きなコレクションを
同梱しています。

このディストリビューションは
同梱しているソフトウェアパッケージのためのドキュメントばかりでなく、
TeX に関するたくさんの一般的なドキュメントを含んでいます。

このパッケージは以下のようなパッケージ集です。
${PKG_SHORTDESC}: 
${PKG_LONGDESC}

このパッケージは以下の CTAN パッケージを含んでいます： 
${PKG_CTANPKGSLIST}

EOF

## subpackage: %{name}-doc
if [ $with_docpkg -eq 1 ]; then
    cat<<EOF
%package doc
Summary: TeX Live: Documentation files of %{name}
Group: Applications/Publishing
Requires: %{name} = %{version}-%{release}

%description doc
This package contains documentation files of %{name}.

EOF
fi

cat<<EOF
%prep
%setup -c -n %{name}-%{version}
$(x=(${PKG_ARCHIVELIST}); for (( i=1; i < ${#x[@]}; ++i )); do $__echo %__tar -xvf %{SOURCE${i}}; done)

%build

%install
[ -n "%{buildroot}" -a "%{buildroot}" != / ] && %__rm -rf %{buildroot}

%__mkdir_p %{buildroot}%{_datadir}

## move texmf-dist to /usr/share/texmf-dist
[ -d texmf-dist ] && %__mv texmf-dist %{buildroot}%{_tl_texmfdist}/ ||:

## move texmf to /usr/share/texmf
[ -d texmf ] && %__mv texmf %{buildroot}%{_tl_texmfmain}/ ||:

## move tlpkg to /usr/share/tlpkg
[ -d tlpkg ] && %__mv tlpkg %{buildroot}%{_datadir}/ ||:

## move all non-arch binaries to /usr/bin
[ -d bin ] && %__mv bin %{buildroot}%{_prefix}/ ||:

## move others into /usr/share/texmf-dist
%__mkdir_p %{buildroot}%{_tl_texmfdist}
%__cp -a * %{buildroot}%{_tl_texmfdist}/ ||:
%__rm -rf * ||:


## remove duplicated files between texlive and texlive-collection-*
## NOTE: We provides texmf.cnf and updmap{-hdr,}.cfg from texlive
## remove unpacked files
x=(
dvipdfmx/dvipdfmx.cfg
scripts/context/stubs/unix/mtxrun
scripts/latexmk/latexmk.pl
scripts/ptex2pdf/ptex2pdf.lua
scripts/texlive/fmtutil.pl
scripts/texlive/texconfig-dialog.sh 
scripts/texlive/texconfig-sys.sh 
scripts/texlive/texconfig.sh 
scripts/texlive/texlinks.sh 
scripts/texlive/tlmgr.pl 
scripts/texlive/updmap.pl
texconfig/tcfmgr 
web2c/fmtutil.cnf
web2c/mktex.opt 
web2c/mktexdir 
web2c/mktexdir.opt 
web2c/mktexnam 
web2c/mktexnam.opt 
web2c/mktexupd 
web2c/texmf.cnf 
web2c/updmap-hdr.cfg 
web2c/updmap.cfg 
install-tl
#
scripts/cjk-gs-integrate/cjk-gs-integrate.pl 
scripts/epspdf/epspdf.tlu 
scripts/epspdf/epspdftk.tcl 
scripts/glossaries/makeglossaries 
scripts/jfontmaps/kanji-config-updmap.pl 
scripts/jfontmaps/kanji-fontmap-creator.pl 
scripts/kotex-utils/jamo-normalize.pl 
scripts/kotex-utils/komkindex.pl 
scripts/crossrefware/ltx2crossrefxml.pl 
scripts/rubik/rubikrotation.pl 
scripts/musixtex/musixtex.lua 
scripts/pmxchords/pmxchords.lua 
scripts/ctanify/ctanify 
scripts/dtxgen/dtxgen 
scripts/findhyph/findhyph 
scripts/latexpand/latexpand 
scripts/ltxfileinfo/ltxfileinfo 
scripts/latexdiff/latexdiff-vc.pl 
scripts/latexdiff/latexdiff.pl 
scripts/texfot/texfot.pl 
scripts/fontools/autoinst 
scripts/luaotfload/luaotfload-tool.lua 
scripts/oberdiek/pdfatfi.pl 
fonts/map/glyphlist/glyphlist.txt ## texlive-collection-basic-2017-1vl7.noarch
scripts/tex4ht/xhlatex.sh ## texlive-collection-plaingeneric-2017-1vl7.noarch
scripts/latexindent/latexindent.pl ## texlive-collection-binextra-2017-1vl7.noarch
scripts/texosquery/texosquery-jre5.sh ## texlive-collection-binextra-2017-1vl7.noarch
scripts/texosquery/texosquery-jre8.sh ## texlive-collection-binextra-2017-1vl7.noarch
scripts/texosquery/texosquery.sh ## texlive-collection-binextra-2017-1vl7.noarch
scripts/lwarp/lwarpmk.lua ## texlive-collection-latexrecommended-2017-2vl7.noarch
)
if [ -d %{buildroot}%{_tl_texmfdist} ]; then
pushd %{buildroot}%{_tl_texmfdist}
%__rm -f \${x[@]} ||:
popd
fi

EOF

cat<<EOF
## Files list
find %{buildroot} -type f -or -type l | \\
    %__sed -e "s|%{buildroot}||g" > filelist.full

find %{buildroot}%{_tl_texmfdist} -type d | \\
    %__sed -e "s|^%{buildroot}|%dir |" \\
           -e "s|\$|/|"             >> filelist.full

EOF

## subpackage: %{name}-doc
if [ $with_docpkg -eq 1 ]; then
    cat<<EOF
## subpackages
grep "/texmf-dist/doc/" filelist.full > filelist.doc
cat filelist.doc filelist.full | sort | uniq -u > filelist.tmp
%__mv -f filelist.tmp filelist.full

EOF
fi

cat<<EOF
%clean
%__rm -rf %{buildroot}

%post
%_tl_touch_run texhash
%_tl_touch_run mtxrun

EOF

## Running updmap
if [ $with_maplist -eq 1 ]; then
    cat<<EOF
[ -f %{_tl_texmfdist}/web2c/updmap.cfg ] || exit 0

$($__echo "$MAPLIST" | while read maptype map; do \
    cat<<EOT
    %_tl_enable_map ${maptype} ${map}
EOT
done)

EOF
fi

## Running fmtutil
if [ $with_inilist -eq 1 ]; then
    cat<<EOF
%_tl_touch_run fmtutil

EOF
fi

cat<<EOF
exit 0


%postun
if [ "\$1" = 0 ]; then
    %_tl_touch_run texhash

EOF

## Running updmap
if [ $with_maplist -eq 1 ]; then
    cat<<EOF
    [ -f %{_tl_texmfdist}/web2c/updmap.cfg ] || exit 0

$($__echo "$MAPLIST" | while read maptype map; do \
    cat<<EOT
    %_tl_disable_map ${map}
EOT
done)

EOF
fi

cat<<EOF
fi

exit 0

%posttrans
%{_tl_exec_texhash}
%{_tl_exec_mtxrun}
%{_tl_exec_updmap}
%{_tl_exec_fmtutil}
exit 0


%files -f filelist.full
%defattr(-,root,root,-)

EOF
## subpackage: %{name}-doc
if [ $with_docpkg -eq 1 ]; then
    cat<<EOF
%files -f filelist.doc doc
%defattr(-,root,root,-)

EOF
fi

cat<<EOF
%changelog
$(cat /usr/share/texlive-vtlpkg/changelog)
EOF

    return 0
}

## mkrpmcollection [--minimal-collections|--standard-collections|--full-collections]
mkrpmcollection(){
    local category=$($__echo $1 | sed -e "s/--\([a-z]*\)-collections/\1/")
    local category_pkglist=
    local macro_sourcedir="%_sourcedir ${TLNETROOT}/archive"
    local i=

    case $category in 
	minimal|standard)
	    category_pkglist=$(grep -e "${category}," $CATEGORYLIST | sed -e "s/${category},//g" | sed -e "s/,$//g")
	    ;;
	full)
	    category_pkglist=$(Usage | egrep "^collection-")
	    ;;
	*)
	    $__echo "E: unknown category: $category"
	    return 1
	    ;;
    esac

    for i in ${category_pkglist}; do 
        $__echo "making texlive-${i} ..."
        if [ -f texlive-${i}-${RPM_SPEC_SUFFIX}.spec ]; then
            rpm -q --specfile --changelog texlive-${i}-${RPM_SPEC_SUFFIX}.spec >/dev/null 2>&1
            if [ $? -eq 0 ]; then
                $__echo "W: already exists: texlive-${i}-${RPM_SPEC_SUFFIX}.spec" >&2
                continue
            fi
        fi
	mkrpmspec $i > texlive-${i}-${RPM_SPEC_SUFFIX}.spec
	if [ $? -eq 1 ]; then 
	    $__echo "texlive-${i}-${RPM_SPEC_SUFFIX}.spec: "
	    cat texlive-${i}-${RPM_SPEC_SUFFIX}.spec
	    rm -f texlive-${i}-${RPM_SPEC_SUFFIX}.spec
	    continue
	fi
	rpmbuild -ba --define="${macro_sourcedir}" texlive-${i}-${RPM_SPEC_SUFFIX}.spec || return 1
    done

    return 0
}

setup-tlpdb2rpmspec(){
    __echo=$(which echo 2>/dev/null)
    [ ! -f $__echo ] && return 1

    ## set external dependencies
    ASYMPTOTE_PACKAGE=
    DETEX_PACKAGE=
    DVIPNG_PACKAGE=
    LATEXMK_PACKAGE=
    LCDF_TYPETOOLS_PACKAGE=
    PSUTILS_PACKAGE=
    T1UTILS_PACKAGE=

    ## set local tlnet
    TLNETROOT=/var/local/tlnet
    TLPDB=/var/local/tlnet/tlpkg/texlive.tlpdb

    ## load your .vtlpkg.conf
    if [ -f ${HOME}/.vtlpkg.conf ]; then
	. ${HOME}/.vtlpkg.conf
    else
	$__echo "E: ${HOME}/.vtlpkg.conf: No such file"
	return 1
    fi

    [ -z "$RPM_SPEC_SUFFIX" ] && \
	$__echo "E: \$RPM_SPEC_SUFFIX is empty" && return 1
    [ -z "$RPM_VENDOR" ] && \
	$__echo "E: \$RPM_VENDOR is empty" && return 1
    [ -z "$RPM_DISTRIBUTION" ] && \
	$__echo "E: \$RPM_DISTRIBUTION is empty" && return 1
    [ -z "$RPM_GPG_NAME" ] && \
	$__echo "E: \$RPM_GPG_NAME is empty" && return 1
    [ -z "$RPM_PACKAGER" ] && \
	$__echo "E: \$RPM_PACKAGER is empty" && return 1

    ## setup configurations
    VERSION=2017
    RELEASE=3

    ## check texlive.tlpdb
    [ ! -f $TLPDB ] && \
	$__echo "E: \$TLPDB: No such file" && return 1
    TLPDB_MAXLINE=$(wc -l $TLPDB | awk '{print $1}')

    ## category of collection-*
    CATEGORYLIST=/usr/share/texlive-vtlpkg/category.list

    ## set some booleans
    with_option=0
}

##############################################################################

setup-tlpdb2rpmspec || exit 1

check-parameter $* || exit 1

case $1 in
    --name|--category|--revision|--depend|--shortdesc|--longdesc|--execute|--catalogue-ctan|--catalogue-date|--catalogue-license|--catalogue-version|--filelist)
	tlpkg4a $1 $2 || exit 1
	;;
    --minimal-collections|--standard-collections|--full-collections)
	mkrpmcollection $1 || exit 1
	;;
    *)
	mkrpmspec $1 || exit 1
	;;
esac

exit

### end of file
