\RequirePackage{tikz}
\usetikzlibrary{calc,positioning}

\newcommand{\module}[2]{#2\\[-4pt]\color{gray}{\tiny(#1)}}
\newcommand{\tinymodule}[2]{\tiny\module{#1}{#2}}

\tikzstyle{block}=[rectangle,align=center]
\tikzset{
	doc/.pic={
		\coordinate (-east)  at (0.309,0);
		\coordinate (-west)  at (-0.309,0);
		\coordinate (-south) at (0,-0.4);
		\coordinate (-north) at (0,0.4);
		\draw[pic actions] (-0.309,-0.4) -- ++(0,0.8) -- ++(0.518,0) -- ++(0,-0.1) -- ++(0.1,0) -- ++(0,-0.7) -- cycle;
		\draw[pic actions] (0.209,0.4) -- ++(0.1,-0.1);
		\draw[pic actions] (-0.209,-0.1) -- ++(0.418,0);
		\draw[pic actions] (-0.209, 0)   -- ++(0.418,0);
		\draw[pic actions] (-0.209, 0.1) -- ++(0.418,0);
	},
	db/.pic={
		% https://tex.stackexchange.com/a/397862/23992
		\coordinate (-west)  at (-0.3,0);
		\coordinate (-east)  at ( 0.3,0);
		\coordinate (-south) at (0,-0.42);
		\foreach \y in {-0.33,-0.1,0.13} {
			\draw[fill=white] (-0.3,\y) to [looseness=0.5,bend right=90] ++(0.6,0)
				-- ++(0,0.2) to [looseness=0.5,bend left=90] ++(-0.6,0)
				-- ++(0,-0.2);
			\draw (-0.3,\y+0.2) edge[looseness=0.5,bend left=90] ++(0.6,0);
		}
	}
}

\def\ifold#1{\ifnum#1=0 }
\def\ifnew#1{\ifnum#1>0 }
\def\oldornew#1#2{#2\ifnew#1-1\fi}
\def\grayedout#1{\ifnum#1<2 dark\fi gray}

\def\tikzsource{%
	\pic (src)  at (-5,3) {doc};
	\node[left=3pt of src-west,draw=none,text width=3em] {\scriptsize Source\\code};
}
\newcommand{\tikzbuilddb}[1][0]{%
	\node  (cocl) at (-3,   3.5 ) {\module{Clean}{Clean compiler}};
	\ifold#1
		\node (ppr) at (-2.5, 2.5 ) {\module{Clean}{Pretty printer}};
	\else
		\node (ppr) at (-2.5, 2.2 ) {\module{Clean}{Pretty printer}};
	\fi
	\pic     (db) at ( 0,   2.98) {db};
	\node[right=3pt of db-east,draw=none] {\module{\tiny JSON}{\scriptsize Database}};
	\draw[->] (src-east) -- (cocl.west);
	\draw[->] (cocl) -- (ppr);
	\ifold#1
		\draw[->] (cocl.east) -- ($(db-west)+(0,0.18)$);
		\draw[->] (ppr.east)  -- ($(db-west)-(0,0.18)$);
	\fi
}
\newcommand{\tikzbackend}[1][0]{%
	\node[color=\grayedout#1] (web) at (0, \oldornew{#1}{1}) {\module{Clean, PHP, JS}{\ifold#1\bf\fi cloogle.org}};
	\node (uni) at (3, 3) {\module{Clean}{Type unifier}};
	\ifold#1
		\draw[->] (db-south) -- (web);
		\draw (uni) -- (web);
	\else
		\node (core) at (0,1.5) {\module{Clean}{\textbf{Cloogle}}};
		\draw[->] (cocl) edge[bend left] (core);
		\draw[->] (ppr)  -- (core);
		\draw[<->] (core) -- (db-south);
		\draw (uni) -- (core);
		\draw[->] (db-south) -- (core);
		\draw[color=\grayedout#1,->] (core) -- (web);
	\fi
}
\newcommand{\tikzcache}[1][0]{%
	\pic[color=\grayedout#1] (cch) at (2.5, \oldornew#1{1.48}) {db};
	\node[color=\grayedout#1,right=3pt of cch-east,draw=none] {\module{\tiny JSON}{\scriptsize Cache}};
	\draw[color=\grayedout#1,<->] (cch-west) -- (web);
}
\newcommand{\tikzfrontend}[1][0]{%
	\node[color=\grayedout#1] (hljs) at (-2, \oldornew#1{0.5}) {\module{JS}{clean.js}};
	\draw[color=\grayedout#1] (hljs) -- (web);
}
\newcommand{\tikzfrontendsrc}[1][0]{%
	\node[color=\grayedout#1] (pygm) at (-2.25, \oldornew#1{1.5}) {\module{Python}{pygments}};
	\draw[color=\grayedout#1,->] (src-south) -- (pygm);
	\draw[color=\grayedout#1] (pygm) -- (web);
}
\newcommand{\tikzstats}[1][0]{%
	\node[color=\grayedout#1] (stats) at (2.5, \oldornew#1{0.5}) {\module{Bash, JS, PHP, SQL}{Log \& Statistics}};
	\draw[color=\grayedout#1] (web) -- (stats);
}
\newcommand{\tikzclientA}[1][0]{%
	\def\tikzclientAcolor{\ifnum#1=3 \else dark\fi gray}
	\node[color=\tikzclientAcolor] (vim) at (-3,-2) {\tinymodule{Vim}{vim-clean}};
	\draw[color=\grayedout#1,->] (web) -- (vim);
	\ifnum#1>2
		\pic (tagfile) at (-5,-1.2) {doc};
		\node[left=3pt of tagfile-west,draw=none] {\scriptsize Tagfile};
		\draw[->] (tags) -- (tagfile-north);
		\ifnum#1>3
			\draw (tagfile-south) edge[bend right,->] (vim);
		\fi
	\fi
}
\newcommand{\tikzclientB}[1][0]{%
	\node[color=\grayedout#1] (tgm) at (-1.5,-2) {\tinymodule{Python}{Telegram bot}};
	\draw[color=\grayedout#1,->] (web) -- (tgm);
}
\newcommand{\tikzclientC}[1][0]{%
	\node[color=\grayedout#1] (cli) at (0,-2) {\tinymodule{Python}{CLI app}};
	\draw[color=\grayedout#1,->] (web) -- (cli);
}
\newcommand{\tikzclientD}[1][0]{%
	\node[color=\grayedout#1] (vim) at (1.4,-2) {\tinymodule{Python}{Email client}};
	\draw[color=\grayedout#1,->] (web) -- (vim);
}
\newcommand{\tikzclientE}[1][0]{%
	\node[color=\grayedout#1] (vim) at (2.7,-2) {\tinymodule{Clean}{IRC bot}};
	\draw[color=\grayedout#1,->] (web) -- (vim);
}
\newcommand{\tikzcloogletags}[0]{%
	\node (tags) at (-5,0) {\module{Clean}{cloogle-tags}};
	\draw[->] (core) edge[bend right,out=-15] (tags);
}
