#!/usr/bin/perl # make-welcome-to-disk-text-file.pl # Version 1.0 # # by Apollia of Astroblahhh.Com. http://astroblahhh.com/ # # # Completed Apr. 1, 2014. # # Released Apr. 1, 2014. # # # Available here: # # http://astroblahhh.com/software/perl/make-welcome-to-disk-text-file-v1_0.txt # # # Works in Lucid Puppy Linux 5.2.8, but probably could be modified # to work even in Windows. # # The part that opens an RXVT window is the part most likely to be # incompatible with operating systems that aren't Puppy. # # And then there's the part that uses the Rox-Filer file manager # to open the folder containing the newly-created Welcome file. # # # This script just makes a text file named (and containing) # something like this: # # Welcome to Fluffyland! # # Where "Fluffyland" is whatever disk name you want. # # # I use this script to make text files to make it more apparent # at a glance which disk I just opened. # # In my Linux system, by default, my disks and TrueCrypt volumes # all get mounted at horribly vaguely-named locations like: # # /mnt/sdc1 # /mnt/truecrypt1/ # # So, these Welcome files are nice little signposts to tell you # where you are. # # # ----- # # # Except for parts not by Apollia: # # Copyright (C) 2014 Apollia # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program 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 Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see http://www.gnu.org/licenses/ . # # # Contact info: http://astroblahhh.com/contact-apollia.shtml ################################################################ # # Some stuff you should leave alone # # This gets the script's location. use Cwd 'abs_path'; use File::Basename qw( dirname ); $this_script_path=abs_path($0); $this_script_parentdirs=dirname( $this_script_path ); print "Running $this_script_path...\n\n\n"; $escaped_script_path=$this_script_path; $escaped_script_path=~s/ /\\ /g; # Makes it possible to use the mkpath function: use File::Path; ################ # # Constants for readability # use constant false => 0; use constant true => 1; # # End of Constants for readability # ################ # End of some stuff you should leave alone # ################################################################ ####################################################################### # # Some stuff you can change # # $full_path_to_folder_in_which_to_create_welcome_file = $this_script_parentdirs; $MAX_DISK_NAME_LENGTH=200; $file_manager_shell_command_to_use="rox"; $should_open_container_of_welcome_file_when_script_is_finished = false; # true|false # # If true, the script will open $full_path_to_folder_in_which_to_create_welcome_file # when finished. # End of stuff you can change. # # # Below this point, nothing else in this script needs to be changed. # # ####################################################################### ################ # # If the user must be asked for input (a prefix or suffix for # the folder name), then, here, we open an RXVT window and # re-run the script. $first_argv_arg=$ARGV[0]; if ( $first_argv_arg != 1 ) # If the RXVT window hasn't been opened yet, # then, the first parameter won't be 1. { exec ("rxvt -rv +sb -sl 999 -e perl $escaped_script_path 1"); # The exec command kills this script and runs the above shell command, # which re-runs this script in an RXVT window with one parameter - # just a 1 - to make this script get past this point during the re-run. } # # End of section where we open an RXVT window. # ################ $full_path_to_folder_in_which_to_create_welcome_file = AddSlashIfNeeded($full_path_to_folder_in_which_to_create_welcome_file); Create_Dest_Path_If_Necessary("full_path_to_folder_in_which_to_create_welcome_file"); sub Create_Dest_Path_If_Necessary { $var=$_[0]; if (!-e $$var) { print "Path doesn't exist yet - $$var \nNow attempting to create path\n\n $$var"; $succeeded = mkpath $$var; if ($succeeded) { print "\n\nPath created!"; } } } CheckForDestPathErrors("full_path_to_folder_in_which_to_create_welcome_file"); sub CheckForDestPathErrors { $var=$_[0]; if (!-d $$var) { print "\n\nError: "; print $$var; print " is not a directory!\n"; print "Sorry, the path provided in the $var variable doesn't lead to a directory. Please put a different path in $var, or make $$var into a directory, and run this script again."; End_Script_and_Wait_for_Return(); } } sub AddSlashIfNeeded { $this_string=$_[0]; $lastchar = substr($this_string,length($this_string)-1,1); if ($lastchar ne "/") { $this_string .= "/"; } return $this_string; } print "Please type a disk name for the Welcome file which is about to be created.\n\n> "; $disk_name = ; chomp $disk_name; $disk_name=~s/[^A-Za-z0-9., _-]/-/g; # Gets rid of bad characters, replacing them with a dash. $disk_name=substr($disk_name, 0, $MAX_DISK_NAME_LENGTH); print "\n\nDisk name: $disk_name \n\n\n"; $final_disk_name="Welcome to $disk_name" . "!"; $final_welcome_file_path=$full_path_to_folder_in_which_to_create_welcome_file . $final_disk_name; sub Create_Text_File_With_Contents_The_Same_As_File_Name { $path=$_[0]; $filename=$_[1]; if (!-e $$path) { print "Welcome file doesn't exist yet - $$path \n\nNow attempting to create welcome file\n\n $$path"; open (WELCOME_FILE, ">$$path"); print WELCOME_FILE "$filename"; close (WELCOME_FILE); if (-e $$path) { print "\n\n\nWelcome file created!\n\n"; } } else { print "Welcome file already exists! - $$path \n\nNot remaking.\n\n"; } } Create_Text_File_With_Contents_The_Same_As_File_Name("final_welcome_file_path", $final_disk_name); $final_welcome_file_path_to_display=$final_welcome_file_path; if (-e $final_welcome_file_path) { #chmod 0400, $final_welcome_file_path; # Owner:Read permissions only. if ($should_open_container_of_welcome_file_when_script_is_finished) { @system_args=($file_manager_shell_command_to_use, $full_path_to_folder_in_which_to_create_welcome_file); print "\n\n\nOpening folder containing welcome file, $full_path_to_folder_in_which_to_create_welcome_file...\n # @system_args\n\n"; system (@system_args); } } else # Couldn't find the folder at the path where it's now supposed to be. { print "The end of the script was reached, but no welcome file was found where it was supposed to be!\n\nTried to create $final_welcome_file_path but failed."; } print "\n\nScript finished!\n"; print "\nYou can press Return (or Enter) to close this window.\n"; $wait_for_return = ;