xref: /openbmc/linux/scripts/sphinx-pre-install (revision c942fddf8793b2013be8c901b47d0a8dc02bf99f)
124071ac1SMauro Carvalho Chehab#!/usr/bin/perl
2*c942fddfSThomas Gleixner# SPDX-License-Identifier: GPL-2.0-or-later
324071ac1SMauro Carvalho Chehabuse strict;
424071ac1SMauro Carvalho Chehab
524071ac1SMauro Carvalho Chehab# Copyright (c) 2017 Mauro Carvalho Chehab <mchehab@kernel.org>
624071ac1SMauro Carvalho Chehab#
724071ac1SMauro Carvalho Chehab
85be33182SMauro Carvalho Chehabmy $virtenv_dir = "sphinx_1.4";
9fb947f3fSMauro Carvalho Chehabmy $requirement_file = "Documentation/sphinx/requirements.txt";
105be33182SMauro Carvalho Chehab
1124071ac1SMauro Carvalho Chehab#
1224071ac1SMauro Carvalho Chehab# Static vars
1324071ac1SMauro Carvalho Chehab#
1424071ac1SMauro Carvalho Chehab
1524071ac1SMauro Carvalho Chehabmy %missing;
1624071ac1SMauro Carvalho Chehabmy $system_release;
1724071ac1SMauro Carvalho Chehabmy $need = 0;
1824071ac1SMauro Carvalho Chehabmy $optional = 0;
1924071ac1SMauro Carvalho Chehabmy $need_symlink = 0;
2024071ac1SMauro Carvalho Chehabmy $need_sphinx = 0;
2124071ac1SMauro Carvalho Chehabmy $install = "";
2224071ac1SMauro Carvalho Chehab
2324071ac1SMauro Carvalho Chehab#
2424071ac1SMauro Carvalho Chehab# Command line arguments
2524071ac1SMauro Carvalho Chehab#
2624071ac1SMauro Carvalho Chehab
2724071ac1SMauro Carvalho Chehabmy $pdf = 1;
2824071ac1SMauro Carvalho Chehabmy $virtualenv = 1;
2924071ac1SMauro Carvalho Chehab
3024071ac1SMauro Carvalho Chehab#
3124071ac1SMauro Carvalho Chehab# List of required texlive packages on Fedora and OpenSuse
3224071ac1SMauro Carvalho Chehab#
3324071ac1SMauro Carvalho Chehab
3424071ac1SMauro Carvalho Chehabmy %texlive = (
3524071ac1SMauro Carvalho Chehab	'amsfonts.sty'       => 'texlive-amsfonts',
3624071ac1SMauro Carvalho Chehab	'amsmath.sty'        => 'texlive-amsmath',
3724071ac1SMauro Carvalho Chehab	'amssymb.sty'        => 'texlive-amsfonts',
3824071ac1SMauro Carvalho Chehab	'amsthm.sty'         => 'texlive-amscls',
3924071ac1SMauro Carvalho Chehab	'anyfontsize.sty'    => 'texlive-anyfontsize',
4024071ac1SMauro Carvalho Chehab	'atbegshi.sty'       => 'texlive-oberdiek',
4124071ac1SMauro Carvalho Chehab	'bm.sty'             => 'texlive-tools',
4224071ac1SMauro Carvalho Chehab	'capt-of.sty'        => 'texlive-capt-of',
4324071ac1SMauro Carvalho Chehab	'cmap.sty'           => 'texlive-cmap',
4424071ac1SMauro Carvalho Chehab	'ecrm1000.tfm'       => 'texlive-ec',
4524071ac1SMauro Carvalho Chehab	'eqparbox.sty'       => 'texlive-eqparbox',
4624071ac1SMauro Carvalho Chehab	'eu1enc.def'         => 'texlive-euenc',
4724071ac1SMauro Carvalho Chehab	'fancybox.sty'       => 'texlive-fancybox',
4824071ac1SMauro Carvalho Chehab	'fancyvrb.sty'       => 'texlive-fancyvrb',
4924071ac1SMauro Carvalho Chehab	'float.sty'          => 'texlive-float',
5024071ac1SMauro Carvalho Chehab	'fncychap.sty'       => 'texlive-fncychap',
5124071ac1SMauro Carvalho Chehab	'footnote.sty'       => 'texlive-mdwtools',
5224071ac1SMauro Carvalho Chehab	'framed.sty'         => 'texlive-framed',
5324071ac1SMauro Carvalho Chehab	'luatex85.sty'       => 'texlive-luatex85',
5424071ac1SMauro Carvalho Chehab	'multirow.sty'       => 'texlive-multirow',
5524071ac1SMauro Carvalho Chehab	'needspace.sty'      => 'texlive-needspace',
5624071ac1SMauro Carvalho Chehab	'palatino.sty'       => 'texlive-psnfss',
5724071ac1SMauro Carvalho Chehab	'parskip.sty'        => 'texlive-parskip',
5824071ac1SMauro Carvalho Chehab	'polyglossia.sty'    => 'texlive-polyglossia',
5924071ac1SMauro Carvalho Chehab	'tabulary.sty'       => 'texlive-tabulary',
6024071ac1SMauro Carvalho Chehab	'threeparttable.sty' => 'texlive-threeparttable',
6124071ac1SMauro Carvalho Chehab	'titlesec.sty'       => 'texlive-titlesec',
6224071ac1SMauro Carvalho Chehab	'ucs.sty'            => 'texlive-ucs',
6324071ac1SMauro Carvalho Chehab	'upquote.sty'        => 'texlive-upquote',
6424071ac1SMauro Carvalho Chehab	'wrapfig.sty'        => 'texlive-wrapfig',
6524071ac1SMauro Carvalho Chehab);
6624071ac1SMauro Carvalho Chehab
6724071ac1SMauro Carvalho Chehab#
6824071ac1SMauro Carvalho Chehab# Subroutines that checks if a feature exists
6924071ac1SMauro Carvalho Chehab#
7024071ac1SMauro Carvalho Chehab
7124071ac1SMauro Carvalho Chehabsub check_missing(%)
7224071ac1SMauro Carvalho Chehab{
7324071ac1SMauro Carvalho Chehab	my %map = %{$_[0]};
7424071ac1SMauro Carvalho Chehab
7524071ac1SMauro Carvalho Chehab	foreach my $prog (sort keys %missing) {
7624071ac1SMauro Carvalho Chehab		my $is_optional = $missing{$prog};
7724071ac1SMauro Carvalho Chehab
7824071ac1SMauro Carvalho Chehab		if ($is_optional) {
7924071ac1SMauro Carvalho Chehab			print "Warning: better to also install \"$prog\".\n";
8024071ac1SMauro Carvalho Chehab		} else {
8124071ac1SMauro Carvalho Chehab			print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
8224071ac1SMauro Carvalho Chehab		}
8324071ac1SMauro Carvalho Chehab		if (defined($map{$prog})) {
8424071ac1SMauro Carvalho Chehab			$install .= " " . $map{$prog};
8524071ac1SMauro Carvalho Chehab		} else {
8624071ac1SMauro Carvalho Chehab			$install .= " " . $prog;
8724071ac1SMauro Carvalho Chehab		}
8824071ac1SMauro Carvalho Chehab	}
8924071ac1SMauro Carvalho Chehab
9024071ac1SMauro Carvalho Chehab	$install =~ s/^\s//;
9124071ac1SMauro Carvalho Chehab}
9224071ac1SMauro Carvalho Chehab
9324071ac1SMauro Carvalho Chehabsub add_package($$)
9424071ac1SMauro Carvalho Chehab{
9524071ac1SMauro Carvalho Chehab	my $package = shift;
9624071ac1SMauro Carvalho Chehab	my $is_optional = shift;
9724071ac1SMauro Carvalho Chehab
9824071ac1SMauro Carvalho Chehab	$missing{$package} = $is_optional;
9924071ac1SMauro Carvalho Chehab	if ($is_optional) {
10024071ac1SMauro Carvalho Chehab		$optional++;
10124071ac1SMauro Carvalho Chehab	} else {
10224071ac1SMauro Carvalho Chehab		$need++;
10324071ac1SMauro Carvalho Chehab	}
10424071ac1SMauro Carvalho Chehab}
10524071ac1SMauro Carvalho Chehab
10624071ac1SMauro Carvalho Chehabsub check_missing_file($$$)
10724071ac1SMauro Carvalho Chehab{
10824071ac1SMauro Carvalho Chehab	my $file = shift;
10924071ac1SMauro Carvalho Chehab	my $package = shift;
11024071ac1SMauro Carvalho Chehab	my $is_optional = shift;
11124071ac1SMauro Carvalho Chehab
11224071ac1SMauro Carvalho Chehab	return if(-e $file);
11324071ac1SMauro Carvalho Chehab
11424071ac1SMauro Carvalho Chehab	add_package($package, $is_optional);
11524071ac1SMauro Carvalho Chehab}
11624071ac1SMauro Carvalho Chehab
11724071ac1SMauro Carvalho Chehabsub findprog($)
11824071ac1SMauro Carvalho Chehab{
11924071ac1SMauro Carvalho Chehab	foreach(split(/:/, $ENV{PATH})) {
12024071ac1SMauro Carvalho Chehab		return "$_/$_[0]" if(-x "$_/$_[0]");
12124071ac1SMauro Carvalho Chehab	}
12224071ac1SMauro Carvalho Chehab}
12324071ac1SMauro Carvalho Chehab
12424071ac1SMauro Carvalho Chehabsub check_program($$)
12524071ac1SMauro Carvalho Chehab{
12624071ac1SMauro Carvalho Chehab	my $prog = shift;
12724071ac1SMauro Carvalho Chehab	my $is_optional = shift;
12824071ac1SMauro Carvalho Chehab
12924071ac1SMauro Carvalho Chehab	return if findprog($prog);
13024071ac1SMauro Carvalho Chehab
13124071ac1SMauro Carvalho Chehab	add_package($prog, $is_optional);
13224071ac1SMauro Carvalho Chehab}
13324071ac1SMauro Carvalho Chehab
13424071ac1SMauro Carvalho Chehabsub check_perl_module($$)
13524071ac1SMauro Carvalho Chehab{
13624071ac1SMauro Carvalho Chehab	my $prog = shift;
13724071ac1SMauro Carvalho Chehab	my $is_optional = shift;
13824071ac1SMauro Carvalho Chehab
13924071ac1SMauro Carvalho Chehab	my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
14024071ac1SMauro Carvalho Chehab	return if ($err == 0);
14124071ac1SMauro Carvalho Chehab
14224071ac1SMauro Carvalho Chehab	add_package($prog, $is_optional);
14324071ac1SMauro Carvalho Chehab}
14424071ac1SMauro Carvalho Chehab
14524071ac1SMauro Carvalho Chehabsub check_python_module($$)
14624071ac1SMauro Carvalho Chehab{
14724071ac1SMauro Carvalho Chehab	my $prog = shift;
14824071ac1SMauro Carvalho Chehab	my $is_optional = shift;
14924071ac1SMauro Carvalho Chehab
15024071ac1SMauro Carvalho Chehab	my $err = system("python3 -c 'import $prog' 2>/dev/null /dev/null");
15124071ac1SMauro Carvalho Chehab	return if ($err == 0);
15224071ac1SMauro Carvalho Chehab	my $err = system("python -c 'import $prog' 2>/dev/null /dev/null");
15324071ac1SMauro Carvalho Chehab	return if ($err == 0);
15424071ac1SMauro Carvalho Chehab
15524071ac1SMauro Carvalho Chehab	add_package($prog, $is_optional);
15624071ac1SMauro Carvalho Chehab}
15724071ac1SMauro Carvalho Chehab
15824071ac1SMauro Carvalho Chehabsub check_rpm_missing($$)
15924071ac1SMauro Carvalho Chehab{
16024071ac1SMauro Carvalho Chehab	my @pkgs = @{$_[0]};
16124071ac1SMauro Carvalho Chehab	my $is_optional = $_[1];
16224071ac1SMauro Carvalho Chehab
16324071ac1SMauro Carvalho Chehab	foreach my $prog(@pkgs) {
16424071ac1SMauro Carvalho Chehab		my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
16524071ac1SMauro Carvalho Chehab		add_package($prog, $is_optional) if ($err);
16624071ac1SMauro Carvalho Chehab	}
16724071ac1SMauro Carvalho Chehab}
16824071ac1SMauro Carvalho Chehab
16924071ac1SMauro Carvalho Chehabsub check_pacman_missing($$)
17024071ac1SMauro Carvalho Chehab{
17124071ac1SMauro Carvalho Chehab	my @pkgs = @{$_[0]};
17224071ac1SMauro Carvalho Chehab	my $is_optional = $_[1];
17324071ac1SMauro Carvalho Chehab
17424071ac1SMauro Carvalho Chehab	foreach my $prog(@pkgs) {
17524071ac1SMauro Carvalho Chehab		my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
17624071ac1SMauro Carvalho Chehab		add_package($prog, $is_optional) if ($err);
17724071ac1SMauro Carvalho Chehab	}
17824071ac1SMauro Carvalho Chehab}
17924071ac1SMauro Carvalho Chehab
18024071ac1SMauro Carvalho Chehabsub check_missing_tex($)
18124071ac1SMauro Carvalho Chehab{
18224071ac1SMauro Carvalho Chehab	my $is_optional = shift;
18324071ac1SMauro Carvalho Chehab	my $kpsewhich = findprog("kpsewhich");
18424071ac1SMauro Carvalho Chehab
18524071ac1SMauro Carvalho Chehab	foreach my $prog(keys %texlive) {
18624071ac1SMauro Carvalho Chehab		my $package = $texlive{$prog};
18724071ac1SMauro Carvalho Chehab		if (!$kpsewhich) {
18824071ac1SMauro Carvalho Chehab			add_package($package, $is_optional);
18924071ac1SMauro Carvalho Chehab			next;
19024071ac1SMauro Carvalho Chehab		}
19124071ac1SMauro Carvalho Chehab		my $file = qx($kpsewhich $prog);
19224071ac1SMauro Carvalho Chehab		add_package($package, $is_optional) if ($file =~ /^\s*$/);
19324071ac1SMauro Carvalho Chehab	}
19424071ac1SMauro Carvalho Chehab}
19524071ac1SMauro Carvalho Chehab
19624071ac1SMauro Carvalho Chehabsub check_sphinx()
19724071ac1SMauro Carvalho Chehab{
19824071ac1SMauro Carvalho Chehab	return if findprog("sphinx-build");
19924071ac1SMauro Carvalho Chehab
20024071ac1SMauro Carvalho Chehab	if (findprog("sphinx-build-3")) {
20124071ac1SMauro Carvalho Chehab		$need_symlink = 1;
20224071ac1SMauro Carvalho Chehab		return;
20324071ac1SMauro Carvalho Chehab	}
20424071ac1SMauro Carvalho Chehab
20524071ac1SMauro Carvalho Chehab	if ($virtualenv) {
206800d408aSMauro Carvalho Chehab		my $prog = findprog("virtualenv-3");
207800d408aSMauro Carvalho Chehab		$prog = findprog("virtualenv-3.5") if (!$prog);
208800d408aSMauro Carvalho Chehab
209800d408aSMauro Carvalho Chehab		check_program("virtualenv", 0) if (!$prog);
21024071ac1SMauro Carvalho Chehab		$need_sphinx = 1;
21124071ac1SMauro Carvalho Chehab	} else {
21224071ac1SMauro Carvalho Chehab		add_package("python-sphinx", 0);
21324071ac1SMauro Carvalho Chehab	}
21424071ac1SMauro Carvalho Chehab}
21524071ac1SMauro Carvalho Chehab
21624071ac1SMauro Carvalho Chehab#
21724071ac1SMauro Carvalho Chehab# Ancillary subroutines
21824071ac1SMauro Carvalho Chehab#
21924071ac1SMauro Carvalho Chehab
22024071ac1SMauro Carvalho Chehabsub catcheck($)
22124071ac1SMauro Carvalho Chehab{
22224071ac1SMauro Carvalho Chehab  my $res = "";
22324071ac1SMauro Carvalho Chehab  $res = qx(cat $_[0]) if (-r $_[0]);
22424071ac1SMauro Carvalho Chehab  return $res;
22524071ac1SMauro Carvalho Chehab}
22624071ac1SMauro Carvalho Chehab
22724071ac1SMauro Carvalho Chehabsub which($)
22824071ac1SMauro Carvalho Chehab{
22924071ac1SMauro Carvalho Chehab	my $file = shift;
23024071ac1SMauro Carvalho Chehab	my @path = split ":", $ENV{PATH};
23124071ac1SMauro Carvalho Chehab
23224071ac1SMauro Carvalho Chehab	foreach my $dir(@path) {
23324071ac1SMauro Carvalho Chehab		my $name = $dir.'/'.$file;
23424071ac1SMauro Carvalho Chehab		return $name if (-x $name );
23524071ac1SMauro Carvalho Chehab	}
23624071ac1SMauro Carvalho Chehab	return undef;
23724071ac1SMauro Carvalho Chehab}
23824071ac1SMauro Carvalho Chehab
23924071ac1SMauro Carvalho Chehab#
24024071ac1SMauro Carvalho Chehab# Subroutines that check distro-specific hints
24124071ac1SMauro Carvalho Chehab#
24224071ac1SMauro Carvalho Chehab
24324071ac1SMauro Carvalho Chehabsub give_debian_hints()
24424071ac1SMauro Carvalho Chehab{
24524071ac1SMauro Carvalho Chehab	my %map = (
24624071ac1SMauro Carvalho Chehab		"python-sphinx"		=> "python3-sphinx",
24724071ac1SMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "python3-sphinx-rtd-theme",
24824071ac1SMauro Carvalho Chehab		"virtualenv"		=> "virtualenv",
24924071ac1SMauro Carvalho Chehab		"dot"			=> "graphviz",
25024071ac1SMauro Carvalho Chehab		"convert"		=> "imagemagick",
25124071ac1SMauro Carvalho Chehab		"Pod::Usage"		=> "perl-modules",
25224071ac1SMauro Carvalho Chehab		"xelatex"		=> "texlive-xetex",
2538e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "librsvg2-bin",
25424071ac1SMauro Carvalho Chehab	);
25524071ac1SMauro Carvalho Chehab
25624071ac1SMauro Carvalho Chehab	if ($pdf) {
25724071ac1SMauro Carvalho Chehab		check_missing_file("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
25824071ac1SMauro Carvalho Chehab				   "fonts-dejavu", 1);
25924071ac1SMauro Carvalho Chehab	}
26024071ac1SMauro Carvalho Chehab
26124071ac1SMauro Carvalho Chehab	check_program("dvipng", 1) if ($pdf);
26224071ac1SMauro Carvalho Chehab	check_missing(\%map);
26324071ac1SMauro Carvalho Chehab
26424071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
26524071ac1SMauro Carvalho Chehab	printf("You should run:\n\n\tsudo apt-get install $install\n");
26624071ac1SMauro Carvalho Chehab}
26724071ac1SMauro Carvalho Chehab
26824071ac1SMauro Carvalho Chehabsub give_redhat_hints()
26924071ac1SMauro Carvalho Chehab{
27024071ac1SMauro Carvalho Chehab	my %map = (
27124071ac1SMauro Carvalho Chehab		"python-sphinx"		=> "python3-sphinx",
27224071ac1SMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "python3-sphinx_rtd_theme",
27324071ac1SMauro Carvalho Chehab		"virtualenv"		=> "python3-virtualenv",
27424071ac1SMauro Carvalho Chehab		"dot"			=> "graphviz",
27524071ac1SMauro Carvalho Chehab		"convert"		=> "ImageMagick",
27624071ac1SMauro Carvalho Chehab		"Pod::Usage"		=> "perl-Pod-Usage",
27724071ac1SMauro Carvalho Chehab		"xelatex"		=> "texlive-xetex-bin",
2788e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "librsvg2-tools",
27924071ac1SMauro Carvalho Chehab	);
28024071ac1SMauro Carvalho Chehab
2815d88953cSMauro Carvalho Chehab	my @fedora26_opt_pkgs = (
2825d88953cSMauro Carvalho Chehab		"graphviz-gd",		# Fedora 26: needed for PDF support
2835d88953cSMauro Carvalho Chehab	);
2845d88953cSMauro Carvalho Chehab
28524071ac1SMauro Carvalho Chehab	my @fedora_tex_pkgs = (
28624071ac1SMauro Carvalho Chehab		"texlive-collection-fontsrecommended",
28724071ac1SMauro Carvalho Chehab		"texlive-collection-latex",
28824071ac1SMauro Carvalho Chehab		"dejavu-sans-fonts",
28924071ac1SMauro Carvalho Chehab		"dejavu-serif-fonts",
29024071ac1SMauro Carvalho Chehab		"dejavu-sans-mono-fonts",
29124071ac1SMauro Carvalho Chehab	);
29224071ac1SMauro Carvalho Chehab
2939b756a9dSMauro Carvalho Chehab	#
2949b756a9dSMauro Carvalho Chehab	# Checks valid for RHEL/CentOS version 7.x.
2959b756a9dSMauro Carvalho Chehab	#
2969b756a9dSMauro Carvalho Chehab	if (! $system_release =~ /Fedora/) {
2979b756a9dSMauro Carvalho Chehab		$map{"virtualenv"} = "python-virtualenv";
2989b756a9dSMauro Carvalho Chehab	}
2999b756a9dSMauro Carvalho Chehab
3005d88953cSMauro Carvalho Chehab	my $release;
3015d88953cSMauro Carvalho Chehab
3025d88953cSMauro Carvalho Chehab	$release = $1 if ($system_release =~ /Fedora\s+release\s+(\d+)/);
3035d88953cSMauro Carvalho Chehab
3045d88953cSMauro Carvalho Chehab	check_rpm_missing(\@fedora26_opt_pkgs, 1) if ($pdf && $release >= 26);
30524071ac1SMauro Carvalho Chehab	check_rpm_missing(\@fedora_tex_pkgs, 1) if ($pdf);
30624071ac1SMauro Carvalho Chehab	check_missing_tex(1) if ($pdf);
30724071ac1SMauro Carvalho Chehab	check_missing(\%map);
30824071ac1SMauro Carvalho Chehab
30924071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
3109b756a9dSMauro Carvalho Chehab
3119b756a9dSMauro Carvalho Chehab	if ($release >= 18) {
3129b756a9dSMauro Carvalho Chehab		# dnf, for Fedora 18+
31324071ac1SMauro Carvalho Chehab		printf("You should run:\n\n\tsudo dnf install -y $install\n");
3149b756a9dSMauro Carvalho Chehab	} else {
3159b756a9dSMauro Carvalho Chehab		# yum, for RHEL (and clones) or Fedora version < 18
3169b756a9dSMauro Carvalho Chehab		printf("You should run:\n\n\tsudo yum install -y $install\n");
3179b756a9dSMauro Carvalho Chehab	}
31824071ac1SMauro Carvalho Chehab}
31924071ac1SMauro Carvalho Chehab
32024071ac1SMauro Carvalho Chehabsub give_opensuse_hints()
32124071ac1SMauro Carvalho Chehab{
32224071ac1SMauro Carvalho Chehab	my %map = (
32324071ac1SMauro Carvalho Chehab		"python-sphinx"		=> "python3-sphinx",
32424071ac1SMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "python3-sphinx_rtd_theme",
32524071ac1SMauro Carvalho Chehab		"virtualenv"		=> "python3-virtualenv",
32624071ac1SMauro Carvalho Chehab		"dot"			=> "graphviz",
32724071ac1SMauro Carvalho Chehab		"convert"		=> "ImageMagick",
32824071ac1SMauro Carvalho Chehab		"Pod::Usage"		=> "perl-Pod-Usage",
32924071ac1SMauro Carvalho Chehab		"xelatex"		=> "texlive-xetex-bin",
3308e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "rsvg-view",
33124071ac1SMauro Carvalho Chehab	);
33224071ac1SMauro Carvalho Chehab
33324071ac1SMauro Carvalho Chehab	my @suse_tex_pkgs = (
33424071ac1SMauro Carvalho Chehab		"texlive-babel-english",
33524071ac1SMauro Carvalho Chehab		"texlive-caption",
33624071ac1SMauro Carvalho Chehab		"texlive-colortbl",
33724071ac1SMauro Carvalho Chehab		"texlive-courier",
33824071ac1SMauro Carvalho Chehab		"texlive-dvips",
33924071ac1SMauro Carvalho Chehab		"texlive-helvetic",
34024071ac1SMauro Carvalho Chehab		"texlive-makeindex",
34124071ac1SMauro Carvalho Chehab		"texlive-metafont",
34224071ac1SMauro Carvalho Chehab		"texlive-metapost",
34324071ac1SMauro Carvalho Chehab		"texlive-palatino",
34424071ac1SMauro Carvalho Chehab		"texlive-preview",
34524071ac1SMauro Carvalho Chehab		"texlive-times",
34624071ac1SMauro Carvalho Chehab		"texlive-zapfchan",
34724071ac1SMauro Carvalho Chehab		"texlive-zapfding",
34824071ac1SMauro Carvalho Chehab	);
34924071ac1SMauro Carvalho Chehab
35024071ac1SMauro Carvalho Chehab	check_rpm_missing(\@suse_tex_pkgs, 1) if ($pdf);
35124071ac1SMauro Carvalho Chehab	check_missing_tex(1) if ($pdf);
35224071ac1SMauro Carvalho Chehab	check_missing(\%map);
35324071ac1SMauro Carvalho Chehab
35424071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
35524071ac1SMauro Carvalho Chehab	printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
35624071ac1SMauro Carvalho Chehab}
35724071ac1SMauro Carvalho Chehab
358800d408aSMauro Carvalho Chehabsub give_mageia_hints()
359800d408aSMauro Carvalho Chehab{
360800d408aSMauro Carvalho Chehab	my %map = (
361800d408aSMauro Carvalho Chehab		"python-sphinx"		=> "python3-sphinx",
362800d408aSMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "python3-sphinx_rtd_theme",
363800d408aSMauro Carvalho Chehab		"virtualenv"		=> "python3-virtualenv",
364800d408aSMauro Carvalho Chehab		"dot"			=> "graphviz",
365800d408aSMauro Carvalho Chehab		"convert"		=> "ImageMagick",
366800d408aSMauro Carvalho Chehab		"Pod::Usage"		=> "perl-Pod-Usage",
367800d408aSMauro Carvalho Chehab		"xelatex"		=> "texlive",
368800d408aSMauro Carvalho Chehab		"rsvg-convert"		=> "librsvg2-tools",
369800d408aSMauro Carvalho Chehab	);
370800d408aSMauro Carvalho Chehab
371800d408aSMauro Carvalho Chehab	my @tex_pkgs = (
372800d408aSMauro Carvalho Chehab		"texlive-fontsextra",
373800d408aSMauro Carvalho Chehab	);
374800d408aSMauro Carvalho Chehab
375800d408aSMauro Carvalho Chehab	check_rpm_missing(\@tex_pkgs, 1) if ($pdf);
376800d408aSMauro Carvalho Chehab	check_missing(\%map);
377800d408aSMauro Carvalho Chehab
378800d408aSMauro Carvalho Chehab	return if (!$need && !$optional);
379800d408aSMauro Carvalho Chehab	printf("You should run:\n\n\tsudo urpmi $install\n");
380800d408aSMauro Carvalho Chehab}
381800d408aSMauro Carvalho Chehab
38224071ac1SMauro Carvalho Chehabsub give_arch_linux_hints()
38324071ac1SMauro Carvalho Chehab{
38424071ac1SMauro Carvalho Chehab	my %map = (
38524071ac1SMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "python-sphinx_rtd_theme",
38624071ac1SMauro Carvalho Chehab		"virtualenv"		=> "python-virtualenv",
38724071ac1SMauro Carvalho Chehab		"dot"			=> "graphviz",
38824071ac1SMauro Carvalho Chehab		"convert"		=> "imagemagick",
38924071ac1SMauro Carvalho Chehab		"xelatex"		=> "texlive-bin",
3908e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "extra/librsvg",
39124071ac1SMauro Carvalho Chehab	);
39224071ac1SMauro Carvalho Chehab
39324071ac1SMauro Carvalho Chehab	my @archlinux_tex_pkgs = (
39424071ac1SMauro Carvalho Chehab		"texlive-core",
39524071ac1SMauro Carvalho Chehab		"texlive-latexextra",
39624071ac1SMauro Carvalho Chehab		"ttf-dejavu",
39724071ac1SMauro Carvalho Chehab	);
39824071ac1SMauro Carvalho Chehab	check_pacman_missing(\@archlinux_tex_pkgs, 1) if ($pdf);
39924071ac1SMauro Carvalho Chehab	check_missing(\%map);
40024071ac1SMauro Carvalho Chehab
40124071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
40224071ac1SMauro Carvalho Chehab	printf("You should run:\n\n\tsudo pacman -S $install\n");
40324071ac1SMauro Carvalho Chehab}
40424071ac1SMauro Carvalho Chehab
40524071ac1SMauro Carvalho Chehabsub give_gentoo_hints()
40624071ac1SMauro Carvalho Chehab{
40724071ac1SMauro Carvalho Chehab	my %map = (
40824071ac1SMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "dev-python/sphinx_rtd_theme",
40924071ac1SMauro Carvalho Chehab		"virtualenv"		=> "dev-python/virtualenv",
41024071ac1SMauro Carvalho Chehab		"dot"			=> "media-gfx/graphviz",
41124071ac1SMauro Carvalho Chehab		"convert"		=> "media-gfx/imagemagick",
41224071ac1SMauro Carvalho Chehab		"xelatex"		=> "dev-texlive/texlive-xetex media-fonts/dejavu",
4138e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "gnome-base/librsvg",
41424071ac1SMauro Carvalho Chehab	);
41524071ac1SMauro Carvalho Chehab
41624071ac1SMauro Carvalho Chehab	check_missing_file("/usr/share/fonts/dejavu/DejaVuSans.ttf",
41724071ac1SMauro Carvalho Chehab			   "media-fonts/dejavu", 1) if ($pdf);
41824071ac1SMauro Carvalho Chehab
41924071ac1SMauro Carvalho Chehab	check_missing(\%map);
42024071ac1SMauro Carvalho Chehab
42124071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
422bba1e4cbSMauro Carvalho Chehab
423bba1e4cbSMauro Carvalho Chehab	printf("You should run:\n\n");
424bba1e4cbSMauro Carvalho Chehab	printf("\tsudo su -c 'echo \"media-gfx/imagemagick svg png\" > /etc/portage/package.use/imagemagick'\n");
425bba1e4cbSMauro Carvalho Chehab	printf("\tsudo su -c 'echo \"media-gfx/graphviz cairo pdf\" > /etc/portage/package.use/graphviz'\n");
426bba1e4cbSMauro Carvalho Chehab	printf("\tsudo emerge --ask $install\n");
427bba1e4cbSMauro Carvalho Chehab
42824071ac1SMauro Carvalho Chehab}
42924071ac1SMauro Carvalho Chehab
43024071ac1SMauro Carvalho Chehabsub check_distros()
43124071ac1SMauro Carvalho Chehab{
43224071ac1SMauro Carvalho Chehab	# Distro-specific hints
43324071ac1SMauro Carvalho Chehab	if ($system_release =~ /Red Hat Enterprise Linux/) {
43424071ac1SMauro Carvalho Chehab		give_redhat_hints;
43524071ac1SMauro Carvalho Chehab		return;
43624071ac1SMauro Carvalho Chehab	}
4379b756a9dSMauro Carvalho Chehab	if ($system_release =~ /CentOS/) {
4389b756a9dSMauro Carvalho Chehab		give_redhat_hints;
4399b756a9dSMauro Carvalho Chehab		return;
4409b756a9dSMauro Carvalho Chehab	}
4419b756a9dSMauro Carvalho Chehab	if ($system_release =~ /Scientific Linux/) {
4429b756a9dSMauro Carvalho Chehab		give_redhat_hints;
4439b756a9dSMauro Carvalho Chehab		return;
4449b756a9dSMauro Carvalho Chehab	}
4459b756a9dSMauro Carvalho Chehab	if ($system_release =~ /Oracle Linux Server/) {
4469b756a9dSMauro Carvalho Chehab		give_redhat_hints;
4479b756a9dSMauro Carvalho Chehab		return;
4489b756a9dSMauro Carvalho Chehab	}
44924071ac1SMauro Carvalho Chehab	if ($system_release =~ /Fedora/) {
45024071ac1SMauro Carvalho Chehab		give_redhat_hints;
45124071ac1SMauro Carvalho Chehab		return;
45224071ac1SMauro Carvalho Chehab	}
45324071ac1SMauro Carvalho Chehab	if ($system_release =~ /Ubuntu/) {
45424071ac1SMauro Carvalho Chehab		give_debian_hints;
45524071ac1SMauro Carvalho Chehab		return;
45624071ac1SMauro Carvalho Chehab	}
45724071ac1SMauro Carvalho Chehab	if ($system_release =~ /Debian/) {
45824071ac1SMauro Carvalho Chehab		give_debian_hints;
45924071ac1SMauro Carvalho Chehab		return;
46024071ac1SMauro Carvalho Chehab	}
46124071ac1SMauro Carvalho Chehab	if ($system_release =~ /openSUSE/) {
46224071ac1SMauro Carvalho Chehab		give_opensuse_hints;
46324071ac1SMauro Carvalho Chehab		return;
46424071ac1SMauro Carvalho Chehab	}
465800d408aSMauro Carvalho Chehab	if ($system_release =~ /Mageia/) {
466800d408aSMauro Carvalho Chehab		give_mageia_hints;
467800d408aSMauro Carvalho Chehab		return;
468800d408aSMauro Carvalho Chehab	}
46924071ac1SMauro Carvalho Chehab	if ($system_release =~ /Arch Linux/) {
47024071ac1SMauro Carvalho Chehab		give_arch_linux_hints;
47124071ac1SMauro Carvalho Chehab		return;
47224071ac1SMauro Carvalho Chehab	}
47324071ac1SMauro Carvalho Chehab	if ($system_release =~ /Gentoo/) {
47424071ac1SMauro Carvalho Chehab		give_gentoo_hints;
47524071ac1SMauro Carvalho Chehab		return;
47624071ac1SMauro Carvalho Chehab	}
47724071ac1SMauro Carvalho Chehab
47824071ac1SMauro Carvalho Chehab	#
47924071ac1SMauro Carvalho Chehab	# Fall-back to generic hint code for other distros
48024071ac1SMauro Carvalho Chehab	# That's far from ideal, specially for LaTeX dependencies.
48124071ac1SMauro Carvalho Chehab	#
48224071ac1SMauro Carvalho Chehab	my %map = (
48324071ac1SMauro Carvalho Chehab		"sphinx-build" => "sphinx"
48424071ac1SMauro Carvalho Chehab	);
48524071ac1SMauro Carvalho Chehab	check_missing_tex(1) if ($pdf);
48624071ac1SMauro Carvalho Chehab	check_missing(\%map);
48724071ac1SMauro Carvalho Chehab	print "I don't know distro $system_release.\n";
48824071ac1SMauro Carvalho Chehab	print "So, I can't provide you a hint with the install procedure.\n";
48924071ac1SMauro Carvalho Chehab	print "There are likely missing dependencies.\n";
49024071ac1SMauro Carvalho Chehab}
49124071ac1SMauro Carvalho Chehab
49224071ac1SMauro Carvalho Chehab#
49324071ac1SMauro Carvalho Chehab# Common dependencies
49424071ac1SMauro Carvalho Chehab#
49524071ac1SMauro Carvalho Chehab
49624071ac1SMauro Carvalho Chehabsub check_needs()
49724071ac1SMauro Carvalho Chehab{
49824071ac1SMauro Carvalho Chehab	if ($system_release) {
4999b756a9dSMauro Carvalho Chehab		print "Detected OS: $system_release.\n";
50024071ac1SMauro Carvalho Chehab	} else {
5019b756a9dSMauro Carvalho Chehab		print "Unknown OS\n";
5029b756a9dSMauro Carvalho Chehab	}
5039b756a9dSMauro Carvalho Chehab
5049b756a9dSMauro Carvalho Chehab	# RHEL 7.x and clones have Sphinx version 1.1.x and incomplete texlive
5059b756a9dSMauro Carvalho Chehab	if (($system_release =~ /Red Hat Enterprise Linux/) ||
5069b756a9dSMauro Carvalho Chehab	    ($system_release =~ /CentOS/) ||
5079b756a9dSMauro Carvalho Chehab	    ($system_release =~ /Scientific Linux/) ||
5089b756a9dSMauro Carvalho Chehab	    ($system_release =~ /Oracle Linux Server/)) {
5099b756a9dSMauro Carvalho Chehab		$virtualenv = 1;
5109b756a9dSMauro Carvalho Chehab		$pdf = 0;
5119b756a9dSMauro Carvalho Chehab
5129b756a9dSMauro Carvalho Chehab		printf("NOTE: On this distro, Sphinx and TexLive shipped versions are incompatible\n");
5139b756a9dSMauro Carvalho Chehab		printf("with doc build. So, use Sphinx via a Python virtual environment.\n\n");
5149b756a9dSMauro Carvalho Chehab		printf("This script can't install a TexLive version that would provide PDF.\n");
51524071ac1SMauro Carvalho Chehab	}
51624071ac1SMauro Carvalho Chehab
51724071ac1SMauro Carvalho Chehab	# Check for needed programs/tools
51824071ac1SMauro Carvalho Chehab	check_sphinx();
51924071ac1SMauro Carvalho Chehab	check_perl_module("Pod::Usage", 0);
52024071ac1SMauro Carvalho Chehab	check_program("make", 0);
52124071ac1SMauro Carvalho Chehab	check_program("gcc", 0);
52224071ac1SMauro Carvalho Chehab	check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
52324071ac1SMauro Carvalho Chehab	check_program("xelatex", 1) if ($pdf);
52424071ac1SMauro Carvalho Chehab	check_program("dot", 1);
52524071ac1SMauro Carvalho Chehab	check_program("convert", 1);
5268e7d5d15SMauro Carvalho Chehab	check_program("rsvg-convert", 1) if ($pdf);
5275f6df00fSMauro Carvalho Chehab	check_program("latexmk", 1) if ($pdf);
52824071ac1SMauro Carvalho Chehab
52924071ac1SMauro Carvalho Chehab	check_distros();
53024071ac1SMauro Carvalho Chehab
53124071ac1SMauro Carvalho Chehab	if ($need_symlink) {
53224071ac1SMauro Carvalho Chehab		printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
53324071ac1SMauro Carvalho Chehab		       which("sphinx-build-3");
53424071ac1SMauro Carvalho Chehab	}
53524071ac1SMauro Carvalho Chehab	if ($need_sphinx) {
5365be33182SMauro Carvalho Chehab		my $activate = "$virtenv_dir/bin/activate";
5375be33182SMauro Carvalho Chehab		if (-e "$ENV{'PWD'}/$activate") {
5385be33182SMauro Carvalho Chehab			printf "\nNeed to activate virtualenv with:\n";
5395be33182SMauro Carvalho Chehab			printf "\t. $activate\n";
5405be33182SMauro Carvalho Chehab		} else {
54124071ac1SMauro Carvalho Chehab			my $virtualenv = findprog("virtualenv-3");
542800d408aSMauro Carvalho Chehab			$virtualenv = findprog("virtualenv-3.5") if (!$virtualenv);
54324071ac1SMauro Carvalho Chehab			$virtualenv = findprog("virtualenv") if (!$virtualenv);
54424071ac1SMauro Carvalho Chehab			$virtualenv = "virtualenv" if (!$virtualenv);
54524071ac1SMauro Carvalho Chehab
5465be33182SMauro Carvalho Chehab			printf "\t$virtualenv $virtenv_dir\n";
5475be33182SMauro Carvalho Chehab			printf "\t. $activate\n";
548fb947f3fSMauro Carvalho Chehab			printf "\tpip install -r $requirement_file\n";
54924071ac1SMauro Carvalho Chehab			$need++;
55024071ac1SMauro Carvalho Chehab		}
5515be33182SMauro Carvalho Chehab	}
55224071ac1SMauro Carvalho Chehab	printf "\n";
55324071ac1SMauro Carvalho Chehab
55424071ac1SMauro Carvalho Chehab	print "All optional dependenties are met.\n" if (!$optional);
55524071ac1SMauro Carvalho Chehab
55624071ac1SMauro Carvalho Chehab	if ($need == 1) {
55724071ac1SMauro Carvalho Chehab		die "Can't build as $need mandatory dependency is missing";
55824071ac1SMauro Carvalho Chehab	} elsif ($need) {
55924071ac1SMauro Carvalho Chehab		die "Can't build as $need mandatory dependencies are missing";
56024071ac1SMauro Carvalho Chehab	}
56124071ac1SMauro Carvalho Chehab
56224071ac1SMauro Carvalho Chehab	print "Needed package dependencies are met.\n";
56324071ac1SMauro Carvalho Chehab}
56424071ac1SMauro Carvalho Chehab
56524071ac1SMauro Carvalho Chehab#
56624071ac1SMauro Carvalho Chehab# Main
56724071ac1SMauro Carvalho Chehab#
56824071ac1SMauro Carvalho Chehab
56924071ac1SMauro Carvalho Chehabwhile (@ARGV) {
57024071ac1SMauro Carvalho Chehab	my $arg = shift(@ARGV);
57124071ac1SMauro Carvalho Chehab
57224071ac1SMauro Carvalho Chehab	if ($arg eq "--no-virtualenv") {
57324071ac1SMauro Carvalho Chehab		$virtualenv = 0;
57424071ac1SMauro Carvalho Chehab	} elsif ($arg eq "--no-pdf"){
57524071ac1SMauro Carvalho Chehab		$pdf = 0;
57624071ac1SMauro Carvalho Chehab	} else {
57724071ac1SMauro Carvalho Chehab		print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf>\n\n";
57824071ac1SMauro Carvalho Chehab		exit -1;
57924071ac1SMauro Carvalho Chehab	}
58024071ac1SMauro Carvalho Chehab}
58124071ac1SMauro Carvalho Chehab
58224071ac1SMauro Carvalho Chehab#
58324071ac1SMauro Carvalho Chehab# Determine the system type. There's no standard unique way that would
58424071ac1SMauro Carvalho Chehab# work with all distros with a minimal package install. So, several
58524071ac1SMauro Carvalho Chehab# methods are used here.
58624071ac1SMauro Carvalho Chehab#
58724071ac1SMauro Carvalho Chehab# By default, it will use lsb_release function. If not available, it will
58824071ac1SMauro Carvalho Chehab# fail back to reading the known different places where the distro name
58924071ac1SMauro Carvalho Chehab# is stored
59024071ac1SMauro Carvalho Chehab#
59124071ac1SMauro Carvalho Chehab
59224071ac1SMauro Carvalho Chehab$system_release = qx(lsb_release -d) if which("lsb_release");
59324071ac1SMauro Carvalho Chehab$system_release =~ s/Description:\s*// if ($system_release);
59424071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/system-release") if !$system_release;
59524071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/redhat-release") if !$system_release;
59624071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/lsb-release") if !$system_release;
59724071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/gentoo-release") if !$system_release;
59824071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/issue") if !$system_release;
59924071ac1SMauro Carvalho Chehab$system_release =~ s/\s+$//;
60024071ac1SMauro Carvalho Chehab
60124071ac1SMauro Carvalho Chehabcheck_needs;
602