#!/bin/bash

cat >LICENSE.txt <<\EOF
The GAATI Research Group Logo.

Copyright (C) 2015-2018, Gaetan Bisson. All rights reserved.

Permission to use, copy, and/or distribute the GAATI Research Group Logo
(hereinafter, "the logo"), as depicted by the files in this directory or
in any other form, is hereby granted, provided that:

1. The logo has not been modified in any way.

2. There is no commercial intent behind the use.

3. The use actually refers to the GAATI Research Group.

4. It does not suggest approval by, sponsorship from, or
   affiliation with the GAATI Research Group.

5. Any copy or redistribution is subject to this very license.

Condition 4 can be waived by obtaining explicit permission from
the GAATI Research Group head. See the group website for contact
information:

	https://gaati.org/
EOF

TEX=texput.tex
rm -f $TEX

cat >>$TEX <<\EOF
\documentclass[a4paper]{article}
\usepackage[french]{babel}
\usepackage{beautiful}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetikzlibrary{decorations.text}
\pagestyle{empty}
\begin{document}

\begin{tikzpicture}
\draw[thick,color=black] (0,0) circle (3.9);
\begin{scope}
\tkzDefPoint(0,0){O}
\tkzDefPoint(3.1,0){P}
\tkzClipCircle(O,P)
EOF

gp -fq >>$TEX <<\EOF
{
	default(realprecision, 5);
	for(i=3,5,
		M=2*i;
		for(j=0,M-1,
			z=4*exp(2*I*Pi*j/M)*(1-1/i);
			printf("\\tkzDefPoint(%f,%f){p%i%i}\n", real(z), imag(z), i, j);
		);
		for(j=0,M-1,
			printf("\\tkzDrawCircle[orthogonal through=p%i%i and p%i%i](O,P)\n", i, j, i, (j+1)%M);
		);
	);
}
EOF

cat >>$TEX <<\EOF
\end{scope}
\path[postaction={decorate,decoration={
                raise=-0.3ex,
                reverse path,
                text along path,
                text align/center,
                text={|\LARGE\scshape\bfseries|laboratoire~~~de~~~mathématiques}
}}] (10:3.4cm) arc (10:170:3.4cm);
\path[postaction={decorate,decoration={
                raise=-1.5ex,
                text along path,
                text align/center,
                text={|\LARGE\scshape\bfseries|de~~la~~polynésie~~française}
}}] (190:3.4cm) arc (190:350:3.4cm);
\node at (-0.05,0.01) {
\fontsize{37}{50}\selectfont$\boldsymbol{\mathbb{
G\mkern-3mu
A\mkern-3mu
A\mkern-4mu
T\mkern-1mu
I
}}$};
\end{tikzpicture}
\end{document}
EOF

lualatex $TEX
pdfcrop -margins 1 texput.pdf gaati.pdf
mutool draw -r 900 -o texput0.ppm texput.pdf

gm composite -compose Multiply -geometry +0+4 texput{0,0,1}.ppm
gm composite -compose Multiply -geometry +4+0 texput{0,1,2}.ppm
gm composite -compose Multiply -geometry -0-4 texput{0,2,3}.ppm
gm composite -compose Multiply -geometry -4-0 texput{0,3,4}.ppm
gm convert texput4.ppm -trim texput5.ppm
potrace -b pdf texput5.ppm
gs -o texput6.pdf -sDEVICE=pdfwrite -sPAPERSIZE=a7 -dFIXEDMEDIA -dPDFFitPage texput5.pdf
pdfcrop texput6.pdf texput7.pdf
pdftocairo -pdf texput7.pdf gaati.pdf
#cp gaati.pdf ../gaati.pdf

mutool draw -r 300 -o texput8.ppm gaati.pdf
gm convert texput8.ppm -trim -type Optimize -transparent white texput9.png
gm convert texput9.png -resize 500x500 gaati-500.png
gm convert texput9.png -resize 250x250 gaati-250.png
gm convert texput9.png -resize 125x125 gaati-125.png

#cp gaati-125.png ../gaati.png
optipng -o7 ../gaati.png

rm -f texput*
