#!/usr/bin/perl # 22:29:36 5/18/2014. The legacy Lib-Apollia, kept here temporarily for anything that still needs it. # # Also here for educational purposes, so you can see how horrible the original code was, # and how much I've improved as a programmer since writing the below. ################ # # Constants for readability use constant false => 0; use constant true => 1; # # End of Constants for readability # ################ ################ # Here, we get the disk this library is on (which is presumably # the disk where the rest of the setup kit is as well). # # The following assumes that disk or partition paths all begin with # the following 5 characters: /mnt/ if (!$library_shouldnt_get_setup_kit_disk) { open APLIB_DISK, "/tmp/apollia_perl_library_disk.txt"; $setup_kit_disk = ; close APLIB_DISK; } # # End of section where we get the disk this library is on. # ################ ################ # # Includes # use File::Basename; # # End of Includes section # ################ $setup_kit_disk="/mnt/" . $setup_kit_disk . "/"; $relative_path_to_setup_kit="Meadow/Lupu528 Setup Kit"; $folder_for_autoinstallation_files="Files for Auto-Installation"; $full_path_to_setup_kit = $setup_kit_disk . $relative_path_to_setup_kit . "/"; $full_path_to_spring_files = "/mnt/truecrypt1/Meadow/Spring/"; $full_path_to_hot_spring_files = "/mnt/truecrypt1/Meadow/Hot Spring/"; #$full_path_to_spring_files = "/mnt/truecrypt1/Hobbiton/Meadow/Spring/"; #$full_path_to_hot_spring_files = "/mnt/truecrypt1/Hobbiton/Meadow/Hot Spring/"; $full_path_to_geany_config_folder = "/root/.config/geany/"; $full_path_to_autoinstallation_files = $full_path_to_setup_kit . $folder_for_autoinstallation_files . "/"; $full_path_to_autoinstallation_perl_scripts = $full_path_to_setup_kit . "Auto-Installer Perl Scripts"; $full_path_to_setup_kit_icons = $full_path_to_autoinstallation_files . "Icons/"; sub GetPathParentdir { $this_path=$_[0]; my ($whocares, $this_unescaped_parentdir, $whocares)=fileparse($this_path); #my ($whocares, $this_scripts_unescaped_parentdir, $whocares)=fileparse($unescaped_script_path); return $this_unescaped_parentdir; } sub GetPathDisk { $this_path=$_[0]; $mnt_loc=index($this_path, "/mnt/"); if ( $mnt_loc == -1) { print "Could not figure out path's disk, no /mnt/ in this path!: $this_path\n\n"; return false; #die; } else { # The following assumes that disk or partition paths all begin with # the following 5 characters: /mnt/ $skip_mnt = 5; $loc_of_slash_after_disk_name = index($this_path, "/", $skip_mnt); $num_of_chars_to_get = $loc_of_slash_after_disk_name-$skip_mnt; $disk=substr($this_path, $skip_mnt, $num_of_chars_to_get); } return $disk; } sub AddSlashIfNeededTo { $this_string=$_[0]; $lastchar = substr($this_string,length($this_string)-1,1); if ($lastchar ne "/") { $this_string .= "/"; $_[0]=$this_string; } } sub DirNameOnlyOf { $this_path=$_[0]; $dirnameonly=basename($this_path); return $dirnameonly; } sub FileNameOnlyOf { $this_path=$_[0]; my ($filenameonly, $whocares1, $whocares2)=fileparse($this_path); return $filenameonly; } sub ParentDirsOf { $this_path=$_[0]; my ($whocares, $parentdirs, $whocares)=fileparse($this_path); return $parentdirs; } sub DeleteDirsRecursively { @dirs_to_delete=@_; foreach $dir_to_delete (@dirs_to_delete) { $result = remove (\1, $dir_to_delete); if ($result !=0) { print "Dir delete result: $result "; } } } sub DeleteFilesLoop { @files_to_delete=@_; DeleteFiles(@files_to_delete); } sub DeleteSymlink { $symlink_to_delete=shift(@_); if (-e $symlink_to_delete) { if (-l $symlink_to_delete) { print "Symlink "; DeleteFile($symlink_to_delete); } else { print "Couldn't delete: $symlink_to_delete This path is not a symlink!\n"; } } else { print "Couldn't delete: $symlink_to_delete No symlink or anything else at that path!\n"; } } sub DeleteFile { $file_to_delete=shift(@_); if (!-e $file_to_delete) { print "\nNot deleted: $file_to_delete Didn't exist in the first place.\n"; } else { unlink ($file_to_delete); print "Deleted: $file_to_delete\n"; } } sub DeleteFiles { @files_to_delete=@_; foreach $file_to_delete (@files_to_delete) { if (!-e $file_to_delete) { print "\nNot deleted: $file_to_delete Didn't exist in the first place.\n"; } else { unlink ($file_to_delete); print "Deleted: $file_to_delete\n"; } } } sub PrefixHashKeysWith { $prefix=shift(@_); %this_hash=@_; foreach $key (keys %this_hash) { $new_hash_key = $prefix . $key; $this_hash{$new_hash_key} = $this_hash{$key}; delete $this_hash{$key}; } return %this_hash; } sub MakeHashOfAutoInstallFilePathsAbsolute { $prefix=shift(@_); %this_hash=@_; %return_hash = PrefixHashKeysWith($prefix, %this_hash); return %return_hash; } sub CopyAndRenameIndividualAutoInstallFiles { # 08:27 04/19/2012. Incomplete, I think. $prefix=shift(@_); %this_hash=@_; %absolute_paths_of_individual_files_to_copy_and_rename_and_their_destinations=MakeHashOfAutoInstallFilePathsAbsolute($prefix, %this_hash); CopyAndRenameIndividualFiles(%absolute_paths_of_individual_files_to_copy_and_rename_and_their_destinations); } sub CopyAutoInstallDirs { $prefix=shift(@_); %this_hash=@_; %absolute_paths_of_dirs_to_copy_and_their_dest_folders = PrefixHashKeysWith($prefix, %this_hash); CopyDirs(%absolute_paths_of_dirs_to_copy_and_their_dest_folders); } sub CopyDirs { %paths_of_dirs_to_copy_and_their_dest_folders = @_; while (($from_path, $to_path) = each (%paths_of_dirs_to_copy_and_their_dest_folders)) { $thisdirnameonly=DirNameOnlyOf($from_path); AddSlashIfNeededTo($to_path); #echo "Frompath: $from_path \n Topath: $to_path \n"; #die; $to_path=$to_path . $thisdirnameonly; if ($copy_will_overwrite_existing_files == true) { dircopy($from_path,$to_path) or die "hmm Copy failed:\n $from_path -> $to_path $!"; print "\nCopied Successfully: $from_path -> $to_path\n"; } else { if (!-e $to_path) { dircopy($from_path,$to_path) or die "Copy failed: $from_path -> $to_path $!"; print "\nCopied Successfully: $from_path -> $to_path\n"; } else { print "\nAlready exists, so not copied: $from_path -> $to_path\n"; } } } } sub CopyIndividualAutoInstallFiles { $prefix=shift(@_); %this_hash=@_; %absolute_paths_of_individual_files_to_copy_and_their_dest_folders = PrefixHashKeysWith($prefix, %this_hash); CopyIndividualFiles(%absolute_paths_of_individual_files_to_copy_and_their_dest_folders); } sub ExperimentalCopyIndividualFilesLoop { while (($thisfilepath, $to_dir) = each (%files)) { $thisfile=FileNameOnlyOf($thisfilepath); AddSlashIfNeededTo($to_dir); $to_path=$to_dir . $thisfile; if ($copy_will_refuse_to_overwrite_symlink == true) { if (-l $to_path) { print "COPY FAILED - destination path is a symlink! $from_path -> $to_path \n"; next; } } if ($copy_will_overwrite_existing_files == true) { # In this case, overwriting files is OK, so we don't have to # check whether anything already exists at the copy destination. if (!copy($from_path,$to_path) ) { # We attempt the copy. # If it failed, we report that. print "\nCOPY FAILED: $from_path -> $to_path $!\n"; } else { # If it succeeded, we report that. print "\nCopied Successfully: $from_path -> $to_path\n"; } } else # If existing files shouldn't be overwritten... { if (!-e $to_path) # If path doesn't exist... { if (!copy($from_path,$to_path) ) # We attempt the copy. # If it failed, we report that. { print "\nCOPY FAILED: $from_path -> $to_path $!\n"; } else # If it succeeded, we report that. { print "\nCopied Successfully: $from_path -> $to_path\n"; } } else # The path already exists, and we're not going to overwrite it. { print "\nAlready exists, so not copied: $from_path -> $to_path\n"; } } } } sub CopyFile { # 16:16 02/25/2012. Does no formatting of paths, just attempts the # copy. This is so both the Copy and CopyAndRename functions can # use this. $from_path=shift(@_); $to_path=shift(@_); if ($copy_will_refuse_to_overwrite_symlink == true) { if (-l $to_path) { print "COPY FAILED - destination path is a symlink! $from_path -> $to_path \n"; next; } } if ($copy_will_overwrite_existing_files == true) { # In this case, overwriting files is OK, so we don't have to # check whether anything already exists at the copy destination. if (!copy($from_path,$to_path) ) { # We attempt the copy. # If it failed, we report that. print "\nCOPY FAILED: $from_path -> $to_path $!\n"; if (!-e $from_path) { print "File you're trying to copy doesn't exist: $from_path\n\n"; } if (!-e $to_path) { print "Destination folder doesn't exist: $to_dir\n\n"; } } else { # If it succeeded, we report that. print "\nCopied Successfully: $from_path -> $to_path\n"; } } else # If existing files shouldn't be overwritten... { if (!-e $to_path) # If path doesn't exist... { if (!copy($from_path,$to_path) ) # We attempt the copy. # If it failed, we report that. { print "\nCOPY FAILED: $from_path -> $to_path $!\n"; } else # If it succeeded, we report that. { print "\nCopied Successfully: $from_path -> $to_path\n"; } } else # The path already exists, and we're not going to overwrite it. { print "\nAlready exists, so not copied: $from_path -> $to_path\n"; } } } sub CopyAndRenameIndividualFiles { %paths_of_individual_files_to_copy_and_their_destinations = @_; while (($from_path, $to_path) = each (%paths_of_individual_files_to_copy_and_their_destinations)) { CopyFile($from_path, $to_path); } } sub CopyIndividualFiles { %paths_of_individual_files_to_copy_and_their_dest_folders = @_; while (($from_path, $to_dir) = each (%paths_of_individual_files_to_copy_and_their_dest_folders)) { $thisfile=FileNameOnlyOf($from_path); AddSlashIfNeededTo($to_dir); $to_path=$to_dir . $thisfile; if ($copy_will_refuse_to_overwrite_symlink == true) { if (-l $to_path) { print "COPY FAILED - destination path is a symlink! $from_path -> $to_path \n"; next; } } if ($copy_will_overwrite_existing_files == true) { # In this case, overwriting files is OK, so we don't have to # check whether anything already exists at the copy destination. if (!copy($from_path,$to_path) ) { # We attempt the copy. # If it failed, we report that. print "\nCOPY FAILED: $from_path -> $to_path $!\n"; if (!-e $from_path) { print "File you're trying to copy doesn't exist: $from_path\n\n"; } if (!-e $to_path) { print "Destination folder doesn't exist: $to_dir\n\n"; } } else { # If it succeeded, we report that. print "\nCopied Successfully: $from_path -> $to_path\n"; } } else # If existing files shouldn't be overwritten... { if (!-e $to_path) # If path doesn't exist... { if (!copy($from_path,$to_path) ) # We attempt the copy. # If it failed, we report that. { print "\nCOPY FAILED: $from_path -> $to_path $!\n"; } else # If it succeeded, we report that. { print "\nCopied Successfully: $from_path -> $to_path\n"; } } else # The path already exists, and we're not going to overwrite it. { print "\nAlready exists, so not copied: $from_path -> $to_path\n"; } } } } sub GrantFull777Permissions { @files=@_; foreach $file (@files) { $res=chmod 0777, $file; if ($res>0) { print "Granted $file 777 permissions.\n\n"; } else { print "Failed to grant $file 777 permissions.\n\n"; } } } sub SymlinkAllOf # 0th arg: the directory that we're going to make symlinks to all the # files of. # 1st arg: unused. just for readability. # 2nd arg: the place where the symlinks are going to be put. { my $dir_to_read=$_[0]; my $dir_to_contain_symlinks=$_[2]; AddSlashIfNeededTo($dir_to_read); AddSlashIfNeededTo($dir_to_contain_symlinks); opendir(DIR, $dir_to_read); @files=readdir(DIR); foreach $file (@files) { if ($file eq "." or $file eq "..") { next; } $symlink_dest = $dir_to_read . $file; $symlink_loc = $dir_to_contain_symlinks . $file; MakeSymlinkTo($symlink_dest, "and put it at", $symlink_loc); } } sub MakeSymlinkTo { # 0th arg: symlink dest, what the symlink is pointing at. # 1st arg: unused. just for readability. # 2nd arg: the place where the symlink will be made. my $symlink_dest=$_[0]; my $symlink_loc=$_[2]; $symlinking_result=symlink ($symlink_dest, $symlink_loc); if ($symlinking_result==1) { print "Successfully created symlink at: $symlink_loc Pointing to: $symlink_dest.\n\n"; return true; } else { print "FAILED to create symlink at $symlink_loc Pointing to: $symlink_dest.\n"; if (-e $symlink_loc) { print " Something already exists at intended location for symlink: $symlink_loc\n\n"; } if (!-e $symlink_dest) { print " Destination of symlink doesn't exist: $thing_being_symlinked\n\n"; } return false; } } sub MakeSymlinks { %symlinks=@_; $num_of_symlinks=0; while ( ($symlink_loc, $symlink_dest) = each (%symlinks)) { print "Symlink dest: $symlink_dest Loc: $symlink_loc \n\n"; $symlinking_result=symlink ($symlink_dest, $symlink_loc); if ($symlinking_result==1) { print "Successfully created symlink at $symlink_loc pointing to $symlink_dest.\n\n"; } else { print "FAILED to create symlink at $symlink_loc pointing to $symlink_dest.\n"; if (-e $symlink_loc) { print " Something already exists at intended location for symlink: $symlink_loc\n\n"; } if (!-e $symlink_dest) { print " Destination of symlink doesn't exist: $thing_being_symlinked\n\n"; } } $num_of_symlinks++; } print "Num of symlinks: $num_of_symlinks"; } sub MakeDir { $mkdir_path=$_[0]; $silent_or_not=$_[1]; if (!-e $mkdir_path) { $result = mkdir $mkdir_path; if ($silent_or_not eq "Silent") { return $result; } if ($result == true) { print "\nMade directory: $mkdir_path\n"; } else { print "\nCouldn't make directory: $!"; } } else { if ($silent_or_not ne "SilentIfExists") { print "\nCouldn't make directory: $mkdir_path Something exists there already.\n"; } } return $result; } sub CreateDeepDirs { @dirs_to_make=@_; foreach $mkdir_path (@dirs_to_make) { $current_path=""; @split_mkdir_path = split("/", $mkdir_path); foreach $path_piece (@split_mkdir_path) { $current_path = $current_path . $path_piece . "/"; #MakeDir($current_path, "SilentIfExists"); MakeDir($current_path); } } } sub CreateDirs { @dirs_to_make=@_; foreach $mkdir_path (@dirs_to_make) { MakeDir($mkdir_path); } } sub InstallProgramViaShellCommand { $automatically_or_not=$_[0]; $folder_to_run_shell_command_in=$_[1]; $shell_command=$_[2]; $asktodoaction_text=$_[3]; $action_announcement=$_[4]; chdir $full_path_to_autoinstallation_files . $folder_to_run_shell_command_in; if ($automatically_or_not eq false) { $do_action=AskToDoAction($asktodoaction_text); } else { $do_action=true; } DoActionOrNot($do_action, $action_announcement, $shell_command); return $do_action; } sub InstallPet { $automatically_or_not=$_[0]; $pet_folder=$_[1]; $pet_file=$_[2]; $program_name=$_[3]; chdir $full_path_to_autoinstallation_files . $pet_folder; $asktodoaction_text="install $program_name"; $action_announcement="Installing $program_name" . "..."; if ($automatically_or_not == false) { $do_action=AskToDoAction($asktodoaction_text); $petget_command="petget"; } elsif ($automatically_or_not == true) { $do_action=true; $petget_command="petgetjet"; } else { $do_action=false; } DoActionOrNot($do_action, $action_announcement, "$petget_command $pet_file"); return $do_action; } sub DoActionOrNot { $do_action=$_[0]; $action_announcement=$_[1]; $action=$_[2]; if ($do_action == false) { print "\nNot $action_announcement\n\n"; return false; } if ($do_action == true) { print "\n$action_announcement\n\n"; print "Command line being executed: # $action\n\n"; system("$action"); return true; } } sub AskToDoAction { $action=$_[0]; print "\n\nPress Return to "; print $action; print ", or 'q' (without quotes) to quit, or 's' to skip this step.\n> "; $command = ; chomp($command); if ($command eq "q") { print "\n Aborting script..."; die; } if ($command eq 's') { return false; } return true; } sub EndScript { $launch_parameter=$_[0]; if ($launch_parameter == 1 || !$launch_parameter) { # Then don't quit until user presses Return. print "\n\nScript finished!\n"; print "\nYou can press Return (or Enter) to close this window.\n"; $wait_for_return = ; # If you've rather have the rxvt window automatically disappear after the # script is finished running, comment out the previous two lines, and # uncomment the next lines. # $secs=5; # print "\n\nThis window will automatically close in $secs seconds.\n\n\n"; # sleep $secs; die; } elsif ($launch_parameter == 2) { # Then wait $secs seconds, then quit. print "\n\nScript finished!\n"; #print "\nYou can press Return (or Enter) to close this window.\n"; #$wait_for_return = ; # If you've rather have the rxvt window automatically disappear after the # script is finished running, comment out the previous two lines, and # uncomment the next lines. $secs=5; print "\n\nThis window will automatically close in $secs seconds.\n\n\n"; sleep $secs; die; } elsif ($launch_parameter == 3) { # Then immediately quit. die; } } 1;