# preparation :
#   $ yum install git
#   $ mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
#   $ echo '%_topdir %(echo $HOME)/rpmbuild' >> ~/.rpmmacros
#   $ echo | gzip -c > ~/rpmbuild/SOURCES/polynum-0.0.tar.gz   # create a first tar.gz (fake)
#   $ cd ~/dev && git clone git://repos.entrouvert.org/polynum.git
#   $ ln -s ~/dev/polynum/rpm/centos.spec ~/rpmbuild/SPECS/polynum.spec
#   $ rpmbuild -ba ~/rpmbuild/SPECS/polynum.spec


#
# git macros
#
# from http://wiki.mandriva.com/en/Rpmbuild_and_git#First-time_build_for_a_project

%define git_repo polynum
%define git_head master

%define git_repodir %(echo ~/dev/)
%define git_gitdir %{git_repodir}/%{git_repo}/.git

%define git_get_source pushd %{git_repodir}/%{git_repo} ;\
        /usr/bin/git archive --format=tar --prefix=%{name}-%{version}/ %{git_head} | \
                gzip -c > %{_sourcedir}/%{name}-%{version}.tar.gz ;\
        popd

%define git_clone_source if [ -d %{name}-%{version} ] ; then \
                cd %{name}-%{version} && git pull origin %{git_head} ; \
        else \
                git clone %{git_gitdir} %{name}-%{version} && \
                cd %{name}-%{version}/ ; \
        fi

%define git_submodule git submodule
%define git_prep_submodules %{git_submodule} init --cloned && %{git_submodule} update

%define git_version %(git --git-dir=%{git_gitdir} describe 2> /dev/null || echo 0.0-`git --git-dir=%{git_gitdir} log --oneline | wc -l`-g`git --git-dir=%{git_gitdir} describe --always`)

# if the git repo has tags
%define git_get_ver %(echo %{git_version} | sed 's/^v\\?\\(.*\\)-\\([0-9]\\+\\)-g.*$/\\1/;s/-//')
%define git_get_rel %(echo %{git_version} | sed 's/^v\\?\\(.*\\)-\\([0-9]\\+-g.*\\)$/\\2/;s/-/_/')

#
# real spec starts here
#

# Header Stanza begins here

Summary: Polycopiés numériques
Name: polynum

Version: %git_get_ver
Release: %git_get_rel
License: AGPLv3+
Group: Applications/Internet
URL: http://dev.entrouvert.org/projects/polynum

Source0: %{name}-%{version}.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
BuildArch: noarch

Provides: polynum
# depends
Requires: python26
Requires: python26-virtualenv
Requires: python26-ldap
Requires(pre): /usr/sbin/groupadd, /usr/sbin/useradd
Requires(post): chkconfig
Requires(preun): chkconfig
# This is for /sbin/service
Requires(preun): initscripts

%description
Polynum (polycopiés numériques) est une application web pour demander
l'impression de polycopiés via l'ENT. Elle propose des options de diffusion
afin de mettre à disposition en ligne les polycopiés pour les étudiants.

%prep
%git_get_source
%setup -q

%build

# wtf... we don't want .pyo and .pyc files
%define __os_install_post echo "NO COMPRESS"

%install
%{__rm} -rf %{buildroot}
make DESTDIR=%{buildroot} install
%{__mkdir} -p %{buildroot}/%{_sysconfdir}/%{name}
%{__mkdir} -p %{buildroot}/%{_sysconfdir}/sysconfig
%{__mkdir} -p %{buildroot}/%{_sysconfdir}/init.d
%{__install} -m 644 rpm/sysconfig %{buildroot}/%{_sysconfdir}/sysconfig/%{name}
%{__install} -m 755 rpm/init.d %{buildroot}/%{_sysconfdir}/init.d/%{name}

%clean

%files
# set perms and ownerships of packaged files
# the - indicates that the current permissions on the files should be used
%defattr(-,root,root)
%doc requirements help local_settings.py.example gunicorn_config.py.example README polynum/base/fixtures
%config(noreplace) %{_sysconfdir}/sysconfig/%{name}
/opt/polynum/*
/etc/init.d/*
%dir %{_sysconfdir}/%{name}

%pre
# add polynum group & user
/usr/sbin/groupadd -r polynum || :
/usr/sbin/useradd -r -c "PolyNum daemon user" -d /opt/polynum -M -g polynum -s /sbin/nologin polynum || :

%post
# This adds the proper /etc/rc*.d links for the script
/sbin/chkconfig --add polynum

[ -f "%{_sysconfdir}/sysconfig/%{name}" ] && . %{_sysconfdir}/sysconfig/%{name}
# Sane defaults:
[ -z "$POLYNUM_USER" ] && POLYNUM_USER=polynum
[ -z "$POLYNUM_NAME" ] && POLYNUM_NAME="PolyNum daemon user"
[ -z "$POLYNUM_HOME" ] && POLYNUM_HOME=/opt/polynum
[ -z "$POLYNUM_GROUP" ] && POLYNUM_GROUP=polynum
[ -z "$POLYNUM_VAR" ] && POLYNUM_VAR=/var/lib/polynum
[ -z "$POLYNUM_ETC" ] && POLYNUM_ETC=%{_sysconfdir}/polynum
[ -z "$PIDFILE" ] && PIDFILE=/var/run/polynum/polynum.pid

# create pyc with python2.6
echo -n "(re)creating pyc files in "
for pydir in polynum bin virtualenv
do
    echo -n "$POLYNUM_HOME/$pydir "
    python2.6 -m compileall -f -q $POLYNUM_HOME/$pydir
done
echo "...ok"


%preun
# 'if [ $1 -eq 0 ] ' checks that this is the actual deinstallation of the
# package, as opposed to just removing the old package on upgrade. These
# statements stop the service, and remove the /etc/rc*.d links.
if [ $1 -eq 0 ] ; then
    /sbin/service polynum stop # >/dev/null 2>&1
    /sbin/chkconfig --del polynum
fi

%changelog
* Tue Jul 24 2012 Thomas NOEL <tnoel@entrouvert.com> 0.0-1
- create spec file

