modify_headers.pl 3.3 KB
Newer Older
1
#!/usr/bin/perl
2 3 4 5
#!/usr/bin/perl -w
# /**
#  * ---------------------------------------------------------------------
#  * GLPI - Gestionnaire Libre de Parc Informatique
6
#  * Copyright (C) 2015-2018 Teclib' and contributors.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#  *
#  * http://glpi-project.org
#  *
#  * based on GLPI - Gestionnaire Libre de Parc Informatique
#  * Copyright (C) 2003-2014 by the INDEPNET Development Team.
#  *
#  * ---------------------------------------------------------------------
#  *
#  * LICENSE
#  *
#  * This file is part of GLPI.
#  *
#  * GLPI is free software; you can redistribute it and/or modify
#  * it under the terms of the GNU General Public License as published by
#  * the Free Software Foundation; either version 2 of the License, or
#  * (at your option) any later version.
#  *
#  * GLPI is distributed in the hope that it will be useful,
#  * but WITHOUT ANY WARRANTY; without even the implied warranty of
#  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  * GNU General Public License for more details.
#  *
#  * You should have received a copy of the GNU General Public License
#  * along with GLPI. If not, see <http://www.gnu.org/licenses/>.
#  * ---------------------------------------------------------------------
# */
33 34 35 36 37

use File::Basename;
my $dirname = dirname(__FILE__);

do_dir("$dirname/..");
38 39 40


sub do_dir{
41
local ($dir)=@_;
42
#print "Entering $dir\n";
43

44 45
opendir(DIRHANDLE,$dir)||die "ERROR: can not read current directory\n";
foreach (readdir(DIRHANDLE)){
46 47
	if ($_ ne '..' && $_ ne '.'){
		if (-d "$dir/$_"){
48 49
			# Excluded directories
			if ($_ !~ m/.git/i && $_ !~ m/lib/i && $_ !~ m/plugins/i && $_ !~ m/vendor/i){
50 51 52 53
				do_dir("$dir/$_");
			}
		} else {
	 		if(!(-l "$dir/$_")){
54 55 56 57 58 59 60
				# Included filetypes - php, css, js => default comment style
				if ((index($_,".php",0)!=-1)||(index($_,".css",0)!=-1)||(index($_,".js",0)!=-1)){
					do_file("$dir/$_", "");
	 			}
				# Included filetypes - sql, sh, pl => Add a specific comment style (#)
				if ((index($_,".sql",0)!=-1)||(index($_,".sh",0)!=-1)||(index($_,".pl",0)!=-1)){
					do_file("$dir/$_", "# ");
61 62 63 64 65
	 			}
			}
		}
	}
}
66
closedir DIRHANDLE;
67 68 69 70

}

sub do_file{
71
	local ($file, $format)=@_;
72 73 74 75 76
    #if($format ne "") {
    #    print $file." (Using specific comment " . $format . ")\n";
    #} else {
    #    print $file."\n";
    #}
77

78 79 80
	### DELETE HEADERS
	open(INIT_FILE,$file);
	@lines=<INIT_FILE>;
81
	close(INIT_FILE);
82 83 84 85 86

	open(TMP_FILE,">/tmp/tmp_glpi.txt");

	$status='';
	foreach (@lines){
87 88
		# Did we found header closure tag ?
		if ($_ =~ m/$format\*\//){
89
			$status="END";
90
		}
91

92
		# If we have reach the header closure tag, we print the rest of the file
93
		if ($status =~ m/END/||$status !~ m/BEGIN/){
94 95
			print TMP_FILE $_;
		}
96

97
		# If we haven't reach the header closure tag
98
		if ($status !~ m/END/){
99 100
			# If we found the header open tag...
			if ($_ =~ m/$format\/\*\*/){
101 102
				$status="BEGIN";
				##### ADD NEW HEADERS
103 104
				my $dirname = dirname(__FILE__);
				open(HEADER_FILE,"$dirname/HEADER");
105 106
				@headers=<HEADER_FILE>;
				foreach (@headers){
107
					print TMP_FILE $format;
moyooo's avatar
moyooo committed
108
					print TMP_FILE $_;
109 110
				}
				close(HEADER_FILE) ;
111
			}
112 113
		}
	}
114
	close(TMP_FILE);
115 116
	system("cp -f /tmp/tmp_glpi.txt $file");

117 118
    # If we haven't found an header on the file, report it
    if($status eq '') {
119
        print "Unable to found an header on $file - please add it manually\n";
120
        #exit 1;
121
    }
122
}