xref: /openbmc/linux/scripts/sphinx-pre-install (revision a8b380c379efcade6008cb5e2359073b72e73ad8)
124071ac1SMauro Carvalho Chehab#!/usr/bin/perl
2c942fddfSThomas Gleixner# SPDX-License-Identifier: GPL-2.0-or-later
324071ac1SMauro Carvalho Chehabuse strict;
424071ac1SMauro Carvalho Chehab
52f9c5025SMauro Carvalho Chehab# Copyright (c) 2017-2020 Mauro Carvalho Chehab <mchehab@kernel.org>
624071ac1SMauro Carvalho Chehab#
724071ac1SMauro Carvalho Chehab
88c69b77aSMike Rapoportmy $prefix = "./";
98c69b77aSMike Rapoport$prefix = "$ENV{'srctree'}/" if ($ENV{'srctree'});
108c69b77aSMike Rapoport
118c69b77aSMike Rapoportmy $conf = $prefix . "Documentation/conf.py";
128c69b77aSMike Rapoportmy $requirement_file = $prefix . "Documentation/sphinx/requirements.txt";
1344f42165SMauro Carvalho Chehabmy $virtenv_prefix = "sphinx_";
145be33182SMauro Carvalho Chehab
1524071ac1SMauro Carvalho Chehab#
1624071ac1SMauro Carvalho Chehab# Static vars
1724071ac1SMauro Carvalho Chehab#
1824071ac1SMauro Carvalho Chehab
1924071ac1SMauro Carvalho Chehabmy %missing;
2024071ac1SMauro Carvalho Chehabmy $system_release;
2124071ac1SMauro Carvalho Chehabmy $need = 0;
2224071ac1SMauro Carvalho Chehabmy $optional = 0;
2324071ac1SMauro Carvalho Chehabmy $need_symlink = 0;
2424071ac1SMauro Carvalho Chehabmy $need_sphinx = 0;
252f9c5025SMauro Carvalho Chehabmy $need_venv = 0;
262f9c5025SMauro Carvalho Chehabmy $need_virtualenv = 0;
2777d09ad9SMauro Carvalho Chehabmy $rec_sphinx_upgrade = 0;
2824071ac1SMauro Carvalho Chehabmy $install = "";
2944f42165SMauro Carvalho Chehabmy $virtenv_dir = "";
302f9c5025SMauro Carvalho Chehabmy $python_cmd = "";
3144f42165SMauro Carvalho Chehabmy $min_version;
3224071ac1SMauro Carvalho Chehab
3324071ac1SMauro Carvalho Chehab#
3424071ac1SMauro Carvalho Chehab# Command line arguments
3524071ac1SMauro Carvalho Chehab#
3624071ac1SMauro Carvalho Chehab
3724071ac1SMauro Carvalho Chehabmy $pdf = 1;
3824071ac1SMauro Carvalho Chehabmy $virtualenv = 1;
399b88ad54SMauro Carvalho Chehabmy $version_check = 0;
4024071ac1SMauro Carvalho Chehab
4124071ac1SMauro Carvalho Chehab#
4224071ac1SMauro Carvalho Chehab# List of required texlive packages on Fedora and OpenSuse
4324071ac1SMauro Carvalho Chehab#
4424071ac1SMauro Carvalho Chehab
4524071ac1SMauro Carvalho Chehabmy %texlive = (
4624071ac1SMauro Carvalho Chehab	'amsfonts.sty'       => 'texlive-amsfonts',
4724071ac1SMauro Carvalho Chehab	'amsmath.sty'        => 'texlive-amsmath',
4824071ac1SMauro Carvalho Chehab	'amssymb.sty'        => 'texlive-amsfonts',
4924071ac1SMauro Carvalho Chehab	'amsthm.sty'         => 'texlive-amscls',
5024071ac1SMauro Carvalho Chehab	'anyfontsize.sty'    => 'texlive-anyfontsize',
5124071ac1SMauro Carvalho Chehab	'atbegshi.sty'       => 'texlive-oberdiek',
5224071ac1SMauro Carvalho Chehab	'bm.sty'             => 'texlive-tools',
5324071ac1SMauro Carvalho Chehab	'capt-of.sty'        => 'texlive-capt-of',
5424071ac1SMauro Carvalho Chehab	'cmap.sty'           => 'texlive-cmap',
5524071ac1SMauro Carvalho Chehab	'ecrm1000.tfm'       => 'texlive-ec',
5624071ac1SMauro Carvalho Chehab	'eqparbox.sty'       => 'texlive-eqparbox',
5724071ac1SMauro Carvalho Chehab	'eu1enc.def'         => 'texlive-euenc',
5824071ac1SMauro Carvalho Chehab	'fancybox.sty'       => 'texlive-fancybox',
5924071ac1SMauro Carvalho Chehab	'fancyvrb.sty'       => 'texlive-fancyvrb',
6024071ac1SMauro Carvalho Chehab	'float.sty'          => 'texlive-float',
6124071ac1SMauro Carvalho Chehab	'fncychap.sty'       => 'texlive-fncychap',
6224071ac1SMauro Carvalho Chehab	'footnote.sty'       => 'texlive-mdwtools',
6324071ac1SMauro Carvalho Chehab	'framed.sty'         => 'texlive-framed',
6424071ac1SMauro Carvalho Chehab	'luatex85.sty'       => 'texlive-luatex85',
6524071ac1SMauro Carvalho Chehab	'multirow.sty'       => 'texlive-multirow',
6624071ac1SMauro Carvalho Chehab	'needspace.sty'      => 'texlive-needspace',
6724071ac1SMauro Carvalho Chehab	'palatino.sty'       => 'texlive-psnfss',
6824071ac1SMauro Carvalho Chehab	'parskip.sty'        => 'texlive-parskip',
6924071ac1SMauro Carvalho Chehab	'polyglossia.sty'    => 'texlive-polyglossia',
7024071ac1SMauro Carvalho Chehab	'tabulary.sty'       => 'texlive-tabulary',
7124071ac1SMauro Carvalho Chehab	'threeparttable.sty' => 'texlive-threeparttable',
7224071ac1SMauro Carvalho Chehab	'titlesec.sty'       => 'texlive-titlesec',
7324071ac1SMauro Carvalho Chehab	'ucs.sty'            => 'texlive-ucs',
7424071ac1SMauro Carvalho Chehab	'upquote.sty'        => 'texlive-upquote',
7524071ac1SMauro Carvalho Chehab	'wrapfig.sty'        => 'texlive-wrapfig',
7624071ac1SMauro Carvalho Chehab);
7724071ac1SMauro Carvalho Chehab
7824071ac1SMauro Carvalho Chehab#
7924071ac1SMauro Carvalho Chehab# Subroutines that checks if a feature exists
8024071ac1SMauro Carvalho Chehab#
8124071ac1SMauro Carvalho Chehab
8224071ac1SMauro Carvalho Chehabsub check_missing(%)
8324071ac1SMauro Carvalho Chehab{
8424071ac1SMauro Carvalho Chehab	my %map = %{$_[0]};
8524071ac1SMauro Carvalho Chehab
8624071ac1SMauro Carvalho Chehab	foreach my $prog (sort keys %missing) {
8724071ac1SMauro Carvalho Chehab		my $is_optional = $missing{$prog};
8824071ac1SMauro Carvalho Chehab
8956e5a633SMauro Carvalho Chehab		# At least on some LTS distros like CentOS 7, texlive doesn't
9056e5a633SMauro Carvalho Chehab		# provide all packages we need. When such distros are
9156e5a633SMauro Carvalho Chehab		# detected, we have to disable PDF output.
9256e5a633SMauro Carvalho Chehab		#
9356e5a633SMauro Carvalho Chehab		# So, we need to ignore the packages that distros would
9456e5a633SMauro Carvalho Chehab		# need for LaTeX to work
9556e5a633SMauro Carvalho Chehab		if ($is_optional == 2 && !$pdf) {
9656e5a633SMauro Carvalho Chehab			$optional--;
9756e5a633SMauro Carvalho Chehab			next;
9856e5a633SMauro Carvalho Chehab		}
9956e5a633SMauro Carvalho Chehab
10024071ac1SMauro Carvalho Chehab		if ($is_optional) {
10124071ac1SMauro Carvalho Chehab			print "Warning: better to also install \"$prog\".\n";
10224071ac1SMauro Carvalho Chehab		} else {
10324071ac1SMauro Carvalho Chehab			print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
10424071ac1SMauro Carvalho Chehab		}
10524071ac1SMauro Carvalho Chehab		if (defined($map{$prog})) {
10624071ac1SMauro Carvalho Chehab			$install .= " " . $map{$prog};
10724071ac1SMauro Carvalho Chehab		} else {
10824071ac1SMauro Carvalho Chehab			$install .= " " . $prog;
10924071ac1SMauro Carvalho Chehab		}
11024071ac1SMauro Carvalho Chehab	}
11124071ac1SMauro Carvalho Chehab
11224071ac1SMauro Carvalho Chehab	$install =~ s/^\s//;
11324071ac1SMauro Carvalho Chehab}
11424071ac1SMauro Carvalho Chehab
11524071ac1SMauro Carvalho Chehabsub add_package($$)
11624071ac1SMauro Carvalho Chehab{
11724071ac1SMauro Carvalho Chehab	my $package = shift;
11824071ac1SMauro Carvalho Chehab	my $is_optional = shift;
11924071ac1SMauro Carvalho Chehab
12024071ac1SMauro Carvalho Chehab	$missing{$package} = $is_optional;
12124071ac1SMauro Carvalho Chehab	if ($is_optional) {
12224071ac1SMauro Carvalho Chehab		$optional++;
12324071ac1SMauro Carvalho Chehab	} else {
12424071ac1SMauro Carvalho Chehab		$need++;
12524071ac1SMauro Carvalho Chehab	}
12624071ac1SMauro Carvalho Chehab}
12724071ac1SMauro Carvalho Chehab
12824071ac1SMauro Carvalho Chehabsub check_missing_file($$$)
12924071ac1SMauro Carvalho Chehab{
130ff8fdb36SJeremy MAURO	my $files = shift;
13124071ac1SMauro Carvalho Chehab	my $package = shift;
13224071ac1SMauro Carvalho Chehab	my $is_optional = shift;
13324071ac1SMauro Carvalho Chehab
134ff8fdb36SJeremy MAURO	for (@$files) {
135ff8fdb36SJeremy MAURO		return if(-e $_);
136ff8fdb36SJeremy MAURO	}
13724071ac1SMauro Carvalho Chehab
13824071ac1SMauro Carvalho Chehab	add_package($package, $is_optional);
13924071ac1SMauro Carvalho Chehab}
14024071ac1SMauro Carvalho Chehab
14124071ac1SMauro Carvalho Chehabsub findprog($)
14224071ac1SMauro Carvalho Chehab{
14324071ac1SMauro Carvalho Chehab	foreach(split(/:/, $ENV{PATH})) {
14424071ac1SMauro Carvalho Chehab		return "$_/$_[0]" if(-x "$_/$_[0]");
14524071ac1SMauro Carvalho Chehab	}
14624071ac1SMauro Carvalho Chehab}
14724071ac1SMauro Carvalho Chehab
14824071ac1SMauro Carvalho Chehabsub check_program($$)
14924071ac1SMauro Carvalho Chehab{
15024071ac1SMauro Carvalho Chehab	my $prog = shift;
15124071ac1SMauro Carvalho Chehab	my $is_optional = shift;
15224071ac1SMauro Carvalho Chehab
1532f9c5025SMauro Carvalho Chehab	return $prog if findprog($prog);
15424071ac1SMauro Carvalho Chehab
15524071ac1SMauro Carvalho Chehab	add_package($prog, $is_optional);
15624071ac1SMauro Carvalho Chehab}
15724071ac1SMauro Carvalho Chehab
15824071ac1SMauro Carvalho Chehabsub check_perl_module($$)
15924071ac1SMauro Carvalho Chehab{
16024071ac1SMauro Carvalho Chehab	my $prog = shift;
16124071ac1SMauro Carvalho Chehab	my $is_optional = shift;
16224071ac1SMauro Carvalho Chehab
16324071ac1SMauro Carvalho Chehab	my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
16424071ac1SMauro Carvalho Chehab	return if ($err == 0);
16524071ac1SMauro Carvalho Chehab
16624071ac1SMauro Carvalho Chehab	add_package($prog, $is_optional);
16724071ac1SMauro Carvalho Chehab}
16824071ac1SMauro Carvalho Chehab
16924071ac1SMauro Carvalho Chehabsub check_python_module($$)
17024071ac1SMauro Carvalho Chehab{
17124071ac1SMauro Carvalho Chehab	my $prog = shift;
17224071ac1SMauro Carvalho Chehab	my $is_optional = shift;
17324071ac1SMauro Carvalho Chehab
1742f9c5025SMauro Carvalho Chehab	return if (!$python_cmd);
1752f9c5025SMauro Carvalho Chehab
1762f9c5025SMauro Carvalho Chehab	my $err = system("$python_cmd -c 'import $prog' 2>/dev/null /dev/null");
17724071ac1SMauro Carvalho Chehab	return if ($err == 0);
17824071ac1SMauro Carvalho Chehab
17924071ac1SMauro Carvalho Chehab	add_package($prog, $is_optional);
18024071ac1SMauro Carvalho Chehab}
18124071ac1SMauro Carvalho Chehab
18224071ac1SMauro Carvalho Chehabsub check_rpm_missing($$)
18324071ac1SMauro Carvalho Chehab{
18424071ac1SMauro Carvalho Chehab	my @pkgs = @{$_[0]};
18524071ac1SMauro Carvalho Chehab	my $is_optional = $_[1];
18624071ac1SMauro Carvalho Chehab
18724071ac1SMauro Carvalho Chehab	foreach my $prog(@pkgs) {
18824071ac1SMauro Carvalho Chehab		my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
18924071ac1SMauro Carvalho Chehab		add_package($prog, $is_optional) if ($err);
19024071ac1SMauro Carvalho Chehab	}
19124071ac1SMauro Carvalho Chehab}
19224071ac1SMauro Carvalho Chehab
19324071ac1SMauro Carvalho Chehabsub check_pacman_missing($$)
19424071ac1SMauro Carvalho Chehab{
19524071ac1SMauro Carvalho Chehab	my @pkgs = @{$_[0]};
19624071ac1SMauro Carvalho Chehab	my $is_optional = $_[1];
19724071ac1SMauro Carvalho Chehab
19824071ac1SMauro Carvalho Chehab	foreach my $prog(@pkgs) {
19924071ac1SMauro Carvalho Chehab		my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
20024071ac1SMauro Carvalho Chehab		add_package($prog, $is_optional) if ($err);
20124071ac1SMauro Carvalho Chehab	}
20224071ac1SMauro Carvalho Chehab}
20324071ac1SMauro Carvalho Chehab
20424071ac1SMauro Carvalho Chehabsub check_missing_tex($)
20524071ac1SMauro Carvalho Chehab{
20624071ac1SMauro Carvalho Chehab	my $is_optional = shift;
20724071ac1SMauro Carvalho Chehab	my $kpsewhich = findprog("kpsewhich");
20824071ac1SMauro Carvalho Chehab
20924071ac1SMauro Carvalho Chehab	foreach my $prog(keys %texlive) {
21024071ac1SMauro Carvalho Chehab		my $package = $texlive{$prog};
21124071ac1SMauro Carvalho Chehab		if (!$kpsewhich) {
21224071ac1SMauro Carvalho Chehab			add_package($package, $is_optional);
21324071ac1SMauro Carvalho Chehab			next;
21424071ac1SMauro Carvalho Chehab		}
21524071ac1SMauro Carvalho Chehab		my $file = qx($kpsewhich $prog);
21624071ac1SMauro Carvalho Chehab		add_package($package, $is_optional) if ($file =~ /^\s*$/);
21724071ac1SMauro Carvalho Chehab	}
21824071ac1SMauro Carvalho Chehab}
21924071ac1SMauro Carvalho Chehab
22077d09ad9SMauro Carvalho Chehabsub get_sphinx_fname()
22124071ac1SMauro Carvalho Chehab{
22277d09ad9SMauro Carvalho Chehab	my $fname = "sphinx-build";
22377d09ad9SMauro Carvalho Chehab	return $fname if findprog($fname);
22424071ac1SMauro Carvalho Chehab
22577d09ad9SMauro Carvalho Chehab	$fname = "sphinx-build-3";
22677d09ad9SMauro Carvalho Chehab	if (findprog($fname)) {
22724071ac1SMauro Carvalho Chehab		$need_symlink = 1;
22877d09ad9SMauro Carvalho Chehab		return $fname;
22924071ac1SMauro Carvalho Chehab	}
23024071ac1SMauro Carvalho Chehab
23177d09ad9SMauro Carvalho Chehab	return "";
23277d09ad9SMauro Carvalho Chehab}
23377d09ad9SMauro Carvalho Chehab
234*a8b380c3SMauro Carvalho Chehabsub get_sphinx_version($)
235*a8b380c3SMauro Carvalho Chehab{
236*a8b380c3SMauro Carvalho Chehab	my $cmd = shift;
237*a8b380c3SMauro Carvalho Chehab	my $ver;
238*a8b380c3SMauro Carvalho Chehab
239*a8b380c3SMauro Carvalho Chehab	open IN, "$cmd --version 2>&1 |";
240*a8b380c3SMauro Carvalho Chehab	while (<IN>) {
241*a8b380c3SMauro Carvalho Chehab		if (m/^\s*sphinx-build\s+([\d\.]+)(\+\/[\da-f]+)?$/) {
242*a8b380c3SMauro Carvalho Chehab			$ver=$1;
243*a8b380c3SMauro Carvalho Chehab			last;
244*a8b380c3SMauro Carvalho Chehab		}
245*a8b380c3SMauro Carvalho Chehab		# Sphinx 1.2.x uses a different format
246*a8b380c3SMauro Carvalho Chehab		if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
247*a8b380c3SMauro Carvalho Chehab			$ver=$1;
248*a8b380c3SMauro Carvalho Chehab			last;
249*a8b380c3SMauro Carvalho Chehab		}
250*a8b380c3SMauro Carvalho Chehab	}
251*a8b380c3SMauro Carvalho Chehab	close IN;
252*a8b380c3SMauro Carvalho Chehab	return $ver;
253*a8b380c3SMauro Carvalho Chehab}
254*a8b380c3SMauro Carvalho Chehab
25577d09ad9SMauro Carvalho Chehabsub check_sphinx()
25677d09ad9SMauro Carvalho Chehab{
25777d09ad9SMauro Carvalho Chehab	my $rec_version;
25877d09ad9SMauro Carvalho Chehab	my $cur_version;
25977d09ad9SMauro Carvalho Chehab
26077d09ad9SMauro Carvalho Chehab	open IN, $conf or die "Can't open $conf";
26177d09ad9SMauro Carvalho Chehab	while (<IN>) {
26277d09ad9SMauro Carvalho Chehab		if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
26377d09ad9SMauro Carvalho Chehab			$min_version=$1;
26477d09ad9SMauro Carvalho Chehab			last;
26577d09ad9SMauro Carvalho Chehab		}
26677d09ad9SMauro Carvalho Chehab	}
26777d09ad9SMauro Carvalho Chehab	close IN;
26877d09ad9SMauro Carvalho Chehab
26977d09ad9SMauro Carvalho Chehab	die "Can't get needs_sphinx version from $conf" if (!$min_version);
27077d09ad9SMauro Carvalho Chehab
27177d09ad9SMauro Carvalho Chehab	open IN, $requirement_file or die "Can't open $requirement_file";
27277d09ad9SMauro Carvalho Chehab	while (<IN>) {
27377d09ad9SMauro Carvalho Chehab		if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
27477d09ad9SMauro Carvalho Chehab			$rec_version=$1;
27577d09ad9SMauro Carvalho Chehab			last;
27677d09ad9SMauro Carvalho Chehab		}
27777d09ad9SMauro Carvalho Chehab	}
27877d09ad9SMauro Carvalho Chehab	close IN;
27977d09ad9SMauro Carvalho Chehab
28077d09ad9SMauro Carvalho Chehab	die "Can't get recommended sphinx version from $requirement_file" if (!$min_version);
28177d09ad9SMauro Carvalho Chehab
28244f42165SMauro Carvalho Chehab	$virtenv_dir = $virtenv_prefix . $rec_version;
28377d09ad9SMauro Carvalho Chehab
28477d09ad9SMauro Carvalho Chehab	my $sphinx = get_sphinx_fname();
2852f9c5025SMauro Carvalho Chehab	if ($sphinx eq "") {
2862f9c5025SMauro Carvalho Chehab		$need_sphinx = 1;
2872f9c5025SMauro Carvalho Chehab		return;
2882f9c5025SMauro Carvalho Chehab	}
28977d09ad9SMauro Carvalho Chehab
290*a8b380c3SMauro Carvalho Chehab	$cur_version = get_sphinx_version($sphinx);
291*a8b380c3SMauro Carvalho Chehab	die ("$sphinx returned an error") if (!$cur_version);
29277d09ad9SMauro Carvalho Chehab
29377d09ad9SMauro Carvalho Chehab	die "$sphinx didn't return its version" if (!$cur_version);
29477d09ad9SMauro Carvalho Chehab
29577d09ad9SMauro Carvalho Chehab	if ($cur_version lt $min_version) {
2969b88ad54SMauro Carvalho Chehab		printf "ERROR: Sphinx version is %s. It should be >= %s (recommended >= %s)\n",
2979b88ad54SMauro Carvalho Chehab		       $cur_version, $min_version, $rec_version;;
29877d09ad9SMauro Carvalho Chehab		$need_sphinx = 1;
29977d09ad9SMauro Carvalho Chehab		return;
30077d09ad9SMauro Carvalho Chehab	}
30177d09ad9SMauro Carvalho Chehab
30277d09ad9SMauro Carvalho Chehab	if ($cur_version lt $rec_version) {
3039b88ad54SMauro Carvalho Chehab		printf "Sphinx version %s\n", $cur_version;
30477d09ad9SMauro Carvalho Chehab		print "Warning: It is recommended at least Sphinx version $rec_version.\n";
30577d09ad9SMauro Carvalho Chehab		$rec_sphinx_upgrade = 1;
3069b88ad54SMauro Carvalho Chehab		return;
30777d09ad9SMauro Carvalho Chehab	}
3089b88ad54SMauro Carvalho Chehab
3099b88ad54SMauro Carvalho Chehab	# On version check mode, just assume Sphinx has all mandatory deps
3109b88ad54SMauro Carvalho Chehab	exit (0) if ($version_check);
31124071ac1SMauro Carvalho Chehab}
31224071ac1SMauro Carvalho Chehab
31324071ac1SMauro Carvalho Chehab#
31424071ac1SMauro Carvalho Chehab# Ancillary subroutines
31524071ac1SMauro Carvalho Chehab#
31624071ac1SMauro Carvalho Chehab
31724071ac1SMauro Carvalho Chehabsub catcheck($)
31824071ac1SMauro Carvalho Chehab{
31924071ac1SMauro Carvalho Chehab  my $res = "";
32024071ac1SMauro Carvalho Chehab  $res = qx(cat $_[0]) if (-r $_[0]);
32124071ac1SMauro Carvalho Chehab  return $res;
32224071ac1SMauro Carvalho Chehab}
32324071ac1SMauro Carvalho Chehab
32424071ac1SMauro Carvalho Chehabsub which($)
32524071ac1SMauro Carvalho Chehab{
32624071ac1SMauro Carvalho Chehab	my $file = shift;
32724071ac1SMauro Carvalho Chehab	my @path = split ":", $ENV{PATH};
32824071ac1SMauro Carvalho Chehab
32924071ac1SMauro Carvalho Chehab	foreach my $dir(@path) {
33024071ac1SMauro Carvalho Chehab		my $name = $dir.'/'.$file;
33124071ac1SMauro Carvalho Chehab		return $name if (-x $name );
33224071ac1SMauro Carvalho Chehab	}
33324071ac1SMauro Carvalho Chehab	return undef;
33424071ac1SMauro Carvalho Chehab}
33524071ac1SMauro Carvalho Chehab
33624071ac1SMauro Carvalho Chehab#
33724071ac1SMauro Carvalho Chehab# Subroutines that check distro-specific hints
33824071ac1SMauro Carvalho Chehab#
33924071ac1SMauro Carvalho Chehab
34024071ac1SMauro Carvalho Chehabsub give_debian_hints()
34124071ac1SMauro Carvalho Chehab{
34224071ac1SMauro Carvalho Chehab	my %map = (
34324071ac1SMauro Carvalho Chehab		"python-sphinx"		=> "python3-sphinx",
34424071ac1SMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "python3-sphinx-rtd-theme",
3452f9c5025SMauro Carvalho Chehab		"ensurepip"		=> "python3-venv",
34624071ac1SMauro Carvalho Chehab		"virtualenv"		=> "virtualenv",
34724071ac1SMauro Carvalho Chehab		"dot"			=> "graphviz",
34824071ac1SMauro Carvalho Chehab		"convert"		=> "imagemagick",
34924071ac1SMauro Carvalho Chehab		"Pod::Usage"		=> "perl-modules",
35024071ac1SMauro Carvalho Chehab		"xelatex"		=> "texlive-xetex",
3518e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "librsvg2-bin",
35224071ac1SMauro Carvalho Chehab	);
35324071ac1SMauro Carvalho Chehab
35424071ac1SMauro Carvalho Chehab	if ($pdf) {
355ff8fdb36SJeremy MAURO		check_missing_file(["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"],
35656e5a633SMauro Carvalho Chehab				   "fonts-dejavu", 2);
35727eed923SMauro Carvalho Chehab
3589692f2fdSJeremy MAURO		check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
359bfc7f428SMauro Carvalho Chehab				    "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
3609692f2fdSJeremy MAURO				    "/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc"],
36127eed923SMauro Carvalho Chehab				   "fonts-noto-cjk", 2);
36224071ac1SMauro Carvalho Chehab	}
36324071ac1SMauro Carvalho Chehab
36456e5a633SMauro Carvalho Chehab	check_program("dvipng", 2) if ($pdf);
36524071ac1SMauro Carvalho Chehab	check_missing(\%map);
36624071ac1SMauro Carvalho Chehab
36724071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
36824071ac1SMauro Carvalho Chehab	printf("You should run:\n\n\tsudo apt-get install $install\n");
36924071ac1SMauro Carvalho Chehab}
37024071ac1SMauro Carvalho Chehab
37124071ac1SMauro Carvalho Chehabsub give_redhat_hints()
37224071ac1SMauro Carvalho Chehab{
37324071ac1SMauro Carvalho Chehab	my %map = (
37424071ac1SMauro Carvalho Chehab		"python-sphinx"		=> "python3-sphinx",
37524071ac1SMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "python3-sphinx_rtd_theme",
37624071ac1SMauro Carvalho Chehab		"virtualenv"		=> "python3-virtualenv",
37724071ac1SMauro Carvalho Chehab		"dot"			=> "graphviz",
37824071ac1SMauro Carvalho Chehab		"convert"		=> "ImageMagick",
37924071ac1SMauro Carvalho Chehab		"Pod::Usage"		=> "perl-Pod-Usage",
38024071ac1SMauro Carvalho Chehab		"xelatex"		=> "texlive-xetex-bin",
3818e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "librsvg2-tools",
38224071ac1SMauro Carvalho Chehab	);
38324071ac1SMauro Carvalho Chehab
3845d88953cSMauro Carvalho Chehab	my @fedora26_opt_pkgs = (
3855d88953cSMauro Carvalho Chehab		"graphviz-gd",		# Fedora 26: needed for PDF support
3865d88953cSMauro Carvalho Chehab	);
3875d88953cSMauro Carvalho Chehab
38824071ac1SMauro Carvalho Chehab	my @fedora_tex_pkgs = (
38924071ac1SMauro Carvalho Chehab		"texlive-collection-fontsrecommended",
39024071ac1SMauro Carvalho Chehab		"texlive-collection-latex",
39127eed923SMauro Carvalho Chehab		"texlive-xecjk",
39224071ac1SMauro Carvalho Chehab		"dejavu-sans-fonts",
39324071ac1SMauro Carvalho Chehab		"dejavu-serif-fonts",
39424071ac1SMauro Carvalho Chehab		"dejavu-sans-mono-fonts",
39524071ac1SMauro Carvalho Chehab	);
39624071ac1SMauro Carvalho Chehab
3979b756a9dSMauro Carvalho Chehab	#
3989b756a9dSMauro Carvalho Chehab	# Checks valid for RHEL/CentOS version 7.x.
3999b756a9dSMauro Carvalho Chehab	#
40056e5a633SMauro Carvalho Chehab	my $old = 0;
40156e5a633SMauro Carvalho Chehab	my $rel;
40256e5a633SMauro Carvalho Chehab	$rel = $1 if ($system_release =~ /release\s+(\d+)/);
40356e5a633SMauro Carvalho Chehab
404b308467cSMauro Carvalho Chehab	if (!($system_release =~ /Fedora/)) {
4059b756a9dSMauro Carvalho Chehab		$map{"virtualenv"} = "python-virtualenv";
40656e5a633SMauro Carvalho Chehab
40756e5a633SMauro Carvalho Chehab		if ($rel && $rel < 8) {
40856e5a633SMauro Carvalho Chehab			$old = 1;
40956e5a633SMauro Carvalho Chehab			$pdf = 0;
41056e5a633SMauro Carvalho Chehab
41156e5a633SMauro Carvalho Chehab			printf("Note: texlive packages on RHEL/CENTOS <= 7 are incomplete. Can't support PDF output\n");
41256e5a633SMauro Carvalho Chehab			printf("If you want to build PDF, please read:\n");
41356e5a633SMauro Carvalho Chehab			printf("\thttps://www.systutorials.com/241660/how-to-install-tex-live-on-centos-7-linux/\n");
41456e5a633SMauro Carvalho Chehab		}
41556e5a633SMauro Carvalho Chehab	} else {
41656e5a633SMauro Carvalho Chehab		if ($rel && $rel < 26) {
41756e5a633SMauro Carvalho Chehab			$old = 1;
41856e5a633SMauro Carvalho Chehab		}
41956e5a633SMauro Carvalho Chehab	}
42056e5a633SMauro Carvalho Chehab	if (!$rel) {
42156e5a633SMauro Carvalho Chehab		printf("Couldn't identify release number\n");
42256e5a633SMauro Carvalho Chehab		$old = 1;
42356e5a633SMauro Carvalho Chehab		$pdf = 0;
4249b756a9dSMauro Carvalho Chehab	}
4259b756a9dSMauro Carvalho Chehab
42627eed923SMauro Carvalho Chehab	if ($pdf) {
427ff8fdb36SJeremy MAURO		check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc"],
42827eed923SMauro Carvalho Chehab				   "google-noto-sans-cjk-ttc-fonts", 2);
42927eed923SMauro Carvalho Chehab	}
43027eed923SMauro Carvalho Chehab
43156e5a633SMauro Carvalho Chehab	check_rpm_missing(\@fedora26_opt_pkgs, 2) if ($pdf && !$old);
43256e5a633SMauro Carvalho Chehab	check_rpm_missing(\@fedora_tex_pkgs, 2) if ($pdf);
43356e5a633SMauro Carvalho Chehab	check_missing_tex(2) if ($pdf);
43424071ac1SMauro Carvalho Chehab	check_missing(\%map);
43524071ac1SMauro Carvalho Chehab
43624071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
4379b756a9dSMauro Carvalho Chehab
43856e5a633SMauro Carvalho Chehab	if (!$old) {
4399b756a9dSMauro Carvalho Chehab		# dnf, for Fedora 18+
44024071ac1SMauro Carvalho Chehab		printf("You should run:\n\n\tsudo dnf install -y $install\n");
4419b756a9dSMauro Carvalho Chehab	} else {
4429b756a9dSMauro Carvalho Chehab		# yum, for RHEL (and clones) or Fedora version < 18
4439b756a9dSMauro Carvalho Chehab		printf("You should run:\n\n\tsudo yum install -y $install\n");
4449b756a9dSMauro Carvalho Chehab	}
44524071ac1SMauro Carvalho Chehab}
44624071ac1SMauro Carvalho Chehab
44724071ac1SMauro Carvalho Chehabsub give_opensuse_hints()
44824071ac1SMauro Carvalho Chehab{
44924071ac1SMauro Carvalho Chehab	my %map = (
45024071ac1SMauro Carvalho Chehab		"python-sphinx"		=> "python3-sphinx",
45124071ac1SMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "python3-sphinx_rtd_theme",
45224071ac1SMauro Carvalho Chehab		"virtualenv"		=> "python3-virtualenv",
45324071ac1SMauro Carvalho Chehab		"dot"			=> "graphviz",
45424071ac1SMauro Carvalho Chehab		"convert"		=> "ImageMagick",
45524071ac1SMauro Carvalho Chehab		"Pod::Usage"		=> "perl-Pod-Usage",
45624071ac1SMauro Carvalho Chehab		"xelatex"		=> "texlive-xetex-bin",
45724071ac1SMauro Carvalho Chehab	);
45824071ac1SMauro Carvalho Chehab
459b3df6223SMauro Carvalho Chehab	# On Tumbleweed, this package is also named rsvg-convert
460b3df6223SMauro Carvalho Chehab	$map{"rsvg-convert"} = "rsvg-view" if (!($system_release =~ /Tumbleweed/));
461b3df6223SMauro Carvalho Chehab
46224071ac1SMauro Carvalho Chehab	my @suse_tex_pkgs = (
46324071ac1SMauro Carvalho Chehab		"texlive-babel-english",
46424071ac1SMauro Carvalho Chehab		"texlive-caption",
46524071ac1SMauro Carvalho Chehab		"texlive-colortbl",
46624071ac1SMauro Carvalho Chehab		"texlive-courier",
46724071ac1SMauro Carvalho Chehab		"texlive-dvips",
46824071ac1SMauro Carvalho Chehab		"texlive-helvetic",
46924071ac1SMauro Carvalho Chehab		"texlive-makeindex",
47024071ac1SMauro Carvalho Chehab		"texlive-metafont",
47124071ac1SMauro Carvalho Chehab		"texlive-metapost",
47224071ac1SMauro Carvalho Chehab		"texlive-palatino",
47324071ac1SMauro Carvalho Chehab		"texlive-preview",
47424071ac1SMauro Carvalho Chehab		"texlive-times",
47524071ac1SMauro Carvalho Chehab		"texlive-zapfchan",
47624071ac1SMauro Carvalho Chehab		"texlive-zapfding",
47724071ac1SMauro Carvalho Chehab	);
47824071ac1SMauro Carvalho Chehab
479353290a9SMauro Carvalho Chehab	$map{"latexmk"} = "texlive-latexmk-bin";
480353290a9SMauro Carvalho Chehab
48127eed923SMauro Carvalho Chehab	# FIXME: add support for installing CJK fonts
48227eed923SMauro Carvalho Chehab	#
48327eed923SMauro Carvalho Chehab	# I tried hard, but was unable to find a way to install
48427eed923SMauro Carvalho Chehab	# "Noto Sans CJK SC" on openSUSE
48527eed923SMauro Carvalho Chehab
48656e5a633SMauro Carvalho Chehab	check_rpm_missing(\@suse_tex_pkgs, 2) if ($pdf);
48756e5a633SMauro Carvalho Chehab	check_missing_tex(2) if ($pdf);
48824071ac1SMauro Carvalho Chehab	check_missing(\%map);
48924071ac1SMauro Carvalho Chehab
49024071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
49124071ac1SMauro Carvalho Chehab	printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
49224071ac1SMauro Carvalho Chehab}
49324071ac1SMauro Carvalho Chehab
494800d408aSMauro Carvalho Chehabsub give_mageia_hints()
495800d408aSMauro Carvalho Chehab{
496800d408aSMauro Carvalho Chehab	my %map = (
497800d408aSMauro Carvalho Chehab		"python-sphinx"		=> "python3-sphinx",
498800d408aSMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "python3-sphinx_rtd_theme",
499800d408aSMauro Carvalho Chehab		"virtualenv"		=> "python3-virtualenv",
500800d408aSMauro Carvalho Chehab		"dot"			=> "graphviz",
501800d408aSMauro Carvalho Chehab		"convert"		=> "ImageMagick",
502800d408aSMauro Carvalho Chehab		"Pod::Usage"		=> "perl-Pod-Usage",
503800d408aSMauro Carvalho Chehab		"xelatex"		=> "texlive",
504d6ebf189SMauro Carvalho Chehab		"rsvg-convert"		=> "librsvg2",
505800d408aSMauro Carvalho Chehab	);
506800d408aSMauro Carvalho Chehab
507800d408aSMauro Carvalho Chehab	my @tex_pkgs = (
508800d408aSMauro Carvalho Chehab		"texlive-fontsextra",
509800d408aSMauro Carvalho Chehab	);
510800d408aSMauro Carvalho Chehab
511353290a9SMauro Carvalho Chehab	$map{"latexmk"} = "texlive-collection-basic";
512353290a9SMauro Carvalho Chehab
513d6ebf189SMauro Carvalho Chehab	my $packager_cmd;
514d6ebf189SMauro Carvalho Chehab	my $noto_sans;
515d6ebf189SMauro Carvalho Chehab	if ($system_release =~ /OpenMandriva/) {
516d6ebf189SMauro Carvalho Chehab		$packager_cmd = "dnf install";
517d6ebf189SMauro Carvalho Chehab		$noto_sans = "noto-sans-cjk-fonts";
518d6ebf189SMauro Carvalho Chehab		@tex_pkgs = ( "texlive-collection-fontsextra" );
519d6ebf189SMauro Carvalho Chehab	} else {
520d6ebf189SMauro Carvalho Chehab		$packager_cmd = "urpmi";
521d6ebf189SMauro Carvalho Chehab		$noto_sans = "google-noto-sans-cjk-ttc-fonts";
522d6ebf189SMauro Carvalho Chehab	}
523d6ebf189SMauro Carvalho Chehab
524d6ebf189SMauro Carvalho Chehab
52527eed923SMauro Carvalho Chehab	if ($pdf) {
526d6ebf189SMauro Carvalho Chehab		check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc",
527d6ebf189SMauro Carvalho Chehab				    "/usr/share/fonts/TTF/NotoSans-Regular.ttf"],
528d6ebf189SMauro Carvalho Chehab				   $noto_sans, 2);
52927eed923SMauro Carvalho Chehab	}
53027eed923SMauro Carvalho Chehab
53156e5a633SMauro Carvalho Chehab	check_rpm_missing(\@tex_pkgs, 2) if ($pdf);
532800d408aSMauro Carvalho Chehab	check_missing(\%map);
533800d408aSMauro Carvalho Chehab
534800d408aSMauro Carvalho Chehab	return if (!$need && !$optional);
535d6ebf189SMauro Carvalho Chehab	printf("You should run:\n\n\tsudo $packager_cmd $install\n");
536800d408aSMauro Carvalho Chehab}
537800d408aSMauro Carvalho Chehab
53824071ac1SMauro Carvalho Chehabsub give_arch_linux_hints()
53924071ac1SMauro Carvalho Chehab{
54024071ac1SMauro Carvalho Chehab	my %map = (
54124071ac1SMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "python-sphinx_rtd_theme",
54224071ac1SMauro Carvalho Chehab		"virtualenv"		=> "python-virtualenv",
54324071ac1SMauro Carvalho Chehab		"dot"			=> "graphviz",
54424071ac1SMauro Carvalho Chehab		"convert"		=> "imagemagick",
54524071ac1SMauro Carvalho Chehab		"xelatex"		=> "texlive-bin",
5460d0da9aaSLouis Taylor		"latexmk"		=> "texlive-core",
5478e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "extra/librsvg",
54824071ac1SMauro Carvalho Chehab	);
54924071ac1SMauro Carvalho Chehab
55024071ac1SMauro Carvalho Chehab	my @archlinux_tex_pkgs = (
55124071ac1SMauro Carvalho Chehab		"texlive-core",
55224071ac1SMauro Carvalho Chehab		"texlive-latexextra",
55324071ac1SMauro Carvalho Chehab		"ttf-dejavu",
55424071ac1SMauro Carvalho Chehab	);
55556e5a633SMauro Carvalho Chehab	check_pacman_missing(\@archlinux_tex_pkgs, 2) if ($pdf);
55656e5a633SMauro Carvalho Chehab
55727eed923SMauro Carvalho Chehab	if ($pdf) {
558ff8fdb36SJeremy MAURO		check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc"],
55927eed923SMauro Carvalho Chehab				   "noto-fonts-cjk", 2);
56027eed923SMauro Carvalho Chehab	}
56127eed923SMauro Carvalho Chehab
56224071ac1SMauro Carvalho Chehab	check_missing(\%map);
56324071ac1SMauro Carvalho Chehab
56424071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
56524071ac1SMauro Carvalho Chehab	printf("You should run:\n\n\tsudo pacman -S $install\n");
56624071ac1SMauro Carvalho Chehab}
56724071ac1SMauro Carvalho Chehab
56824071ac1SMauro Carvalho Chehabsub give_gentoo_hints()
56924071ac1SMauro Carvalho Chehab{
57024071ac1SMauro Carvalho Chehab	my %map = (
57124071ac1SMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "dev-python/sphinx_rtd_theme",
57224071ac1SMauro Carvalho Chehab		"virtualenv"		=> "dev-python/virtualenv",
57324071ac1SMauro Carvalho Chehab		"dot"			=> "media-gfx/graphviz",
57424071ac1SMauro Carvalho Chehab		"convert"		=> "media-gfx/imagemagick",
57524071ac1SMauro Carvalho Chehab		"xelatex"		=> "dev-texlive/texlive-xetex media-fonts/dejavu",
5768e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "gnome-base/librsvg",
57724071ac1SMauro Carvalho Chehab	);
57824071ac1SMauro Carvalho Chehab
579ff8fdb36SJeremy MAURO	check_missing_file(["/usr/share/fonts/dejavu/DejaVuSans.ttf"],
58056e5a633SMauro Carvalho Chehab			   "media-fonts/dejavu", 2) if ($pdf);
58124071ac1SMauro Carvalho Chehab
58227eed923SMauro Carvalho Chehab	if ($pdf) {
583e45a6317SMauro Carvalho Chehab		check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJKsc-Regular.otf",
584e45a6317SMauro Carvalho Chehab				    "/usr/share/fonts/noto-cjk/NotoSerifCJK-Regular.ttc"],
58527eed923SMauro Carvalho Chehab				   "media-fonts/noto-cjk", 2);
58627eed923SMauro Carvalho Chehab	}
58727eed923SMauro Carvalho Chehab
58824071ac1SMauro Carvalho Chehab	check_missing(\%map);
58924071ac1SMauro Carvalho Chehab
59024071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
591bba1e4cbSMauro Carvalho Chehab
592bba1e4cbSMauro Carvalho Chehab	printf("You should run:\n\n");
5934ea96d57SMauro Carvalho Chehab
5944ea96d57SMauro Carvalho Chehab	my $imagemagick = "media-gfx/imagemagick svg png";
5954ea96d57SMauro Carvalho Chehab	my $cairo = "media-gfx/graphviz cairo pdf";
5964ea96d57SMauro Carvalho Chehab	my $portage_imagemagick = "/etc/portage/package.use/imagemagick";
5974ea96d57SMauro Carvalho Chehab	my $portage_cairo = "/etc/portage/package.use/graphviz";
5984ea96d57SMauro Carvalho Chehab
599e45a6317SMauro Carvalho Chehab	if (qx(grep imagemagick $portage_imagemagick 2>/dev/null) eq "") {
6004ea96d57SMauro Carvalho Chehab		printf("\tsudo su -c 'echo \"$imagemagick\" > $portage_imagemagick'\n")
6014ea96d57SMauro Carvalho Chehab	}
602e45a6317SMauro Carvalho Chehab	if (qx(grep graphviz $portage_cairo 2>/dev/null) eq  "") {
6034ea96d57SMauro Carvalho Chehab		printf("\tsudo su -c 'echo \"$cairo\" > $portage_cairo'\n");
6044ea96d57SMauro Carvalho Chehab	}
6054ea96d57SMauro Carvalho Chehab
606bba1e4cbSMauro Carvalho Chehab	printf("\tsudo emerge --ask $install\n");
607bba1e4cbSMauro Carvalho Chehab
60824071ac1SMauro Carvalho Chehab}
60924071ac1SMauro Carvalho Chehab
61024071ac1SMauro Carvalho Chehabsub check_distros()
61124071ac1SMauro Carvalho Chehab{
61224071ac1SMauro Carvalho Chehab	# Distro-specific hints
61324071ac1SMauro Carvalho Chehab	if ($system_release =~ /Red Hat Enterprise Linux/) {
61424071ac1SMauro Carvalho Chehab		give_redhat_hints;
61524071ac1SMauro Carvalho Chehab		return;
61624071ac1SMauro Carvalho Chehab	}
6179b756a9dSMauro Carvalho Chehab	if ($system_release =~ /CentOS/) {
6189b756a9dSMauro Carvalho Chehab		give_redhat_hints;
6199b756a9dSMauro Carvalho Chehab		return;
6209b756a9dSMauro Carvalho Chehab	}
6219b756a9dSMauro Carvalho Chehab	if ($system_release =~ /Scientific Linux/) {
6229b756a9dSMauro Carvalho Chehab		give_redhat_hints;
6239b756a9dSMauro Carvalho Chehab		return;
6249b756a9dSMauro Carvalho Chehab	}
6259b756a9dSMauro Carvalho Chehab	if ($system_release =~ /Oracle Linux Server/) {
6269b756a9dSMauro Carvalho Chehab		give_redhat_hints;
6279b756a9dSMauro Carvalho Chehab		return;
6289b756a9dSMauro Carvalho Chehab	}
62924071ac1SMauro Carvalho Chehab	if ($system_release =~ /Fedora/) {
63024071ac1SMauro Carvalho Chehab		give_redhat_hints;
63124071ac1SMauro Carvalho Chehab		return;
63224071ac1SMauro Carvalho Chehab	}
63324071ac1SMauro Carvalho Chehab	if ($system_release =~ /Ubuntu/) {
63424071ac1SMauro Carvalho Chehab		give_debian_hints;
63524071ac1SMauro Carvalho Chehab		return;
63624071ac1SMauro Carvalho Chehab	}
63724071ac1SMauro Carvalho Chehab	if ($system_release =~ /Debian/) {
63824071ac1SMauro Carvalho Chehab		give_debian_hints;
63924071ac1SMauro Carvalho Chehab		return;
64024071ac1SMauro Carvalho Chehab	}
64124071ac1SMauro Carvalho Chehab	if ($system_release =~ /openSUSE/) {
64224071ac1SMauro Carvalho Chehab		give_opensuse_hints;
64324071ac1SMauro Carvalho Chehab		return;
64424071ac1SMauro Carvalho Chehab	}
645800d408aSMauro Carvalho Chehab	if ($system_release =~ /Mageia/) {
646800d408aSMauro Carvalho Chehab		give_mageia_hints;
647800d408aSMauro Carvalho Chehab		return;
648800d408aSMauro Carvalho Chehab	}
649d6ebf189SMauro Carvalho Chehab	if ($system_release =~ /OpenMandriva/) {
650d6ebf189SMauro Carvalho Chehab		give_mageia_hints;
651d6ebf189SMauro Carvalho Chehab		return;
652d6ebf189SMauro Carvalho Chehab	}
65324071ac1SMauro Carvalho Chehab	if ($system_release =~ /Arch Linux/) {
65424071ac1SMauro Carvalho Chehab		give_arch_linux_hints;
65524071ac1SMauro Carvalho Chehab		return;
65624071ac1SMauro Carvalho Chehab	}
65724071ac1SMauro Carvalho Chehab	if ($system_release =~ /Gentoo/) {
65824071ac1SMauro Carvalho Chehab		give_gentoo_hints;
65924071ac1SMauro Carvalho Chehab		return;
66024071ac1SMauro Carvalho Chehab	}
66124071ac1SMauro Carvalho Chehab
66224071ac1SMauro Carvalho Chehab	#
66324071ac1SMauro Carvalho Chehab	# Fall-back to generic hint code for other distros
66424071ac1SMauro Carvalho Chehab	# That's far from ideal, specially for LaTeX dependencies.
66524071ac1SMauro Carvalho Chehab	#
66624071ac1SMauro Carvalho Chehab	my %map = (
66724071ac1SMauro Carvalho Chehab		"sphinx-build" => "sphinx"
66824071ac1SMauro Carvalho Chehab	);
66956e5a633SMauro Carvalho Chehab	check_missing_tex(2) if ($pdf);
67024071ac1SMauro Carvalho Chehab	check_missing(\%map);
67124071ac1SMauro Carvalho Chehab	print "I don't know distro $system_release.\n";
67224071ac1SMauro Carvalho Chehab	print "So, I can't provide you a hint with the install procedure.\n";
67324071ac1SMauro Carvalho Chehab	print "There are likely missing dependencies.\n";
67424071ac1SMauro Carvalho Chehab}
67524071ac1SMauro Carvalho Chehab
67624071ac1SMauro Carvalho Chehab#
67724071ac1SMauro Carvalho Chehab# Common dependencies
67824071ac1SMauro Carvalho Chehab#
67924071ac1SMauro Carvalho Chehab
6802730ce01SShuah Khansub deactivate_help()
6812730ce01SShuah Khan{
6822f9c5025SMauro Carvalho Chehab	printf "\nIf you want to exit the virtualenv, you can use:\n";
6832730ce01SShuah Khan	printf "\tdeactivate\n";
6842730ce01SShuah Khan}
6852730ce01SShuah Khan
68624071ac1SMauro Carvalho Chehabsub check_needs()
68724071ac1SMauro Carvalho Chehab{
6882f9c5025SMauro Carvalho Chehab	# Check if Sphinx is already accessible from current environment
68924071ac1SMauro Carvalho Chehab	check_sphinx();
6909b88ad54SMauro Carvalho Chehab
6919b88ad54SMauro Carvalho Chehab	if ($system_release) {
6929b88ad54SMauro Carvalho Chehab		print "Detected OS: $system_release.\n\n";
6939b88ad54SMauro Carvalho Chehab	} else {
6949b88ad54SMauro Carvalho Chehab		print "Unknown OS\n\n";
6959b88ad54SMauro Carvalho Chehab	}
6969b88ad54SMauro Carvalho Chehab
6979b88ad54SMauro Carvalho Chehab	print "To upgrade Sphinx, use:\n\n" if ($rec_sphinx_upgrade);
6989b88ad54SMauro Carvalho Chehab
6992f9c5025SMauro Carvalho Chehab	# Check python command line, trying first python3
7002f9c5025SMauro Carvalho Chehab	$python_cmd = findprog("python3");
7012f9c5025SMauro Carvalho Chehab	$python_cmd = check_program("python", 0) if (!$python_cmd);
7022f9c5025SMauro Carvalho Chehab
7032f9c5025SMauro Carvalho Chehab	# Check the type of virtual env, depending on Python version
7042f9c5025SMauro Carvalho Chehab	if ($python_cmd) {
7052f9c5025SMauro Carvalho Chehab		if ($virtualenv) {
7062f9c5025SMauro Carvalho Chehab			my $tmp = qx($python_cmd --version 2>&1);
7072f9c5025SMauro Carvalho Chehab			if ($tmp =~ m/(\d+\.)(\d+\.)/) {
7082f9c5025SMauro Carvalho Chehab				if ($1 >= 3 && $2 >= 3) {
7092f9c5025SMauro Carvalho Chehab					$need_venv = 1;		# python 3.3 or upper
7102f9c5025SMauro Carvalho Chehab				} else {
7112f9c5025SMauro Carvalho Chehab					$need_virtualenv = 1;
7122f9c5025SMauro Carvalho Chehab				}
7132f9c5025SMauro Carvalho Chehab				if ($1 < 3) {
7142f9c5025SMauro Carvalho Chehab					# Complain if it finds python2 (or worse)
7152f9c5025SMauro Carvalho Chehab					printf "Warning: python$1 support is deprecated. Use it with caution!\n";
7162f9c5025SMauro Carvalho Chehab				}
7172f9c5025SMauro Carvalho Chehab			} else {
7182f9c5025SMauro Carvalho Chehab				die "Warning: couldn't identify $python_cmd version!";
7192f9c5025SMauro Carvalho Chehab			}
7202f9c5025SMauro Carvalho Chehab		} else {
7212f9c5025SMauro Carvalho Chehab			add_package("python-sphinx", 0);
7222f9c5025SMauro Carvalho Chehab		}
7232f9c5025SMauro Carvalho Chehab	}
7242f9c5025SMauro Carvalho Chehab
7252f9c5025SMauro Carvalho Chehab	# Set virtualenv command line, if python < 3.3
7262f9c5025SMauro Carvalho Chehab	my $virtualenv_cmd;
7272f9c5025SMauro Carvalho Chehab	if ($need_virtualenv) {
7282f9c5025SMauro Carvalho Chehab		$virtualenv_cmd = findprog("virtualenv-3");
7292f9c5025SMauro Carvalho Chehab		$virtualenv_cmd = findprog("virtualenv-3.5") if (!$virtualenv_cmd);
7302f9c5025SMauro Carvalho Chehab		if (!$virtualenv_cmd) {
7312f9c5025SMauro Carvalho Chehab			check_program("virtualenv", 0);
7322f9c5025SMauro Carvalho Chehab			$virtualenv_cmd = "virtualenv";
7332f9c5025SMauro Carvalho Chehab		}
7342f9c5025SMauro Carvalho Chehab	}
7352f9c5025SMauro Carvalho Chehab
7369b88ad54SMauro Carvalho Chehab	# Check for needed programs/tools
73724071ac1SMauro Carvalho Chehab	check_perl_module("Pod::Usage", 0);
73824071ac1SMauro Carvalho Chehab	check_program("make", 0);
73924071ac1SMauro Carvalho Chehab	check_program("gcc", 0);
74024071ac1SMauro Carvalho Chehab	check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
74124071ac1SMauro Carvalho Chehab	check_program("dot", 1);
74224071ac1SMauro Carvalho Chehab	check_program("convert", 1);
74356e5a633SMauro Carvalho Chehab
74456e5a633SMauro Carvalho Chehab	# Extra PDF files - should use 2 for is_optional
74556e5a633SMauro Carvalho Chehab	check_program("xelatex", 2) if ($pdf);
74656e5a633SMauro Carvalho Chehab	check_program("rsvg-convert", 2) if ($pdf);
74756e5a633SMauro Carvalho Chehab	check_program("latexmk", 2) if ($pdf);
74824071ac1SMauro Carvalho Chehab
7492f9c5025SMauro Carvalho Chehab	if ($need_sphinx || $rec_sphinx_upgrade) {
7502f9c5025SMauro Carvalho Chehab		check_python_module("ensurepip", 0) if ($need_venv);
7512f9c5025SMauro Carvalho Chehab	}
7522f9c5025SMauro Carvalho Chehab
7532f9c5025SMauro Carvalho Chehab	# Do distro-specific checks and output distro-install commands
75424071ac1SMauro Carvalho Chehab	check_distros();
75524071ac1SMauro Carvalho Chehab
7562f9c5025SMauro Carvalho Chehab	if (!$python_cmd) {
7572f9c5025SMauro Carvalho Chehab		if ($need == 1) {
7582f9c5025SMauro Carvalho Chehab			die "Can't build as $need mandatory dependency is missing";
7592f9c5025SMauro Carvalho Chehab		} elsif ($need) {
7602f9c5025SMauro Carvalho Chehab			die "Can't build as $need mandatory dependencies are missing";
7612f9c5025SMauro Carvalho Chehab		}
7622f9c5025SMauro Carvalho Chehab	}
7632f9c5025SMauro Carvalho Chehab
7642f9c5025SMauro Carvalho Chehab	# Check if sphinx-build is called sphinx-build-3
76524071ac1SMauro Carvalho Chehab	if ($need_symlink) {
76624071ac1SMauro Carvalho Chehab		printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
76724071ac1SMauro Carvalho Chehab		       which("sphinx-build-3");
76824071ac1SMauro Carvalho Chehab	}
7692f9c5025SMauro Carvalho Chehab
7702f9c5025SMauro Carvalho Chehab	# NOTE: if the system has a too old Sphinx version installed,
7712f9c5025SMauro Carvalho Chehab	# it will recommend installing a newer version using virtualenv
7722f9c5025SMauro Carvalho Chehab
77377d09ad9SMauro Carvalho Chehab	if ($need_sphinx || $rec_sphinx_upgrade) {
77444f42165SMauro Carvalho Chehab		my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
77544f42165SMauro Carvalho Chehab		my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
77644f42165SMauro Carvalho Chehab
77744f42165SMauro Carvalho Chehab		@activates = sort {$b cmp $a} @activates;
778*a8b380c3SMauro Carvalho Chehab		my ($activate, $ver);
779*a8b380c3SMauro Carvalho Chehab		foreach my $f (@activates) {
780*a8b380c3SMauro Carvalho Chehab			$activate = $f;
781*a8b380c3SMauro Carvalho Chehab			next if ($activate lt $min_activate);
78244f42165SMauro Carvalho Chehab
783*a8b380c3SMauro Carvalho Chehab			my $sphinx_cmd = $activate;
784*a8b380c3SMauro Carvalho Chehab			$sphinx_cmd =~ s/activate/sphinx-build/;
785*a8b380c3SMauro Carvalho Chehab			next if (! -f $sphinx_cmd);
786*a8b380c3SMauro Carvalho Chehab
787*a8b380c3SMauro Carvalho Chehab			$ver = get_sphinx_version($sphinx_cmd);
788*a8b380c3SMauro Carvalho Chehab			last if ($ver ge $min_version);
789*a8b380c3SMauro Carvalho Chehab		}
790*a8b380c3SMauro Carvalho Chehab		if ($need_sphinx && ($activate ne "")) {
791*a8b380c3SMauro Carvalho Chehab			printf "\nNeed to activate Sphinx (version $ver) on virtualenv with:\n";
792*a8b380c3SMauro Carvalho Chehab			printf "\t. $activate\n";
7932730ce01SShuah Khan			deactivate_help();
7949b88ad54SMauro Carvalho Chehab			exit (1);
7955be33182SMauro Carvalho Chehab		} else {
79644f42165SMauro Carvalho Chehab			my $rec_activate = "$virtenv_dir/bin/activate";
79724071ac1SMauro Carvalho Chehab
7982f9c5025SMauro Carvalho Chehab			if ($need_venv) {
7992f9c5025SMauro Carvalho Chehab				printf "\t$python_cmd -m venv $virtenv_dir\n";
8002f9c5025SMauro Carvalho Chehab			} else {
8012f9c5025SMauro Carvalho Chehab				printf "\t$virtualenv_cmd $virtenv_dir\n";
802c428cd52STim Bird			}
80344f42165SMauro Carvalho Chehab			printf "\t. $rec_activate\n";
804fb947f3fSMauro Carvalho Chehab			printf "\tpip install -r $requirement_file\n";
8052730ce01SShuah Khan			deactivate_help();
80677d09ad9SMauro Carvalho Chehab
80777d09ad9SMauro Carvalho Chehab			$need++ if (!$rec_sphinx_upgrade);
80824071ac1SMauro Carvalho Chehab		}
8095be33182SMauro Carvalho Chehab	}
81024071ac1SMauro Carvalho Chehab	printf "\n";
81124071ac1SMauro Carvalho Chehab
81254002b56SBjorn Helgaas	print "All optional dependencies are met.\n" if (!$optional);
81324071ac1SMauro Carvalho Chehab
81424071ac1SMauro Carvalho Chehab	if ($need == 1) {
81524071ac1SMauro Carvalho Chehab		die "Can't build as $need mandatory dependency is missing";
81624071ac1SMauro Carvalho Chehab	} elsif ($need) {
81724071ac1SMauro Carvalho Chehab		die "Can't build as $need mandatory dependencies are missing";
81824071ac1SMauro Carvalho Chehab	}
81924071ac1SMauro Carvalho Chehab
82024071ac1SMauro Carvalho Chehab	print "Needed package dependencies are met.\n";
82124071ac1SMauro Carvalho Chehab}
82224071ac1SMauro Carvalho Chehab
82324071ac1SMauro Carvalho Chehab#
82424071ac1SMauro Carvalho Chehab# Main
82524071ac1SMauro Carvalho Chehab#
82624071ac1SMauro Carvalho Chehab
82724071ac1SMauro Carvalho Chehabwhile (@ARGV) {
82824071ac1SMauro Carvalho Chehab	my $arg = shift(@ARGV);
82924071ac1SMauro Carvalho Chehab
83024071ac1SMauro Carvalho Chehab	if ($arg eq "--no-virtualenv") {
83124071ac1SMauro Carvalho Chehab		$virtualenv = 0;
83224071ac1SMauro Carvalho Chehab	} elsif ($arg eq "--no-pdf"){
83324071ac1SMauro Carvalho Chehab		$pdf = 0;
8349b88ad54SMauro Carvalho Chehab	} elsif ($arg eq "--version-check"){
8359b88ad54SMauro Carvalho Chehab		$version_check = 1;
83624071ac1SMauro Carvalho Chehab	} else {
8379b88ad54SMauro Carvalho Chehab		print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf> <--version-check>\n\n";
8389b88ad54SMauro Carvalho Chehab		print "Where:\n";
8399b88ad54SMauro Carvalho Chehab		print "\t--no-virtualenv\t- Recommend installing Sphinx instead of using a virtualenv\n";
8409b88ad54SMauro Carvalho Chehab		print "\t--version-check\t- if version is compatible, don't check for missing dependencies\n";
8419b88ad54SMauro Carvalho Chehab		print "\t--no-pdf\t- don't check for dependencies required to build PDF docs\n\n";
84224071ac1SMauro Carvalho Chehab		exit -1;
84324071ac1SMauro Carvalho Chehab	}
84424071ac1SMauro Carvalho Chehab}
84524071ac1SMauro Carvalho Chehab
84624071ac1SMauro Carvalho Chehab#
84724071ac1SMauro Carvalho Chehab# Determine the system type. There's no standard unique way that would
84824071ac1SMauro Carvalho Chehab# work with all distros with a minimal package install. So, several
84924071ac1SMauro Carvalho Chehab# methods are used here.
85024071ac1SMauro Carvalho Chehab#
85124071ac1SMauro Carvalho Chehab# By default, it will use lsb_release function. If not available, it will
85224071ac1SMauro Carvalho Chehab# fail back to reading the known different places where the distro name
85324071ac1SMauro Carvalho Chehab# is stored
85424071ac1SMauro Carvalho Chehab#
85524071ac1SMauro Carvalho Chehab
85624071ac1SMauro Carvalho Chehab$system_release = qx(lsb_release -d) if which("lsb_release");
85724071ac1SMauro Carvalho Chehab$system_release =~ s/Description:\s*// if ($system_release);
85824071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/system-release") if !$system_release;
85924071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/redhat-release") if !$system_release;
86024071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/lsb-release") if !$system_release;
86124071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/gentoo-release") if !$system_release;
862d14d0c1aSMauro Carvalho Chehab
863d14d0c1aSMauro Carvalho Chehab# This seems more common than LSB these days
864d14d0c1aSMauro Carvalho Chehabif (!$system_release) {
865d14d0c1aSMauro Carvalho Chehab	my %os_var;
866d14d0c1aSMauro Carvalho Chehab	if (open IN, "cat /etc/os-release|") {
867d14d0c1aSMauro Carvalho Chehab		while (<IN>) {
868d14d0c1aSMauro Carvalho Chehab			if (m/^([\w\d\_]+)=\"?([^\"]*)\"?\n/) {
869d14d0c1aSMauro Carvalho Chehab				$os_var{$1}=$2;
870d14d0c1aSMauro Carvalho Chehab			}
871d14d0c1aSMauro Carvalho Chehab		}
872d14d0c1aSMauro Carvalho Chehab		$system_release = $os_var{"NAME"};
873d14d0c1aSMauro Carvalho Chehab		if (defined($os_var{"VERSION_ID"})) {
874d14d0c1aSMauro Carvalho Chehab			$system_release .= " " . $os_var{"VERSION_ID"} if (defined($os_var{"VERSION_ID"}));
875d14d0c1aSMauro Carvalho Chehab		} else {
876d14d0c1aSMauro Carvalho Chehab			$system_release .= " " . $os_var{"VERSION"};
877d14d0c1aSMauro Carvalho Chehab		}
878d14d0c1aSMauro Carvalho Chehab	}
879d14d0c1aSMauro Carvalho Chehab}
88024071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/issue") if !$system_release;
88124071ac1SMauro Carvalho Chehab$system_release =~ s/\s+$//;
88224071ac1SMauro Carvalho Chehab
88324071ac1SMauro Carvalho Chehabcheck_needs;
884