#!/bin/perl ################################################################################ # sig_cookie.pl # # Purpose: Build a .signature file with a random fortune cookie in it. # # Written by: Kelly Cash on December 26, 2001 # # # # Required files: The fortune cookie file. Hardcoded location below. # # .signature file - Hardcoded location below. # # ~/.signature_top (signature block above the cookie) # # ~/.signature_bottom (guess what this is for) # # getopt.pl- Hardcoded location below; find it on your system # # # # Switches: -rXX - Specifies a specific record in the cookie file (optional) # # # # Args: None. # # # # Notes: This assumes the cookies are normal text. Lines that are longer # # than "$maxlen", and have NO spaces will cause a problem. # # (this shouldn't happen, so I won't code for it) # # # # The spacing/indenting of the cookie lines is based upon my # # preferences (oddly enough). # # # # If a record specified on the command line is outside the valid # # range (1 .. the last line in the file), a random cookie is used. # # # # This figures out the size of the cookie file so that you don't # # have to update this script after adding cookies. # # # # Serving Suggestion: Put it into .login, .profile, crontab, etc. # # # # Usage examples: sig_cookie.pl (uses random cookie) # # sig_cookie.pl -r302 (uses record 302) # # sig_cookie.pl -r-23 (uses random cookie: rec too small) # # sig_cookie.pl -r23092 (uses random cookie: rec too big) # # # # Used with: n/a # # # # Revision history: # # KAC 2001/12/26 Initial implementation # # KAC 2001/12/28 Added functionality for wrapping longer quotes # # KAC 2001/12/28 Added functionality for specifying a particular record # ################################################################################ require "/usr/local/lib/perl5/5.8.6/getopt.pl"; # --- Begin user customizable things $cookies = "/usr/local/bin/cookie.txt"; $sig = "/usr/home/kelly/.signature"; $sigt = "/usr/home/kelly/.signature_top"; $sigb = "/usr/home/kelly/.signature_bottom"; $maxlen = 62; # the max # of characters a cookie line should be # --- End of user-customizable things; start of main code &Getopt('r'); # -r switch allowed $file_len = `wc -l $cookies`; if ($opt_r != 0) { # The -r switch was supplied. check the record number. # printf "switch r is %d\n", $opt_r; if ($opt_r > 0 & $opt_r <= $file_len) { $rec = $opt_r; } else { # Rec falls outside valid lines. Calc random rec. $rec = int(rand($file_len)) + 1; } } else { # No record number requested. Calculate a random record. $rec = int(rand($file_len)) + 1; } $fortune = `head -$rec $cookies | tail -1`; system("cat $sigt > $sig"); open (SIG, ">>$sig") || die "Can't create signature file: $!\n"; while (length($fortune) > $maxlen) { # The cookie is longer than the signature block, Break it up. $fptr = $maxlen; while (substr($fortune,$fptr,1) ne " ") { # search backwards from the "end" for a space $fptr--; } printf SIG " %s\n", substr($fortune,0,$fptr); $fortune = substr($fortune,$fptr+1); } printf SIG " %s", $fortune; close(SIG); system("cat $sigb >> $sig");