研究了一下 sagemath

我觉得 sagemath 算是比较有良心的科学软件了。 实话说我没怎么学数学的东西,也只浅显地用过一点点 sagemath,还是用 mandriva 时候尝鲜的。 现在大二了,迟早以后要用这些软件。 就下载了一个 tar 源码包,看看怎么打包。 不过我还没有打过包(除了小小玩过 AUR),所以肯定一开始打不了这么大的包的。
下载了 sagemath 的 tar 源码。

首先声明我完全不会打包,所以发这个帖子, ** 以探讨、提高为主 ** 。

sage-5.12 目录:
COPYING.txt
Makefile
README.txt
sage.sh
VERSION.txt

其中 README 文件非常详细地讲解了怎么从源码安装 sagemath。 只要运行这个 make 就行了。 还有一段比较详细的介绍,介绍了一些比较详细的编译方面的细节。 总体来说,算是科学软件中很有良心的了。

比那些说开放源代码,下载回来就是一个可执行 shell 脚本,然后说把我拷贝到 /usr/bin,权限满格,然后运行就安装好了的典型 osx 混乱思维的科学软件已经领先了不少了。

具体来说,我记录了几个地方:
1)依赖关系:gcc, make, m4, perl, ranlib, tar。还说在 OSX 上依赖 xcode
2)特地提到了在部分 deb 系发行版,尤其是 ubuntu12.04 上依赖 dpkg-dev 包,让人费解,缺乏进一步说明
3)然后说解决了依赖关系之后 cd,make 即可。 显然不能这么简单得看
4)文件中提到了 SELinux 可能带来安装错误
5)接下来开始混乱了,提到了 Sage 由很多不同的语言写成的不同的组件集合而成,这个是大家都知道的 sage 是既有开源数学软件集合而来的
5)然后又有了一个详细编译指南,仔细一看,安装完之后,是编译到了./sage 目录,而不是 /usr 之类的地方,然后要再使用 shell 脚本给拷贝。 不知道这个是否对打包产生影响?
6)接下来又说了如果要 docbuild 就要依赖 latex。前面为什么不说?
7)接下来又出现了与 pyopenSSL 一起编译的说明。
8)接下来出现了编译器介绍。上面说 sage 自带了 gcc,所以编译的时候需要一个能够编译 gcc 的编译器,推荐用 gcc??? 还好有选项可以去掉安装 gcc。 Linux 里面早就有了 gcc 了。
9)有一个分发说明,说很容易对 sage 进行打包,只需要 sage --sdist sage --bdist 就分别打源码包和二进制包。 可惜不是打 rpm 包。 难道 sage 的作者不知道 rpm 的存在?

然后进入 sage-5.12/spkg 目录:
有以下几个文件:
gen_html.py
install
pipestatus.sh
README.txt
root-spkg-install.sh

其中 install 没有扩展名,打开来一看是个 shell 脚本。 root-spkg-install 看不懂在干什么,只看懂了在操作 hg 命令(mercurial 分布式版本控制软件),为什么要操作 hg??

里面三个目录:base、bin、standard

base 里面有一个 prereq 的脚本,检查你的操作系统、依赖是否满足。 里面还检查 spkg。
bin 里面一堆 shell 脚本。
重点是 standard,就是 sage 所包含的包:(版本号掠过了)
atlas
boehm_gc
boost_cropped
bzip2
cddlib
cephes
cliquer
conway_polynomials
cvxopt
cyphon
docutils
ecl
eclib
ecm
elliptic
extcode
fflas_ffpack
flint
flintqs
freetype
gap
gcc
gd
gdmodule
genus2reduction
gf2x
gfan
givaro
glpk
graphs
gsl
iconv
iml
ipython
jinja2
jmol
icalc
libfplll
libgap
libm4ri
libm4rie
libpng
linbox
ircalc
matplotlib
maxima
mercurial
mpc
mpfi
mpfr
mpir
mpmath
ncurses
networkx
ntl
numpy
palp
pari
patch
pexpect
pil
polybori
polytopes_db
ppl
pycrypto
pygments
pynac
python-2.7.5
r-2.1.5
ratpoints
readline
rpy2
rubiks
sage
sage_root
sage_scripts
sagenb
sagetex
scipy
scons
setuptools
singular
sphinx
sqlalchemy
sqlite
symmetrica
sympow
sympy
tachyon
zlib
zn_poly
里面有多少个是大部分 Linux 发行版的源里面都有的?

不得不说,这方面是让人有些失望的。 下一步需要搞清楚的是,能否不使用 sage 提供的这些包。 有一些诸如 sqlite、maxima、sphinx、pygments、zlib 等等太多太多的,明显就是系统自带的东西。 如果不愿意使用,怎样才能够给 sagemath 减肥并且打包?

如果嫌 sagemath 的本地版太难折腾,可以在 cloud.sagemath.com/ ( com 的结尾肯定有深刻意义。。。) 搞个账号,里面的终端连 Java,w3m,emacs,gcc 之类的货都有,而且带 40g 内存,你在其中创建的文件还能通过网页界面下载回来。嘿嘿,拿这个能干啥我就不多说了。。。

Sent from my SCH-I699 using Xparent Skyblue Tapatalk 2

sagemath 的本地编译还是比较方便的。直接解压到主目录下,进去 make 一下。完了把解压的目录隐藏就好了。对了,别忘了让 PATH 找到 sage 可执行文件。

觉得比较好的一点是虽然没有在 YaST 的管理下,sage 有自己的升级命令可以在线升级,所以也还算可以,不会把系统搞得很脏。我就直接丢在主目录下升级也好重装也好都不用担心。

查了一下,果然开发者用的 mac。

我感觉苹果是开源高级黑。 苹果里很多 linux 程序可以用。 所以现在有不少用着开源最大公敌苹果系统的人也号称热衷于开源,然后写出了这一堆乱七八糟的开源软件。

而传统的 linux 用户写的开源软件,实在是舒服地多。 依赖关系清晰、makefile 等一应俱全、有 INSTALL 安装指南。 而 mac 用户写的开源软件往往缺乏合理的安装方式,而是要求用户运行可执行脚本安装。

我刚到美国的时候计算机第一堂课用的一个 processing 编程语言,可视化的,也是基于 java 的(蛋疼)。 我在 linux 装,号称开源的但是下载的就是编译好的。 上课必须要用,我只好用,结果启动之后就报错,原因是缺乏权限不能在 /usr/xxx 目录中建立工作空间。 还逼迫我用 root 执行你这个程序? 为什么不能让我选择工作目录? 而强行要用一个需要 root 权限的位置来作为工作目录?

苹果用户都很淡定。 因为我经过同意试验过别人的苹果电脑。 我没有输入任何密码就把同学装的一个软件在 bin 目录里的可执行文件替换掉了。

我只好高度怀疑这个 processing 的开发者是 mac 用户。 Linux 用户开发程序应该不至于犯这么低级的具有如此大安全隐患的错误。 大部分 Linux 发行版在安装时就强制建立非 root 用户并使用。

sage 可以自己升级。但是自己升级很讨厌的啊~ 我用学校的 mac,每次打开 firefox 就提示要升级,然后又关闭掉。 还是统一管理让人感觉舒舒服服。

我多少能猜到你说的能干啥是什么事情。不过我是此事的反对者。而且我在美国也不需要干。

我想这个 cloud 服务器多少是基于 linux 的吧? 至少我看图片不是苹果机。 那么这个开发者为什么不采用 linux 的先进包管理思想?

所以说我个人肯定会选择其他数学软件,而不是 sagemath。 sagemath 本来就是一个对其他开源数学工具的集合罢了,直接用其他工具即可。 ipython 和 sagemath 的 notebook 倒是有点兴趣。 但是打字的东西我自己用 vim 打。

当然不排除放假之后研究一下 sage 的代码把它给拆解了打包。

这是个比较耗费精力的事情,但也无可奈何。是否可以考虑试试向 upstream 反映呢?看看它怎么回复的。

这是个比较耗费精力的事情,但也无可奈何。是否可以考虑试试向 upstream 反映呢?看看它怎么回复的。

恩。我会问一下怎么处理依赖关系比较好。

包括几个数学软件就算了。 连 gcc 都自带了,还自带了一陀与数学无关的包比如 pygments。。 最好能用 linux 系统的包管理器解决这些依赖而不是让 sagemath 再往系统里增加一堆。

指望 upstream 估计不容易。因为貌似创始人是 mac 用户。 mac 用户大部分貌似都很难理解 linux 的包管理思维。 我认识的大部分 mac/ubuntu 用户都只关心能否安装上,而不关心以后怎么样,会带来什么后果。 不过确实可以试试。

不就是当个小 vps 比如跑跑实验程序。
你难道以为我用这翻 q 昂?无此需求。。。

Sent from my SCH-I699 using Xparent Skyblue Tapatalk 2

当 VPS? 可以搞个人网页吗? 估计不能。 不过可能确实可以跑实验程序。 但是这个世界用学校的 CentOS 集群就可以了。

这是个比较耗费精力的事情,但也无可奈何。是否可以考虑试试向 upstream 反映呢?看看它怎么回复的。

看了 fedora 的 specfile,发现 fedora 的 packager 们就真的手动把 sagemath 给拆解了,然后写了几十个 patch 和上千行的 specfile 来解决问题。 实话说,sage 的开发者真的应该好好反省自己。 搞这么多东西 bundle 到里面干什么呢? 非要别人弄一堆重复的东西? 唉。

Fedora的specfile:

# of a system package
%global __provides_exclude_from	.*/site-packages/.*\\.so

# for quicker test with packages not yet in fedora
%global packager_debug		0

# for quicker fedora-review tests without downloading the main tarball
%global download_tarball	0

# not functional due to missing jar dependencies
%global with_sage3d		0

# use an workaround to match upstream sagemath patched sphinx
%global with_sphinx_hack	1

# sagemath works only with pexpect-2.0
%global with_sage_pexpect	1

# https://bugzilla.redhat.com/show_bug.cgi?id=909510
%global have_lrcalc		%{packager_debug}

# Cbc dependency under review is as follow:
#  first need coin-or-CoinUtils
# 	https://bugzilla.redhat.com/show_bug.cgi?id=894585
#  then coin-or-Osi (that requires coin-or-CoinUtils)
#	https://bugzilla.redhat.com/show_bug.cgi?id=894586
#  then coin-or-Clp (that requires coin-or-Osi)
#	https://bugzilla.redhat.com/show_bug.cgi?id=894587
# then coin-or-Cgl (that requires coin-or-Clp)
#	https://bugzilla.redhat.com/show_bug.cgi?id=894588
# and finally coin-or-Cbc (that requires coin-or-Cgl)
#	https://bugzilla.redhat.com/show_bug.cgi?id=894597
# other coin-or-* packages under review are not required by sagemath
# but are required to have the "basic" set of coin-or packages
%global have_coin_or_Cbc	%{packager_debug}

# https://bugzilla.redhat.com/show_bug.cgi?id=914936
%ifarch %{ix86} x86_64
%global have_fes		%{packager_debug}
%else
%global have_fes		0
%endif

# set to run sage -testall in %%install
%global with_check		0
%global SAGE_TIMEOUT		60
%global SAGE_TIMEOUT_LONG	180

%global conway_polynomials_pkg	conway_polynomials-0.4.p0
%global	elliptic_curves_pkg	elliptic_curves-0.7
%global	flintqs_pkg		flintqs-20070817.p8
%global graphs_pkg		graphs-20120404.p4
%global pexpect_pkg		pexpect-2.0.p5
%global polytopes_db_pkg	polytopes_db-20120220
%global rubiks_pkg		rubiks-20070912.p18
%global	sagenb_pkg		sagenb-0.10.7.2
%global sagetex_pkg		sagetex-2.3.4

%global SAGE_ROOT		%{_libdir}/sagemath
%global SAGE_LOCAL		%{SAGE_ROOT}/local
%global SAGE_SRC		%{SAGE_ROOT}/src
%global SAGE_DOC		%{_docdir}/%{name}
%global SAGE_SHARE		%{_datadir}/sagemath
%global SAGE_EXTCODE		%{SAGE_SHARE}/ext
%global SAGE_PYTHONPATH		%{SAGE_ROOT}/site-packages

Name:		sagemath
Group:		Applications/Engineering
Summary:	A free open-source mathematics software system
Version:	5.12
Release:	2%{?dist}
# The file ${SAGE_ROOT}/COPYING.txt is the upstream license breakdown file
# Additionally, every $files section has a comment with the license name
# before files with that license
License:	ASL 2.0 and BSD and GPL+ and GPLv2+ and LGPLv2+ and MIT and Public Domain
URL:		http://www.sagemath.org
%if %{download_tarball}
Source0:	http://boxen.math.washington.edu/home/%{name}/sage-mirror/src/sage-%{version}.tar
%else
Source0:	sage-%{version}.tar
%endif
Source1:	gprc.expect
Source2:	makecmds.sty
# not installed by jmol package, use one in sagemath jmol spkg
Source3:	Jmol.js
Source4:	JmolHelp.html
# from jmol-12.3.27.p2 spkg
Source5:	testjava.sh

# 1. scons ignores most environment variables
# 2. scons 2.2* does not have soname support (expected for scons 2.3*)
# This patch adds some regex substition templates for CFLAGS, etc, and
# minor adaptation from full scons patch at:
# http://scons.tigris.org/nonav/issues/showattachment.cgi/902/soname_func.py
# Discussed at:
# http://scons.tigris.org/issues/show_bug.cgi?id=2869
Patch0:		%{name}-scons.patch

# Upstream uses mpir not gmp, but the rpm package is tailored to use gmp
Patch1:		%{name}-gmp.patch

# Set of patches to work with system wide packages
Patch2:		%{name}-scripts.patch

# Adapt to ntl 6.0.0.
Patch3:		%{name}-ntl6.patch

# remove call to not implemented sagemath "is_package_installed" interfaces
# need to package coin-or solver in fedora
# remove check for non free solvers
Patch4:		%{name}-extensions.patch

# helper to:
#	o respect a DESTDIR environment variable
#	o avoid double '//' in pathnames, what can confused debugedit & co
#	o minor change to help in incremental builds by avoiding rebuilding
#	  files
#	o do not assume there is an installed sagemath
Patch5:		%{name}-rpmbuild.patch

# build documentation in buildroot environment
Patch6:		%{name}-sagedoc.patch

# sage notebook rpm and system environment adjustments
Patch7:		%{name}-sagenb.patch

# do not attempt to create state files in system directories
Patch8:		%{name}-readonly.patch

# force coercion of ecl t_string to ecl t_base_string
# this is hackish and only required if ecl is built with unicode support
Patch9:		%{name}-ecl-unicode.patch

# do not link explicitly to png12
Patch10:	%{name}-png.patch

# work with all maxima-runtime lisp backend packages
Patch11:	%{name}-maxima.patch

# execute 4ti2 programs in $PATH not in $SAGE_ROOT/local/bin
Patch12:	%{name}-4ti2.patch

# http://trac.sagemath.org/sage_trac/ticket/12992
# http://pari.math.u-bordeaux.fr/cgi-bin/bugreport.cgi?bug=1317
Patch13:	%{name}-pari.patch

# Portuguese translations: http://trac.sagemath.org/sage_trac/ticket/12822
Patch14:	trac_12502_pt_translation_of_a_tour_of_sage_rebase1.patch
Patch15:	trac_12822_pt_translation_of_tutorial.patch
Patch16:	trac_12822_pt_translation_of_tutorial_rev1.patch

# use jmol itself to export preview images
# FIXME besides not using X and told so, fails if DISPLAY is not set
Patch17:	%{name}-jmol.patch

# only cremona mini database built and installed
# FIXME add a package with the full cremona database
# FIXME actually it should be already available in pari-elldata
Patch18:	%{name}-cremona.patch

# lrslib is a requires
Patch19:	%{name}-lrslib.patch

# nauty cannot be packaged due to license restrictions
# http://cs.anu.edu.au/~bdm/nauty/
# http://pallini.di.uniroma1.it/
Patch20:	%{name}-nauty.patch

# gap hap package not (yet) available
# http://www-gap.mcs.st-and.ac.uk/Packages/hap.html
Patch21:	%{name}-gap-hap.patch

# Patch to enable lrcalc once review request is done in Fedora
Patch22:	%{name}-lrcalc.patch

# Patch to enable cbc once review requests are done in Fedora
Patch23:	%{name}-cbc.patch

# Use system gap directories
Patch24:	%{name}-libgap.patch

# Patch to enable fes once review requests are done in Fedora
Patch25:	%{name}-fes.patch

# Get package to build with known problem if not yet updated to pari 2.6.
Patch26:	%{name}-nopari2.6.patch

# sagemath 5.8 (optionally) requires cryptominisat 2.9.6 (in >= f19)
# and does not work with cryptominisat 2.9.5 (in f18)
Patch27:	%{name}-cryptominisat.patch

# Side effect of using distro packages
# https://bugzilla.redhat.com/show_bug.cgi?id=974769
Patch28:	%{name}-sympy.patch

BuildRequires:	4ti2
BuildRequires:	atlas-devel
BuildRequires:	cddlib-tools
BuildRequires:	cliquer-devel
%if %{have_coin_or_Cbc}
BuildRequires:	coin-or-Cbc-devel
%endif
BuildRequires:	cryptominisat-devel
BuildRequires:	Cython
BuildRequires:	desktop-file-utils
BuildRequires:	dos2unix
BuildRequires:	dvipng
BuildRequires:	ecl
BuildRequires:	eclib-devel
BuildRequires:	factory-devel
%if %{have_fes}
BuildRequires:	fes-devel
%endif
BuildRequires:	flint-devel >= 2.3
BuildRequires:	gmp-ecm-devel
BuildRequires:	gap
BuildRequires:	GAPDoc
BuildRequires:	gap-character-tables
BuildRequires:	gap-libs
BuildRequires:	gap-sonata
BuildRequires:	gap-table-of-marks
BuildRequires:	gc-devel
BuildRequires:	gd-devel
BuildRequires:	gfan
BuildRequires:	glpk-devel
BuildRequires:	gnutls-devel
BuildRequires:	gsl-devel
BuildRequires:	iml-devel
BuildRequires:	L-function-devel
BuildRequires:	libfac-devel
BuildRequires:	libfplll-devel
BuildRequires:	libgap-devel
BuildRequires:	libmpc-devel
BuildRequires:	linbox-devel
%if %{have_lrcalc}
BuildRequires:	lrcalc-devel
%endif
BuildRequires:	m4ri-devel
BuildRequires:	m4rie-devel
BuildRequires:	maxima-runtime-ecl
BuildRequires:	mpfi-devel
BuildRequires:	ntl-devel
BuildRequires:	numpy
BuildRequires:	palp
BuildRequires:	pari-devel
BuildRequires:	pari-gp
BuildRequires:	ppl-devel
BuildRequires:	pynac-devel
BuildRequires:	python2-devel
BuildRequires:	python-flask-autoindex
BuildRequires:	python-flask-babel
BuildRequires:	python-flask-openid
BuildRequires:	python-flask-silk
BuildRequires:	python-ipython
BuildRequires:	python-matplotlib
BuildRequires:	python-networkx
BuildRequires:	python-sphinx
BuildRequires:	python-twisted-web
BuildRequires:	python-twisted-web2
BuildRequires:	polybori-devel
BuildRequires:	R
BuildRequires:	ratpoints-devel
BuildRequires:	readline-devel
%if 0%{?fedora} > 18
BuildRequires:	rpy
%endif
BuildRequires:	scons
BuildRequires:	Singular-devel
%if 0%{?fedora} >= 18
BuildRequires:	stix-math-fonts
%else
BuildRequires:	stix-fonts
%endif
BuildRequires:	sympow
BuildRequires:	sympy
BuildRequires:	symmetrica-devel
BuildRequires:	texlive
BuildRequires:	zn_poly-devel

Requires:	4ti2
Requires:	apache-commons-cli
Requires:	cddlib-tools
Requires:	Cython
Requires:	ecl
Requires:	firefox
Requires:	gap
Requires:	GAPDoc
Requires:	gap-character-tables
Requires:	gap-prim-groups
Requires:	gap-small-groups
Requires:	gap-sonata
Requires:	gap-table-of-marks
Requires:	gap-trans-groups
Requires:	genus2reduction
Requires:	gfan
Requires:	gmp-ecm
Requires:	jmol
Requires:	jsmath-fonts
Requires:	lrslib-utils
Requires:	maxima-gui
Requires:	maxima-runtime-ecl
Requires:	palp
Requires:	pari-gp
Requires:	python-crypto
Requires:	python-cvxopt
Requires:	python-flask-autoindex
Requires:	python-flask-babel
Requires:	python-flask-openid
Requires:	python-flask-silk
Requires:	python-ipython
Requires:	python-matplotlib
Requires:	python-networkx
Requires:	python-polybori
Requires:	python-twisted-web
Requires:	python-twisted-web2
Requires:	python-ZODB3
Requires:	R
Requires:	rpy
Requires:	%{name}-core
Requires:	%{name}-data
Requires:	%{name}-doc-en
Requires:	%{name}-notebook
Requires:	%{name}-rubiks
Requires:	%{name}-sagetex
Requires:	Singular
%if 0%{?fedora} >= 18
Requires:	stix-math-fonts
%else
Requires:	stix-fonts
%endif
Requires:	sympow
Requires:	sympy
Requires:	tachyon
Requires:	texlive
Requires:	vecmath

# Missing build requires on armv7hl
ExclusiveArch:	%{ix86} x86_64

%description
Sage is a free open-source mathematics software system licensed
under the GPL. It combines the power of many existing open-source
packages into a common Python-based interface.

#------------------------------------------------------------------------
%package	core
Summary:	Open Source Mathematics Software
Group:		Applications/Engineering
Requires:	%{name}%{?_isa} = %{version}-%{release}

%description	core
This package contains the core sagemath python modules.

#------------------------------------------------------------------------
%package	data
Summary:	Databases and scripts for %{name}
Group:		Applications/Engineering
Requires:	%{name} = %{version}-%{release}
Requires:	%{name}-data-conway_polynomials = %{version}-%{release}
Requires:	%{name}-data-elliptic_curves = %{version}-%{release}
Requires:	%{name}-data-extcode = %{version}-%{release}
Requires:	%{name}-data-graphs = %{version}-%{release}
Requires:	%{name}-data-polytopes_db = %{version}-%{release}
BuildArch:	noarch

%description	data
Collection of databases and interface customization scripts for sagemath.

#------------------------------------------------------------------------
%package	data-conway_polynomials
Summary:	Conway Polynomials Database
Group:		Applications/Engineering
Requires:	%{name}-data = %{version}-%{release}
BuildArch:	noarch

%description	data-conway_polynomials
Small database of Conway polynomials for sagemath.

#------------------------------------------------------------------------
%package	data-elliptic_curves
Summary:	Databases of elliptic curves
Group:		Applications/Engineering
Requires:	%{name}-data = %{version}-%{release}
BuildArch:	noarch

%description	data-elliptic_curves
Includes two databases:

 * A small subset of the data in John Cremona's database of elliptic curves up
   to conductor 10000. See http://www.warwick.ac.uk/~masgaj/ftp/data/ or
   http://sage.math.washington.edu/cremona/INDEX.html

 * William Stein's database of interesting curves

#------------------------------------------------------------------------
%package	data-extcode
Summary:	Extcode for Sagemath
Group:		Applications/Engineering
Requires:	%{name}-data = %{version}-%{release}
BuildArch:	noarch

%description	data-extcode
Collection of scripts and interfaces to sagemath.

#------------------------------------------------------------------------
%package	data-graphs
Summary:	Sagemath database of graphs
Group:		Applications/Engineering
Requires:	%{name}-data = %{version}-%{release}
BuildArch:	noarch

%description	data-graphs
A database of graphs. Created by Emily Kirkman based on the work of Jason
Grout. Since April 2012 it also contains the ISGCI graph database.

#------------------------------------------------------------------------
%package	data-polytopes_db
Summary:	Lists of 2- and 3-dimensional reflexive polytopes
Group:		Applications/Engineering
Requires:	%{name}-data = %{version}-%{release}
BuildArch:	noarch

%description	data-polytopes_db
The list of polygons is quite easy to get and it has been known for a while.
The list of 3-polytopes was originally obtained by Maximilian Kreuzer and
Harald Skarke using their software PALP, which is included into the standard
distribution of Sage. To work with lattice and reflexive polytopes from Sage
you can use sage.geometry.lattice_polytope module, which relies on PALP for
some of its functionality. To get access to the databases of this package, use
ReflexivePolytope and ReflexivePolytopes commands.

#------------------------------------------------------------------------
%package	devel
Summary:	Development files for %{name}
Group:		Development/Libraries
Requires:	%{name}%{?_isa} = %{version}-%{release}

%description	devel
This package contains the header files and development documentation
for %{name}.

#------------------------------------------------------------------------
%package	doc
Summary:	Documentation infrastructure files for %{name}
Group:		Documentation
BuildArch:	noarch

%description	doc
This package contains the documentation infrastructure for %{name}.

#------------------------------------------------------------------------
%package	doc-de
Summary:	German documentation files for %{name}
Group:		Documentation
Requires:	%{name}-doc = %{version}-%{release}
BuildArch:	noarch

%description	doc-de
This package contains the German %{name} documentation.

#------------------------------------------------------------------------
%package	doc-en
Summary:	English documentation files for %{name}
Group:		Documentation
Requires:	%{name}-doc = %{version}-%{release}
BuildArch:	noarch

%description	doc-en
This package contains the English %{name} documentation.

#------------------------------------------------------------------------
%package	doc-fr
Summary:	French documentation files for %{name}
Group:		Documentation
Requires:	%{name}-doc = %{version}-%{release}
BuildArch:	noarch

%description	doc-fr
This package contains the French %{name} documentation.

#------------------------------------------------------------------------
%package	doc-pt
Summary:	Portuguese documentation files for %{name}
Group:		Documentation
Requires:	%{name}-doc = %{version}-%{release}
BuildArch:	noarch

%description	doc-pt
This package contains the Portuguese %{name} documentation.

#------------------------------------------------------------------------
%package	doc-ru
Summary:	Russian documentation files for %{name}
Group:		Documentation
Requires:	%{name}-doc = %{version}-%{release}
BuildArch:	noarch

%description	doc-ru
This package contains the Russian %{name} documentation.

#------------------------------------------------------------------------
%package	doc-tr
Summary:	Turkish documentation files for %{name}
Group:		Documentation
Requires:	%{name}-doc = %{version}-%{release}
BuildArch:	noarch

%description	doc-tr
This package contains the Turkish %{name} documentation.

#------------------------------------------------------------------------
%package	notebook
Summary:	The Sage Notebook
Group:		Applications/Engineering
Requires:	%{name}%{?_isa} = %{version}-%{release}

%description	notebook
The Sage Notebook is a web-based graphical user interface for
mathematical software.

#------------------------------------------------------------------------
%package	rubiks
Summary:	Several programs for working with Rubik's cubes
Group:		Applications/Engineering
Requires:	%{name}%{?_isa} = %{version}-%{release}

%description	rubiks
Several programs for working with Rubik's cubes, by three  different people.
In summary the three contributors are:

Michael Reid (GPL) http://www.math.ucf.edu/~reid/Rubik/optimal_solver.html
    optimal - uses many pre-computed tables to find an optimal 
              solution to the 3x3x3 Rubik's cube

Dik T. Winter (MIT License)
    cube    - uses Kociemba's algorithm to iteratively find a short
              solution to the 3x3x3 Rubik's cube 
    size222 - solves a 2x2x2 Rubik's cube 

Eric Dietz (GPL) http://www.wrongway.org/?rubiksource
    cu2   - A fast, non-optimal 2x2x2 solver
    cubex - A fast, non-optimal 3x3x3 solver
    mcube - A fast, non-optimal 4x4x4 solver

#------------------------------------------------------------------------
%package	sagetex
Summary:	Sagemath into LaTeX documents
Group:		Applications/Engineering
Requires:	%{name}%{?_isa} = %{version}-%{release}

%description	sagetex
This is the SageTeX package. It allows you to embed code, results of
computations, and plots from the Sage mathematics software suite
(http://sagemath.org) into LaTeX documents.

########################################################################
%prep
%setup -q -n sage-%{version}

mkdir -p spkg/build
pushd spkg/build
    for pkg in					\
	%{conway_polynomials_pkg}		\
	%{elliptic_curves_pkg}			\
	extcode-%{version}			\
	%{flintqs_pkg}				\
	%{graphs_pkg}				\
%if %{with_sage_pexpect}
	%{pexpect_pkg}				\
%endif
	%{polytopes_db_pkg}			\
	%{rubiks_pkg}				\
	%{sagenb_pkg}				\
	%{sagetex_pkg}				\
	sage-%{version}				\
	sage_scripts-%{version}			\
    ; do
	tar jxf ../standard/$pkg.spkg
    done

    # apply in spkgs that do not have patches already applied
    # or that actually have patches
pushd %{flintqs_pkg}/src
    for diff in `ls ../patches/*.patch`; do
	patch -p1 < $diff
    done
popd

pushd %{sagenb_pkg}/src
    tar zxf %{sagenb_pkg}.tar.gz
    mv  %{sagenb_pkg} sagenb
popd

%if %{with_sage_pexpect}
    pushd %{pexpect_pkg}/src
	for diff in `ls ../patches/*.patch ../patches/*.diff`; do
	    patch -p1 < $diff
	done
    popd
%endif
    pushd %{rubiks_pkg}
	cp patches/dietz-mcube-Makefile src/dietz/mcube/Makefile
	cp patches/dietz-solver-Makefile src/dietz/solver/Makefile
	cp patches/dietz-cu2-Makefile src/dietz/cu2/Makefile
	cp patches/reid-Makefile src/reid/Makefile
    popd
popd

%patch0 -p1
%patch1 -p1
%patch2 -p1
%patch3 -p1
%patch4 -p1
%patch5 -p1
%patch6 -p1
%patch7 -p1
%patch8 -p1
%patch9 -p1
%patch10 -p1
%patch11 -p1
%patch12 -p1
%patch13 -p1

pushd spkg/build/sage-%{version}
mkdir -p doc/pt/a_tour_of_sage/
cp -fa doc/en/a_tour_of_sage/*.png doc/pt/a_tour_of_sage/
%patch14 -p1
%patch15 -p1
%patch16 -p1
popd

%patch17 -p1
%patch18 -p1
%patch19 -p1

%patch20 -p1
%patch21 -p1

%if %{have_lrcalc}
%patch22 -p1
%endif

# other coin-or packages are build requires or coin-or-Cbc
%if %{have_coin_or_Cbc}
%patch23 -p1
%endif

%patch24 -p1

%if %{have_fes}
%patch25 -p1
%endif

%patch26 -p1
%patch27 -p1
%patch28 -p1

sed -e 's|@@SAGE_ROOT@@|%{SAGE_ROOT}|' \
    -e 's|@@SAGE_DOC@@|%{SAGE_DOC}|' \
    -i spkg/build/sage-%{version}/sage/env.py

#------------------------------------------------------------------------
# ensure proper/preferred libatlas is in linker path
pushd spkg/build/sage-%{version}
    perl -pi -e 's|^(extra_link_args = ).*|$1\"-L%{_libdir}/atlas"\]|;' sage/misc/cython.py
    # some .c files are not (re)generated
    find . \( -name \*.pyx -o -name \*.pxd \) | xargs touch
popd

# remove bundled jar files before build
rm spkg/build/%{sagenb_pkg}/src/sagenb/sagenb/data/sage3d/lib/sage3d.jar

# remove binary egg
rm -r spkg/build/%{sagenb_pkg}/src/sagenb/sagenb.egg-info
###

########################################################################
%build
export CFLAGS="%{optflags}"
export CXXFLAGS="%{optflags}"
export SAGE_ROOT=%{buildroot}%{SAGE_ROOT}
export SAGE_LOCAL=%{buildroot}%{SAGE_LOCAL}
# Avoid buildroot in gcc command line (use _builddir instead)
export SAGE_SRC="$PWD/spkg/build/sage-%{version}"
export SAGE_FORTRAN=%{_bindir}/gfortran
export SAGE_FORTRAN_LIB=`gfortran --print-file-name=libgfortran.so`
export DESTDIR=%{buildroot}
# Use file in /tmp because there are issues with long pathnames
export DOT_SAGE=/tmp/sage$$
mkdir -p $DOT_SAGE/tmp

# match system packages as sagemath packages
mkdir -p $SAGE_ROOT $SAGE_LOCAL #%{buildroot}%{SAGE_SRC}
ln -sf %{_libdir} $SAGE_LOCAL/lib
ln -sf %{_includedir} $SAGE_LOCAL/include
ln -sf %{_datadir} $SAGE_LOCAL/share
#ln -sf $SAGE_SRC/sage %{buildroot}%{SAGE_SRC}/sage

export PATH=%{buildroot}%{_bindir}:$PATH
export PYTHONPATH=%{buildroot}%{python_sitearch}:$PYTHONPATH

#------------------------------------------------------------------------
pushd spkg/build/sage-%{version}
    pushd c_lib
	# scons ignores most environment variables
	# and does not have soname support
	sed -e 's|@@includedir@@|%{_includedir}|g' \
	    -e 's|@@libdir@@|%{_libdir}|g' \
	    -e 's|@@optflags@@|%{optflags}|g' \
	    -e 's|@@__global_ldflags@@|%{__global_ldflags}|g' \
	    -i SConstruct
	CXX=g++ UNAME=Linux SAGE64=auto scons
	ln -s libcsage.so.0 libcsage.so
    popd
    pushd sage/libs/mpmath
	dos2unix ext_impl.pxd ext_libmp.pyx ext_main.pxd ext_main.pyx
    popd
    python ./setup.py build
popd

#------------------------------------------------------------------------
pushd spkg/build/%{sagenb_pkg}/src/sagenb
    python ./setup.py build
popd

#------------------------------------------------------------------------
pushd spkg/build/%{flintqs_pkg}/src
    make %{?_smpflags} CPP="g++ %{optflags} -fPIC"
popd

pushd spkg/build/%{rubiks_pkg}/src
    make %{?_smp_mflags} CC="gcc -fPIC" CXX="g++ -fPIC" CFLAGS="%{optflags}" CXXFLAGS="%{optflags}"
popd

# last build command
rm -fr $DOT_SAGE

########################################################################
%install
export SAGE_ROOT=%{buildroot}%{SAGE_ROOT}
export SAGE_LOCAL=%{buildroot}%{SAGE_LOCAL}
export SAGE_SRC=%{buildroot}%{SAGE_SRC}
export SAGE_SHARE=%{buildroot}%{SAGE_SHARE}
export SAGE_EXTCODE=%{buildroot}%{SAGE_EXTCODE}
export SAGE_DOC=%{buildroot}%{SAGE_DOC}
export SAGE_PYTHONPATH=%{buildroot}%{SAGE_PYTHONPATH}
export LD_LIBRARY_PATH=%{buildroot}%{_libdir}:$LD_LIBRARY_PATH
export DESTDIR=%{buildroot}
export DOT_SAGE=/tmp/sage$$
mkdir -p $DOT_SAGE/tmp

export PATH=%{buildroot}%{_bindir}:$PATH
export PYTHONPATH=%{buildroot}%{python_sitearch}:$PYTHONPATH

#------------------------------------------------------------------------
mkdir -p %{buildroot}%{_bindir}
mkdir -p %{buildroot}%{_libdir}
mkdir -p $SAGE_PYTHONPATH
rm -fr $SAGE_LOCAL/{include,lib,share,notebook}
mkdir -p $SAGE_SHARE $SAGE_DOC $SAGE_LOCAL/bin $SAGE_SRC
ln -sf $PWD/spkg/build/sage-%{version}/sage $SAGE_SRC/sage
ln -sf %{_libdir} $SAGE_LOCAL/lib
ln -sf %{_includedir} $SAGE_LOCAL/include
ln -sf %{_datadir} $SAGE_LOCAL/share

#------------------------------------------------------------------------
pushd spkg/build/sage-%{version}
    python setup.py install --root=%{buildroot}
    cp -fa c_lib/libcsage.so.0 %{buildroot}%{_libdir}
    ln -sf libcsage.so.0 %{buildroot}%{_libdir}/libcsage.so
    pushd sage
	# install sage notebook templates
	cp -fa server/notebook/templates %{buildroot}%{python_sitearch}/sage/server/notebook
    popd
    # install documentation sources
    rm -fr $SAGE_DOC/{common,en,fr}
    cp -far doc/{common,de,en,fr,pt,ru,tr} $SAGE_DOC
popd

#------------------------------------------------------------------------
pushd spkg/build/%{sagenb_pkg}/src/sagenb
    rm -f %{buildroot}%{python_sitearch}/sagenb/data/sage3d/sage3d
    python setup.py install --root=%{buildroot} --install-purelib=%{python_sitearch}
    install -p -m0755 %{SOURCE5} $SAGE_LOCAL/bin/testjava.sh
    # jmol
    rm -fr %{buildroot}%{python_sitearch}/sagenb/data/jmol
    mkdir -p %{buildroot}%{python_sitearch}/sagenb/data/jmol/appletweb
    pushd %{buildroot}%{python_sitearch}/sagenb/data/jmol
	cp -fa %{SOURCE3} %{SOURCE4} appletweb
    popd
    # sage3d
    rm -f %{buildroot}%{_bindir}/sage3d
%if %{with_sage3d}
    ln -sf %{SAGE_LOCAL}/bin/sage3d %{buildroot}%{python_sitearch}/sagenb/data/sage3d/sage3d
%endif
    # flask stuff not installed
    cp -ar flask_version %{buildroot}%{python_sitearch}/sagenb
    ln -sf %{python_sitearch}/sagenb %{buildroot}%{SAGE_SRC}/sagenb
popd

#------------------------------------------------------------------------
%if %{with_sage_pexpect}
pushd spkg/build/%{pexpect_pkg}/src
    cp -fa {ANSI,FSM,pexpect,pxssh,screen}.py $SAGE_PYTHONPATH
popd
%endif

#------------------------------------------------------------------------
cp -fa COPYING.txt $SAGE_ROOT
pushd spkg/build/sage_scripts-%{version}
    mkdir -p $SAGE_LOCAL/bin
    cp -fa sage-* $SAGE_LOCAL/bin
    pushd $SAGE_LOCAL/bin
	ln -sf %{_bindir}/python sage.bin
	ln -sf %{_bindir}/gp sage_pari
	ln -sf %{_bindir}/gap gap_stamp
	ln -sf %{_bindir}/gmp-ecm ecm
    popd
popd
install -p -m755 spkg/bin/sage $SAGE_LOCAL/bin

#------------------------------------------------------------------------
pushd spkg/build/%{flintqs_pkg}/src
    cp -fa QuadraticSieve $SAGE_LOCAL/bin
popd

pushd spkg/build/%{rubiks_pkg}/src
    cp -fa \
	reid/optimal \
	dietz/solver/cubex \
	dietz/mcube/mcube \
	dietz/cu2/cu2 \
	dik/dikcube \
	dik/size222 \
	$SAGE_LOCAL/bin
popd

#------------------------------------------------------------------------
pushd $SAGE_LOCAL/bin/
    for file in \
	sage-apply-ticket \
	sage-bdist \
	sage-build \
	sage-clone \
	sage-combinat \
	sage-crap \
	sage-list-experimental \
	sage-list-optional \
	sage-list-packages \
	sage-list-standard \
	sage-location \
	sage-make_devel_packages \
	sage-omega \
	sage-pkg \
	sage-pull \
	sage-push \
	sage-pypkg-location \
	sage-README-osx.txt \
	sage-rebaseall.bat \
	sage-rebaseall.sh \
	sage-rebase.bat \
	sage-rebase.sh \
	sage-rsyncdist \
	sage-sdist \
	sage-spkg-install \
	sage-startuptime.py \
	sage-sync-build.py \
	sage-test-import \
	sage-update \
	sage-update-build \
	sage-upgrade \
	spkg-install; do
	rm -f $file
    done
popd

#------------------------------------------------------------------------
pushd spkg/build/%{conway_polynomials_pkg}
    %__python2 ./spkg-install
popd

#------------------------------------------------------------------------
pushd spkg/build/%{elliptic_curves_pkg}
    %__python2 ./spkg-install
popd

#------------------------------------------------------------------------
pushd spkg/build/extcode-%{version}
    mkdir -p $SAGE_EXTCODE
    for dir in 			\
	gap			\
	genus2reduction		\
	gnuplot			\
	images			\
	kash			\
	macaulay2		\
	magma			\
	maple			\
	matlab			\
	mathematica		\
	maxima			\
	MuPAD			\
	mwrank			\
	octave			\
	QEPCAD			\
	scilab			\
	singular		\
	sobj; do
	COUNT=`find $dir -type f | wc -l `
	if  $COUNT -gt 0 ]; then
	    cp -far $dir $SAGE_EXTCODE
	fi
	cp -far pari $SAGE_EXTCODE
    done
    cp -fa %{SOURCE1} $SAGE_EXTCODE/pari
popd

#------------------------------------------------------------------------
pushd spkg/build/%{graphs_pkg}
    mkdir -p $SAGE_SHARE/graphs
    cp -fa src/* $SAGE_SHARE/graphs
popd

#------------------------------------------------------------------------
pushd spkg/build/%{polytopes_db_pkg}
    mkdir -p $SAGE_SHARE/reflexive_polytopes
    cp -fa src/* $SAGE_SHARE/reflexive_polytopes
popd

#------------------------------------------------------------------------
pushd spkg/build/%{sagetex_pkg}/src
    python setup.py install --root=%{buildroot} --install-purelib=%{python_sitearch}
    install -p -m 0644 -D %{SOURCE2} \
	%{buildroot}%{_datadir}/texmf/tex/generic/sagetex/makecmds.sty
    mv %{buildroot}%{_docdir}/{sagetex,%{sagetex_pkg}}
    mv %{buildroot}%{_datadir}/texmf/tex/generic/sagetex/CONTRIBUTORS \
	 %{buildroot}%{_docdir}/%{sagetex_pkg}
    for file in PKG-INFO README; do
	install -p -m 0644 $file %{buildroot}%{_docdir}/%{sagetex_pkg}/$file
    done
popd

#------------------------------------------------------------------------
cat > %{buildroot}%{_bindir}/sage << EOF
#!/bin/sh

export CUR=\`pwd\`
##export DOT_SAGE="\$HOME/.sage"
export DOT_SAGENB="\$DOT_SAGE"
mkdir -p \$DOT_SAGE/{maxima,sympow,tmp}
export SAGE_TESTDIR=\$DOT_SAGE/tmp
export SAGE_ROOT="$SAGE_ROOT"
export SAGE_LOCAL="$SAGE_LOCAL"
export SAGE_SHARE="$SAGE_SHARE"
export SAGE_EXTCODE="$SAGE_EXTCODE"
export SAGE_SRC="$SAGE_SRC"
##export SAGE_DOC="$SAGE_DOC"
module load 4ti2-%{_arch}
%if %{have_lrcalc}
module load lrcalc-%{_arch}
%endif
module load surf-geometry-%{_arch}
export PATH=$SAGE_LOCAL/bin:\$PATH
export SINGULARPATH=%{_libdir}/Singular/LIB
export SINGULAR_BIN_DIR=%{_libdir}/Singular
##export PYTHONPATH="$SAGE_PYTHONPATH:\$SAGE_LOCAL/bin"
export SAGE_CBLAS=cblas
export SAGE_FORTRAN=%{_bindir}/gfortran
export SAGE_FORTRAN_LIB=\`gfortran --print-file-name=libgfortran.so\`
export SYMPOW_DIR="\$DOT_SAGE/sympow"
export LC_MESSAGES=C
export LC_NUMERIC=C
MALLOC_CHECK_=1 $SAGE_LOCAL/bin/sage "\$@"
EOF
#------------------------------------------------------------------------
chmod +x %{buildroot}%{_bindir}/sage

#------------------------------------------------------------------------
%if %{with_sage3d}
cat > %{buildroot}%{SAGE_LOCAL}/bin/sage3d << EOF
#!/bin/sh

java -classpath %{SAGE_SRC}/sage/sagenb/data/sage3d/lib/sage3d.jar:%{_javadir}/j3dcore.jar:%{_javadir}/vecmath.jar:%{_javadir}/j3dutils.jar org.sagemath.sage3d.ObjectViewerApp "\$1"
EOF
chmod +x %{buildroot}%{SAGE_LOCAL}/bin/sage3d
%endif

#------------------------------------------------------------------------
# adjust cython interface:
# o link with proper atlas
# o install csage headers
# o install .pxi and .pxd files
pushd spkg/build/sage-%{version}
    # make atlas/blas available to compiled sources
    perl -pi -e								\
	's|^(extra_link_args =).*|$1 "-L%{_libdir}/atlas"]|;'		\
	%{buildroot}%{python_sitearch}/sage/misc/cython.py
    # make csage headers available
    mkdir -p %{buildroot}%{_includedir}/csage
    cp -fa c_lib/include/* %{buildroot}%{_includedir}/csage
    for f in `find sage \( -name \*.pxi -o -name \*.pxd -o -name \*.pyx \)`; do
	install -p -D -m 0644 $f %{buildroot}%{python_sitearch}/$f
    done
    # need this or will not "find" the files in the directory, and
    # fail to link with gmp
    touch %{buildroot}%{python_sitearch}/sage/libs/gmp/__init__.py
popd

#------------------------------------------------------------------------
%if %{with_sage_pexpect}
    cp -f $SAGE_PYTHONPATH/{ANSI,FSM,pexpect,pxssh,screen}.py %{buildroot}%{python_sitearch}
%endif

# Build documentation, using %#{buildroot} environment
pushd spkg/build/sage-%{version}/doc
    export SAGE_DOC=`pwd`
    export PATH=%{buildroot}%{_bindir}:$SAGE_LOCAL/bin:$PATH
    export SINGULARPATH=%{_libdir}/Singular/LIB
    export SINGULAR_BIN_DIR=%{_libdir}/Singular
    export LD_LIBRARY_PATH=%{buildroot}%{_libdir}:%{_libdir}/atlas:$LD_LIBRARY_PATH
    export PYTHONPATH=%{buildroot}%{python_sitearch}:$SAGE_PYTHONPATH:$SAGE_DOC

%if %{with_sphinx_hack}
    cp -far %{python_sitelib}/sphinx %{buildroot}%{python_sitearch}
    sed -i "s|\(source.startswith('>>>')\)|\1 or source.startswith('sage: ')|" \
	%{buildroot}%{python_sitearch}/sphinx/highlighting.py
%endif

    # there we go
    ln -sf %{buildroot}%{SAGE_DOC} $SAGE_SRC/doc
    python common/builder.py all html
    export SAGE_DOC=%{buildroot}%{SAGE_DOC}
    cp -far output $SAGE_DOC

    # should not be required and encodes buildroot
    rm -fr $SAGE_DOC/output/doctrees
    sed -e 's|%{buildroot}||g' -i $SAGE_DOC/output/html/en/reference/sage/misc/hg.html
popd

%if %{with_check}
export SAGE_TIMEOUT=%{SAGE_TIMEOUT}
export SAGE_TIMEOUT_LONG=%{SAGE_TIMEOUT_LONG}
sage -testall --verbose || :
install -p -m644 $DOT_SAGE/tmp/test.log $SAGE_DOC/test.log
# remove buildroot references from test.log
sed -i 's|%{buildroot}||g' $SAGE_DOC/test.log
%endif

%if %{with_sage_pexpect}
    rm -f %{buildroot}%{python_sitearch}/{ANSI,FSM,pexpect,pxssh,screen}.py{,c}
%endif

%if %{with_sphinx_hack}
    rm -fr %{buildroot}%{python_sitearch}/sphinx
%endif

# Script was used to build documentation 
perl -pi -e 's|%{buildroot}||g;s|^##||g;' %{buildroot}%{_bindir}/sage

# More wrong buildroot references
perl -pi -e 's|%{buildroot}||g;' \
	 -e "s|$PWD/spkg/build/sage-%{version}/doc|%{SAGE_DOC}|g;" \
    %{buildroot}%{SAGE_DOC}/output/html/en/reference/todolist.html \
    %{buildroot}%{SAGE_DOC}/output/html/en/reference/misc/sage/misc/hg.html

#------------------------------------------------------------------------
# Fix links
rm -fr $SAGE_SRC/sage $SAGE_EXTCODE/sage $SAGE_ROOT/doc $SAGE_SRC/doc
rm -fr $SAGE_ROOT/share $SAGE_ROOT/devel
ln -sf %{python_sitearch}/sage $SAGE_SRC/sage
ln -sf %{python_sitearch} $SAGE_EXTCODE/sage
ln -sf %{SAGE_DOC} $SAGE_ROOT/doc
ln -sf %{SAGE_DOC} $SAGE_SRC/doc
ln -sf %{SAGE_SHARE} $SAGE_ROOT/share
# compat devel symlink
ln -sf src $SAGE_ROOT/devel

# Install menu and icons
pushd spkg/build/%{sagenb_pkg}/src/sagenb/sagenb/data
    install -p -m644 -D sage/images/icon32x32.png %{buildroot}%{_datadir}/pixmaps/%{name}.png
popd
mkdir -p %{buildroot}%{_datadir}/applications
cat > %{buildroot}%{_datadir}/applications/%{name}.desktop << EOF
[Desktop Entry]
Name=Sagemath
Comment=A free open-source mathematics software system
Exec=sage
Icon=%{name}
Terminal=true
Type=Application
Categories=Science;Math;
EOF
desktop-file-validate %{buildroot}%{_datadir}/applications/%{name}.desktop

# Fix permissions
find %{buildroot} -name '*.so' | xargs chmod 755
pushd %{buildroot}%{SAGE_LOCAL}/bin
    chmod 755 QuadraticSieve
    chmod 755 mcube dikcube cu2 size222 cubex optimal
popd
for file in `find %{buildroot} -name \*.py`; do
    if head -1 $file | grep -q '^#!'; then
	chmod +x $file
    fi
done
chmod -x %{buildroot}%{SAGE_DOC}/en/prep/media/Rplot001.png

# Documentation is not rebuilt (also corrects rpmlint warning of hidden file)
find %{buildroot}%{SAGE_DOC} -name .buildinfo -exec rm {} \;
rm -fr %{buildroot}%{SAGE_DOC}/output/inventory
find %{buildroot}%{SAGE_DOC} -type d -name _sources | xargs rm -fr

# remove bundles fonts
rm -r %{buildroot}%{python_sitearch}/sagenb/data/mathjax/fonts

# remove .po files
rm %{buildroot}%{python_sitearch}/sagenb/translations/*/LC_MESSAGES/*.po

%if !%{with_sage3d}
rm -r %{buildroot}%{python_sitearch}/sagenb/data/sage3d
%endif

# remove cache files
rm -r %{buildroot}%{python_sitearch}/sagenb/data/.webassets-cache

# last install command
rm -fr $DOT_SAGE

########################################################################
%post		core
/sbin/ldconfig
exit 0

%postun		core
/sbin/ldconfig
exit 0

# Use symlinks and a minor patch to the notebook to not bundle jmol
%post		notebook
ln -sf %{_javadir}/JmolApplet.jar %{python_sitearch}/sagenb/data/jmol/
ln -sf %{_javadir}/vecmath.jar %{python_sitearch}/sagenb/data/jmol/
exit 0

%postun		notebook
if  $1 -eq 0 ] ; then
    rm -f %{python_sitearch}/sagenb/data/jmol/JmolApplet.jar
    rm -f %{python_sitearch}/sagenb/data/jmol/vecmath.jar
    rmdir %{python_sitearch}/sagenb/data/jmol &&
	rmdir %{python_sitearch}/sagenb/data &&
	    rmdir %{python_sitearch}/sagenb
fi
exit 0

%post		sagetex
%{_bindir}/mktexlsr
exit 0

%postun		sagetex
if  $1 -eq 0 ] ; then
    %{_bindir}/mktexlsr
fi
exit 0

########################################################################
%files
# GPLv2+
%dir %{SAGE_ROOT}
%doc %{SAGE_ROOT}/COPYING.txt
%dir %{SAGE_LOCAL}
%dir %{SAGE_LOCAL}/bin
%{SAGE_LOCAL}/bin/QuadraticSieve
%{SAGE_LOCAL}/bin/ecm
%{SAGE_LOCAL}/bin/gap_stamp
%{SAGE_LOCAL}/bin/sage*
%{SAGE_LOCAL}/bin/testjava.sh
%{SAGE_LOCAL}/include
%{SAGE_LOCAL}/lib
%{SAGE_LOCAL}/share
%{SAGE_ROOT}/doc
%{SAGE_ROOT}/devel
%{SAGE_ROOT}/share
%dir %{SAGE_SRC}
%{SAGE_SRC}/doc
%{SAGE_SRC}/sage
%dir %{SAGE_PYTHONPATH}
%if %{with_sage_pexpect}
# MIT
%{SAGE_PYTHONPATH}/*.py*
%endif
# GPLv2+
%{_bindir}/sage
%{_datadir}/pixmaps/%{name}.png
%{_datadir}/applications/%{name}.desktop

#------------------------------------------------------------------------
%files		core
# GPLv2+
%{_libdir}/libcsage.so.*
%{python_sitearch}/sage
%{python_sitearch}/sage-*.egg-info

#------------------------------------------------------------------------
%files		data
%dir %{SAGE_SHARE}
%dir %{SAGE_EXTCODE}
%{SAGE_EXTCODE}/sage

#------------------------------------------------------------------------
%files		data-conway_polynomials
# GPLv2+
%{SAGE_SHARE}/conway_polynomials

#------------------------------------------------------------------------
%files		data-elliptic_curves
# GPLv2+
%{SAGE_SHARE}/cremona
%{SAGE_SHARE}/ellcurves

#------------------------------------------------------------------------
%files		data-extcode
# GPLv2+
%{SAGE_EXTCODE}/gap
%{SAGE_EXTCODE}/images
%{SAGE_EXTCODE}/magma
%{SAGE_EXTCODE}/maxima
%{SAGE_EXTCODE}/mwrank
%{SAGE_EXTCODE}/pari
%{SAGE_EXTCODE}/singular

#------------------------------------------------------------------------
%files		data-graphs
# GPLv2+
%{SAGE_SHARE}/graphs

#------------------------------------------------------------------------
%files		data-polytopes_db
# GPL+
%{SAGE_SHARE}/reflexive_polytopes

#------------------------------------------------------------------------
%files		devel
# GPLv2+
%{_includedir}/csage
%{_libdir}/libcsage.so

#------------------------------------------------------------------------
%files		doc
# GPLv2+
%dir %{SAGE_DOC}
%{SAGE_DOC}/common
%dir %{SAGE_DOC}/output
%dir %{SAGE_DOC}/output/html

#------------------------------------------------------------------------
%files		doc-de
# GPLv2+
%{SAGE_DOC}/de
%{SAGE_DOC}/output/html/de

#------------------------------------------------------------------------
%files		doc-en
# GPLv2+
%{SAGE_DOC}/en
%{SAGE_DOC}/output/html/en

#------------------------------------------------------------------------
%files		doc-fr
# GPLv2+
%{SAGE_DOC}/fr
%{SAGE_DOC}/output/html/fr

#------------------------------------------------------------------------
%files		doc-pt
# GPLv2+
%{SAGE_DOC}/pt
%{SAGE_DOC}/output/html/pt

#------------------------------------------------------------------------
%files		doc-ru
# GPLv2+
%{SAGE_DOC}/ru
%{SAGE_DOC}/output/html/ru

#------------------------------------------------------------------------
%files		doc-tr
# GPLv2+
%{SAGE_DOC}/tr
%{SAGE_DOC}/output/html/tr

#------------------------------------------------------------------------
%files		notebook
# GPLv2+
%{SAGE_SRC}/sagenb
%dir %{python_sitearch}/sagenb
%{python_sitearch}/sagenb/*.py*
%{python_sitearch}/sagenb-*.egg-info
%dir %{python_sitearch}/sagenb/data
# BSD
%{python_sitearch}/sagenb/data/codemirror
# MIT
%{python_sitearch}/sagenb/data/graph_editor
# ASL 2.0
%{python_sitearch}/sagenb/data/highlight
# LGPLv2+
%{python_sitearch}/sagenb/data/jmol
# (MIT or GPLv2) and (MIT and BSD and GPL)
%{python_sitearch}/sagenb/data/jquery
# (MIT or GPLv2) and (MIT and BSD and GPL)
%{python_sitearch}/sagenb/data/jqueryui
# Public Domain
%{python_sitearch}/sagenb/data/json
# ASL 2.0
%{python_sitearch}/sagenb/data/mathjax
# Empty (do not run doctests flag file)
%{python_sitearch}/sagenb/data/nodoctest.py*
# BSD
%{python_sitearch}/sagenb/data/openid-realselector
# GPLv2+
%{python_sitearch}/sagenb/data/sage
%if %{with_sage3d}
# GPLv2+
%{python_sitearch}/sagenb/data/sage3d
%endif
# LGPLv2+
%{python_sitearch}/sagenb/data/tiny_mce
# Auto generated files
%{python_sitearch}/sagenb/data/webassets_generated
# LGPLv2+
%{python_sitearch}/sagenb/data/zorn
# GPLv2+
%{python_sitearch}/sagenb/flask_version
# GPLv2+
%{python_sitearch}/sagenb/interfaces
# GPLv2+
%{python_sitearch}/sagenb/misc
# GPLv2+
%{python_sitearch}/sagenb/notebook
# GPLv2+
%{python_sitearch}/sagenb/simple
# GPLv2+
%{python_sitearch}/sagenb/storage
# GPLv2+
%dir %{python_sitearch}/sagenb/testing
%{python_sitearch}/sagenb/testing/*.py*
%{python_sitearch}/sagenb/testing/tests
# ASL 2.0
%{python_sitearch}/sagenb/testing/selenium
# GPLv2+
%dir %{python_sitearch}/sagenb/translations
%lang(cs_CZ) %{python_sitearch}/sagenb/translations/cs_CZ
%lang(de_AT) %{python_sitearch}/sagenb/translations/de_AT
%lang(pt_BR) %{python_sitearch}/sagenb/translations/pt_BR
%lang(ru_RU) %{python_sitearch}/sagenb/translations/ru_RU

#------------------------------------------------------------------------
%files		rubiks
# GPL+
%{SAGE_LOCAL}/bin/optimal
# MIT
%{SAGE_LOCAL}/bin/dikcube
%{SAGE_LOCAL}/bin/size222
# GPL+
%{SAGE_LOCAL}/bin/cu2
%{SAGE_LOCAL}/bin/cubex
%{SAGE_LOCAL}/bin/mcube

#------------------------------------------------------------------------
%files		sagetex
# GPLv2+
%{python_sitearch}/sagetex*
%{_datadir}/texmf/tex/generic/sagetex
%doc %{_docdir}/%{sagetex_pkg}