anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

extras (5444B)

      1 #!/usr/bin/env perl
      2 
      3 #
      4 # Script taken from InspIRCd, https://www.inspircd.org/
      5 #
      6 # This program is distributed in the hope that it will be useful, but WITHOUT
      7 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      8 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
      9 # details.
     10 #
     11 # You should have received a copy of the GNU General Public License
     12 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
     13 #
     14 
     15 
     16 BEGIN { require 5.8.0; }
     17 
     18 use strict;
     19 use warnings FATAL => qw(all);
     20 
     21 use File::Copy ();
     22 use Cwd;
     23 
     24 sub list_extras ();
     25 sub enable_extras (@);
     26 sub disable_extras (@);
     27 
     28 # Routine to list out the extra/ modules that have been enabled.
     29 # Note: when getting any filenames out and comparing, it's important to lc it if the
     30 # file system is not case-sensitive (== Epoc, MacOS, OS/2 (incl DOS/DJGPP), VMS, Win32
     31 # (incl NetWare, Symbian)). Cygwin may or may not be case-sensitive, depending on
     32 # configuration, however, File::Spec does not currently tell us (it assumes Unix behavior).
     33 sub list_extras () {
     34 	use File::Spec;
     35 	# @_ not used
     36 	my $srcdir = File::Spec->catdir("modules");
     37 	my $abs_srcdir = File::Spec->rel2abs($srcdir);
     38 	local $_;
     39 	my $dd;
     40 	opendir $dd, File::Spec->catdir($abs_srcdir, "extra") or die (File::Spec->catdir($abs_srcdir, "extra") . ": $!\n");
     41 	my @extras = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
     42 	closedir $dd;
     43 	undef $dd;
     44 	opendir $dd, $abs_srcdir or die "$abs_srcdir: $!\n";
     45 	my @sources = map { File::Spec->case_tolerant() ? lc($_) : $_ } (readdir($dd));
     46 	closedir $dd;
     47 	undef $dd;
     48 	my $maxlen = (sort { $b <=> $a } (map {length($_)} (@extras)))[0];
     49 	my %extras = ();
     50 EXTRA:	for my $extra (@extras) {
     51 		next if (File::Spec->curdir() eq $extra || File::Spec->updir() eq $extra);
     52 		my $abs_extra = File::Spec->catfile($abs_srcdir, "extra", $extra);
     53 		my $abs_source = File::Spec->catfile($abs_srcdir, $extra);
     54 		next unless ($extra =~ m/\.(cpp|h)$/ || (-d $abs_extra)); # C++ Source/Header, or directory
     55 		if (-l $abs_source) {
     56 			# Symlink, is it in the right place?
     57 			my $targ = readlink($abs_source);
     58 			my $abs_targ = File::Spec->rel2abs($targ, $abs_srcdir);
     59 			if ($abs_targ eq $abs_extra) {
     60 				$extras{$extra} = "\e[32;1menabled\e[0m";
     61 			} else {
     62 				$extras{$extra} = sprintf("\e[31;1mwrong symlink target (%s)\e[0m", $abs_targ);
     63 			}
     64 		} elsif (-e $abs_source) {
     65 			my ($devext, $inoext) = stat($abs_extra);
     66 			my ($devsrc, $inosrc, undef, $lnksrc) = stat($abs_source);
     67 			if ($lnksrc > 1) {
     68 				if ($devsrc == $devext && $inosrc == $inoext) {
     69 					$extras{$extra} = "\e[32;1menabled\e[0m";
     70 				} else {
     71 					$extras{$extra} = sprintf("\e[31;1mwrong hardlink target (%d:%d)\e[0m", $devsrc, $inosrc);
     72 				}
     73 			} else {
     74 				open my $extfd, "<", $abs_extra;
     75 				open my $srcfd, "<", $abs_source;
     76 				local $/ = undef;
     77 				if (scalar(<$extfd>) eq scalar(<$srcfd>)) {
     78 					$extras{$extra} = "\e[32;1menabled\e[0m";
     79 				} else {
     80 					$extras{$extra} = sprintf("\e[31;1mout of synch (re-copy)\e[0m");
     81 				}
     82 			}
     83 		} else {
     84 			$extras{$extra} = "\e[33;1mdisabled\e[0m";
     85 		}
     86 	}
     87 
     88 	for my $extra (sort {$a cmp $b} keys(%extras)) {
     89 		my $text = $extras{$extra};
     90 		if ($text =~ m/needed by/ && $text !~ m/enabled/) {
     91 			printf "\e[31;1;5m%-*s = %s%s\e[0m\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? ")" : "");
     92 		} else {
     93 			printf "%-*s = %s%s\n", $maxlen, $extra, $text, ($text =~ m/needed by/ ? "\e[0m)" : "");
     94 		}
     95 	}
     96 	return keys(%extras) if wantarray; # Can be used by manage_extras.
     97 }
     98 
     99 sub enable_extras (@) {
    100 	my (@extras) = @_;
    101 	for my $extra (@extras) {
    102 		my $extrapath = "modules/extra/$extra";
    103 		if (!-e $extrapath) {
    104 			print STDERR "Cannot enable \e[32;1m$extra\e[0m : No such file or directory in modules/extra\n";
    105 			next;
    106 		}
    107 		my $source = "modules/$extra";
    108 		if (-e $source) {
    109 			print STDERR "Cannot enable \e[32;1m$extra\e[0m : destination in modules exists (might already be enabled?)\n";
    110 			next;
    111 		}
    112 		print "Enabling $extra ... \n";
    113 		symlink "extra/$extra", $source or print STDERR "$source: Cannot link to 'extra/$extra': $!\n";
    114 	}
    115 }
    116 
    117 sub disable_extras (@)
    118 {
    119 	opendir my $dd, "modules/extra/";
    120 	my @files = readdir($dd);
    121 	closedir $dd;
    122 	my (@extras) = @_;
    123 EXTRA:	for my $extra (@extras) {
    124 		my $extrapath = "modules/extra/$extra";
    125 		my $source = "modules/$extra";
    126 		if (!-e $extrapath) {
    127 			print STDERR "Cannot disable \e[32;1m$extra\e[0m : Is not an extra\n";
    128 			next;
    129 		}
    130 		if ((! -l $source) || readlink($source) ne "extra/$extra") {
    131 			print STDERR "Cannot disable \e[32;1m$extra\e[0m : Source is not a link or doesn't refer to the right file. Remove manually if this is in error.\n";
    132 			next;
    133 		}
    134 		# Now remove.
    135 		print "Disabling $extra ... \n";
    136 		unlink "modules/$extra" or print STDERR "Cannot disable \e[32;1m$extra\e[0m : $!\n";
    137 	}
    138 }
    139 
    140 my $clearscreen = `clear`;
    141 print $clearscreen;
    142 while (1)
    143 {
    144 	list_extras;     # print the module list
    145 	print "\nPlease enter the name of the module or type 'q' to quit.: ";
    146 	my $input = <STDIN>;
    147 	chop($input); # remove the trailing \n from the user input
    148 
    149 	if ($input eq "q") {
    150 		if (-e "build/CMakeFiles") {
    151 			system("cmake", "build/.");
    152 			print "\nNow cd build, then run make to build Anope.\n\n";
    153 		} else {
    154 			print "\nBuild directory not found. You should run ./Config now.\n\n"
    155 		}
    156 		exit 0;
    157 	}
    158 	print $clearscreen;
    159 	if ($input eq "") {
    160 		next;
    161 	}
    162 
    163 	if (-e "modules/$input") {
    164 		disable_extras($input)
    165 	} else {
    166 		enable_extras($input)
    167 	}
    168 }