#!/usr/bin/perl # addir 1.10 2006-01-20 16:01:05-05 davidsen Exp # add a directory to a CD # set sizes to default or environment values # default are for a 2.4 Linux using ide-scsi or any USB CD/DVD device $read_device = ( defined $ENV{ADDIR_READ} ? $ENV{ADDIR_READ} : "/dev/scd0" ); $scsi_device = ( defined $ENV{ADDIR_WRITE} ? $ENV{ADDIR_WRITE} : "0,0,0" ); $fifosize = ( defined $ENV{ADDIR_FIFO} ? $ENV{ADDIR_FIFO} : 5 ); $CDrecord = ( defined $ENV{ADDIR_CDRECORD} ? $ENV{ADDIR_CDRECORD} : "cdrecord" ); sub justadd; sub addm; sub Usage; sub Usage2; sub do_args; sub add_arg; # option processing use Getopt::Std; getopts("ZMGR:S:sdvB:O:D:f:Eh") || Usage; print STDERR "CDR=${CDrecord}\n" if $opt_d; ### # process the options which might override defaults Usage2 if $opt_h; $read_device = $opt_R if $opt_R; $scsi_device = $opt_S if $opt_S; $fifosize = $opt_f if $opt_f; $Eject = ( $opt_E ? "-eject" : "" ); # build the command strings $fixedopts_M = "-q -dDRNL -pad"; $fixedopts_C .= " -v" if $opt_v; $fixedopts_C = "dev=${scsi_device} -waiti -multi ${Eject}"; $fixedopts_C .= " fs=${fifosize}m"; $fixedopts_C .= " ${opt_O}" if $opt_O; $fixedopts_C .= " driveropts=${opt_D}" if $opt_D; $UsingGraft = ( $opt_G || $opt_B ); if ($opt_Z && $opt_M) { print STDERR "Only one of -Z or -M may be used\n\n"; sleep(3); Usage; } elsif ($opt_M) { # add to existing session print "Getting size\n"; open(SZ, "${CDrecord} dev=${scsi_device} -msinfo|"); $sesinfo = ; print "Sesinfo: $sesinfo"; close SZ; chomp $sesinfo; die "Bad sesinfo" unless $sesinfo =~ m/\d+,\d+/; # build the command $cmd_mk = "mkisofs ${fixedopts_M} -M${read_device} -C ${sesinfo}"; $cmd_mk .= " -graft-points" if $UsingGraft; $cmd_mk .= do_args; $cmd_cd = "${CDrecord} ${fixedopts_C} -"; } elsif ($opt_Z) { $cmd_mk = "mkisofs ${fixedopts_M}"; $cmd_mk .= " -graft-points" if $UsingGraft; $cmd_mk .= do_args; $cmd_cd = "${CDrecord} ${fixedopts_C} -"; } else { print STDERR "\n\nOne of -Z or -M must be selected.\n\n"; sleep(3); Usage; } if ($opt_d || $opt_v) { print "Mcmd: $cmd_mk\n"; print "Ccmd: $cmd_cd\n"; sleep 10; } # okay, try it out! $FullCmd = "set -x; $cmd_mk | $cmd_cd"; if ($opt_s) { open(WC, "|$FullCmd") unless $opt_d; while (<>) { chomp; $dir = add_arg($_); if ($opt_d) { print "$dir\n"; } else { print WC "$dir\n"; } } close WC; } else { system($FullCmd) unless $opt_d; } exit 0; # the argument builder sub add_arg { my ($dir) = $_[0]; my ($arglist) = ""; if ($dir =~ m/[^\\]=/) { # force the "using graft points" on unless ($UsingGraft) { print STDERR ( "unescaped \"=\" in arguemnt, -G option not selected.\n" . "This won't work right, please fix\n\n" ); exit 1; } # use as is, don't generate a value $arglist = $dir; } elsif ($UsingGraft) { # strip off a leading "./" if present $dir =~ s#^\./##; # strip a trailing /, this bites people occasionally $dir =~ s#/$##; # escape quotes $dir =~ s/\"/\\\"/g; # new_dir is the new name we use on the CD $dir_new = $dir; if ($opt_B) { if ($dir =~ m#^/#) { $dir_new = $opt_B . $dir; } elsif ($dir eq ".") { $dir_new = $opt_B; } else { $dir_new = "$opt_B/$dir"; } } $arglist = "$dir_new=$dir"; } else { # convert escaped = to raw $dir =~ s/\\=/=/g; $arglist=$dir; } return $arglist; } sub do_args { my ($cmd) = ""; my ($dir); if ($opt_s) { # read from the stdin $cmd = " -path-list -"; } else { # use the args for $dir (@ARGV) { $dir = add_arg($dir); $cmd .= " \"${dir}\""; } } return $cmd; } # user info sub Usage { my ($Version) = '1.10'; print STDERR "addir v${Version} - add information to a multisession CD\n" . "\n" . "Usage\n" . " addir -[MZ] [ options ] data list\n" . "\n" . "save modes (one required)\n" . " -Z this is the first session on the CD\n" . " -M this is an added session to an existing CD\n" . "\n" . "Options\n" . " -G force graft-points to preserve names\n" . " -Bdir prepend \"dir\" to directory or file names being save\n" . " (note entries in the for \"name=name\" are unchanged)\n" . " -Rdev device for reading existing session information, def: /dev/cdrom\n" . " -Sdev SCSI device for burning, def: 0,0,0\n" . " the ATA: or ATAPI: names may be used here\n" . " -Oopt provide an option to cdrecord (-dummy is useful for testing)\n" . " -Dopt driveropt, passes \"driveropts=opt\" to cdrecord\n" . " -fNN set FIFO size to NN MB (def: 20)\n" . " -E Eject the media when burn is complete\n" . " -s read names from stdin. Usful for piping find to CD sessions\n" . " -v some additional verbose information\n" . " -h help with environment values\n" . "\n"; exit 1 } # environment variables sub Usage2 { print "addir - environment variables used\n" . "\n" . "If the following environment variables are defined, their values will be\n" . "used as noted. Don't forget to export the variables.\n" . "The default values work well for cdrecord using ide-scsi with Linux\n" . "\n" . "ADDIR_READ device to read for multisession info, def: /dev/scd0\n" . "\n" . "ADDIR_WRITE device to write, def: \"0,0,0\"\n" . "\n" . "ADDIR_FIFO the FIFO size in MB, def: 5\n" . "\n" . "ADDIR_CDRECORD full path to a version of cdrecord to use for CD operations.\n" . " some distribution versions may work with DVD as well.\n" . "\n" . "Use \"addir -?\" to get help for options\n" . "\n"; exit 1; }