-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodify-files.pl
More file actions
126 lines (109 loc) · 3.94 KB
/
modify-files.pl
File metadata and controls
126 lines (109 loc) · 3.94 KB
1
2
3
4
5
6
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use strict;
use warnings;
use File::Copy;
sub zsh {
my $whoami = getpwuid($<);
my $zshrc_path = "/home/".$whoami."/.zshrc";
my $zshrc_backup_path = "/home/".$whoami."/.zshrc.bak";
my $zsh_theme = "jovial";
my @plugins = (
'command-not-found', 'git', 'dotenv', 'ssh', 'sudo', 'zsh-autosuggestions', 'zsh-syntax-highlighting', 'jovial',
);
my @aliases = (
"# Custom aliases:",
"alias up='sudo apt update && sudo apt upgrade -y'",
"alias ll='ls -lahF'",
"alias nf='neofetch'",
"alias sz='source ~/.zshrc'",
"alias python='python3'",
"alias count='wc -l'",
"alias print-arch='dpkg --print-architechture'"
);
# Backup original zshrc
copy($zshrc_path, $zshrc_backup_path) or die "Failed to create backup: $!, $zshrc_path, $zshrc_backup_path";
my @lines = do {
open(my $fh, '<', $zshrc_path) or die "Can't open file: $!";
<$fh>;
};
my $in_plugins_block = 0;
my @collected_plugins = ();
my @new_lines = ();
# Join plugins to string and replace the existing plugins line
foreach my $line (@lines) {
if ($line =~ /^\s*plugins\s*=\s*\(/) {
$in_plugins_block = 1;
@collected_plugins = ();
next;
}
if ($in_plugins_block) {
if ($line =~ /\)/) {
$in_plugins_block = 0;
my $plugins_str = join(' ', @plugins);
push @new_lines, "plugins=($plugins_str)\n";
}
# Ignore existing plugins
next;
} else {
# Change theme
$line =~ s/ZSH_THEME=".*"/ZSH_THEME="$zsh_theme"/;
push @new_lines, $line;
}
}
# Join aliases to the end of .zshrc
my %existing_lines = map { $_ => 1 } @lines;
foreach my $alias (@aliases) {
push(@new_lines, "$alias\n") unless $existing_lines{$alias};
}
# Write the modified lines back to the file
open(my $fh, '>', $zshrc_path) or die "Can't write to file: $!";
print $fh @new_lines;
close($fh);
print("Zsh configuration successful!\n");
}
sub php {
my $php_ini_path = "/etc/php/8.4/apache2/php.ini";
my $backup_php_path = "/etc/php/8.4/apache2/php.ini.bak";
# Backup original php.ini
copy($php_ini_path, $backup_php_path) or die "Failed to create backup: $!";
my @lines = do {
open(my $fh, '<', $php_ini_path) or die "Can't open file: $!";
<$fh>;
};
# Modify php.ini
for (@lines) {
s/^memory_limit\s*=.*/memory_limit = 256M/;
s/^upload_max_filesize\s*=.*/upload_max_filesize = 10M/;
s/^post_max_size\s*=.*/post_max_size = 12M/;
s/^max_execution_time\s*=.*/max_execution_time = 60/;
s/^max_input_time\s*=.*/max_input_time = 60/;
s/^error_reporting\s*=.*/error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE/;
s/^display_errors\s*=.*/display_errors = Off/;
s/^log_errors\s*=.*/log_errors = On/;
s/^error_log\s*=.*/error_log = \/var\/log\/php_errors.log/;
s/^session\.save_path\s*=.*/session.save_path = "\/var\/lib\/php\/sessions"/;
s/^disable_functions\s*=.*/disable_functions = exec,shell_exec,system,passthru/;
s/^expose_php\s*=.*/expose_php = Off/;
s/^file_uploads\s*=.*/file_uploads = On/;
}
# Write back to a temp file to avoid corruption
my $temp_file = "$php_ini_path.tmp";
open(my $fh_out, '>', $temp_file) or die "Can't open temp file: $!";
print $fh_out @lines;
close($fh_out);
# Move the temp file to the original php.ini
rename $temp_file, $php_ini_path or die "Failed to update php.ini: $!";
print("PHP configuration successful! \n");
}
if (@ARGV) {
my $command = shift @ARGV;
if ($command eq 'zsh') {
zsh();
} elsif ($command eq 'php') {
php();
}
else {
print "Unknown command: $command\n";
}
} else {
print "No command provided.\n";
}