File libreoffice-thesaurus.spec.in of Package libreoffice-thesaurus

#
# spec file for package libreoffice-thesaurus (Version 20070719)
#
# Copyright (c) 2010 SUSE LINUX Products GmbH, Nuernberg, Germany.
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via http://bugs.opensuse.org/
#

# norootforbuild


Name:           libreoffice-thesaurus
@DO_NOT_EDIT_COMMENT@
BuildRequires:  dos2unix recode unzip
License:        @ALL_LICENSES@
Group:          Productivity/Office/Dictionary
AutoReqProv:    on
Version:        20110202
Release:        4
Summary:        A Source Package for LibreOffice Thesaurus Dictionaries
BuildRoot:      %{_tmppath}/%{name}-%{version}-build
BuildArch:      noarch
Url:            http://wiki.services.openoffice.org/wiki/Dictionaries
Source0:        libreoffice-thesaurus-gen-spec
Source1:        libreoffice-thesaurus.spec.in
Source2:        libreoffice-thesaurus.data
@SOURCES@
%description
This source package is used to build LibreOffice thesaurus
dictionaries.



@METADATA@
%prep
%setup -T -c
# unpack sources
for archive in \
@SOURCES_UNPACK@    ; do
	archive_name=`basename $archive | sed -e "s|.zip$||" -e "s|.oxt$||" -e "s|.tar.gz$||" -e "s|.tar.bz2$||"`
	mkdir $archive_name || exit 1;
	case $archive in
	    *.zip|*.oxt)
		cd $archive_name; unzip $archive || exit 1; cd -
		;;
	    *.tar.gz)
		tar -xzf $archive -C $archive_name || exit 1;
		;;
	    *.tar.bz2)
		tar -xjf $archive -C $archive_name || exit 1;
		;;
	    *)
		echo "Error: Do not know how to uncompress $archive"
		exit 1;
		;;
	esac
done
#
# fix file names
@PREP_HACKS@#
# rename thes_ to th_ and add _v2 suffix
for suffix in idx dat ; do
    for file in `find . -name "th*_*.$suffix" -type f` ; do
	dir=$(dirname $file)
	fname=$(basename $file)
	fname_new=`echo $fname | sed -e "s|^thes_|th_|" -e "s|\.$suffix$|_v2.$suffix|" -e "s|_v2_v2|_v2|"`
	test "$fname" = "$fname_new" || mv -v "$dir/$fname" "$dir/$fname_new"
    done
done
#
# fix permissions
find . -type f -exec chmod 644 {} \;

%build

%install
rm -f filelist.template
install -m 755 -d $RPM_BUILD_ROOT%{_datadir}/mythes
# compat dir
install -m 755 -d $RPM_BUILD_ROOT%{_datadir}/ooo/thesaurus
for lang_dir in * ; do
    test -d $lang_dir || continue
    # temporary file list
    filelist=`mktemp /tmp/lo-thes-XXXXXX`
    echo "%%defattr(-,root,root)" >$filelist
    echo "%%dir %{_datadir}/mythes" >>$filelist
    # compat dir
    echo "%%dir %{_datadir}/ooo" >>$filelist
    echo "%%dir %{_datadir}/ooo/thesaurus" >>$filelist
    # install dictionaries
    dic=
    # support locales with 2 and 3 letters in the language code: th_ll_CC_v2.idx and th_lll_CC_v2.idx
    for idx in `find $lang_dir -regextype posix-extended -regex ".*/th_[[:lower:]][[:lower:]][[:lower:]]?_[[:upper:]][[:upper:]]_v2.idx" | sort` ; do
	dat=`echo $idx | sed -e "s|.idx$|.dat|"`
	if ! test -f $dat ; then
	    echo "Warning: Found $idx but not $dat"
	    echo "Skipping $idx"
	    continue;
	fi
	# both .idx and .dat files exists => install them
	for f in $idx $dat ; do
	    fname=$(basename $f)
	    if test -f $RPM_BUILD_ROOT%{_datadir}/mythes/$fname ; then
		echo "Error: file already installed: $f"
		echo "       Probably is provided by two source tarballs"
		exit 1;
	    fi
	    install -m 644 $f $RPM_BUILD_ROOT%{_datadir}/mythes/
	    echo %{_datadir}/mythes/$fname >>$filelist
            # compat stuff
	    ln -sf ../../mythes/$fname $RPM_BUILD_ROOT%{_datadir}/ooo/thesaurus/
	    echo %{_datadir}/ooo/thesaurus/$fname >>$filelist
	done
    done

    # detect locale, lang and country
    if test -n $idx ; then
    # replace the underscore with a dot to be able to separate the language and the coutry code with the command "cut"
	dic_pname=`basename $idx | sed "s|_|.|g"`
	lang=`echo $dic_pname | cut -d "." -f2`
	country=`echo $dic_pname | cut -d "." -f3`
    else
	echo "Error: No dictionary found in $lang_dir"
	exit 1;
    fi
    locale="${lang}_${country}"

    # detect long language name
    case "$locale" in 
@LANG_NAME@	*)	 lang_name="$lang" ;;
    esac

    # detect README encoding
    case "$locale" in 
@ENCODING@	*) coding=`LC_CTYPE="$locale" locale -k LC_CTYPE | grep charmap | sed "s|charmap=\"\(.*\)\"\$|\1|"` ;;
    esac

    # install documentation
    docdir=%{_docdir}/libreoffice-thesaurus-$lang_name
    mkdir -p $RPM_BUILD_ROOT$docdir
    echo "%%doc $docdir" >>$filelist
    for doc in `find $lang_dir -type f -printf "%%P\n" | \
                grep -v -e ".aff$" -e ".dic$" -e ".dat$" -e ".idx$" \
                        -e ".orig$" -e ".old$" -e "~$" \
                        -e ".sh$" -e "makefile" -e "META-INF"` ; do
	if ! test -f $lang_dir/$doc ; then
	    echo "Error: is not a file: $lang_dir/$doc"
	    exit 1;
	fi
	recode utf8..utf16 <$lang_dir/$doc >/dev/null 2>&1 || recode $coding..utf8 $lang_dir/$doc
	dos2unix $lang_dir/$doc
	dname=$(dirname $doc)
	install -m 755 -d $RPM_BUILD_ROOT$docdir/$dname
	install -m 644 $lang_dir/$doc $RPM_BUILD_ROOT$docdir/$dname
    done
    # rename the file list according to the detacted lang and country
    mv $filelist $locale.list
    # generate tempalte for the %files section
    echo "%%files -f $locale.list $lang_name" >>filelist.template
    echo "" >>filelist.template
done


%clean
rm -rf $RPM_BUILD_ROOT

@FILES@
%changelog
openSUSE Build Service is sponsored by