"Fossies" - the Fresh Open Source Software Archive

Member "namefix.pl/namefix-cli.pl" (16 Sep 2009, 13511 Bytes) of package /linux/privat/old/namefix.pl_4.0.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "namefix-cli.pl" see the Fossies "Dox" file reference documentation.

    1 #!/usr/bin/perl -w
    2 
    3 use strict;
    4 no warnings;
    5 
    6 use English;
    7 use Cwd;
    8 use MP3::Tag;
    9 use File::Find;
   10 use File::Basename qw(&basename &dirname);
   11 
   12 use File::Copy;
   13 
   14 # $0 = location of scipt either full or relative, usefull to determine scripts location
   15 our $prog_dir = $0;
   16 if(-l $prog_dir)    # if its a link find real file
   17 {
   18     $prog_dir = readlink($prog_dir);
   19 }
   20 $prog_dir =~ s/\\/\//g;
   21 # remove script name from $dir and then we "should" have a nice path to our scripts
   22 $prog_dir =~ s/^(.*)\/(.*?)$/$1/;
   23 
   24 # mems libs
   25 require "$prog_dir/libs/fixname.pm";
   26 require "$prog_dir/libs/run_namefix.pm";
   27 require "$prog_dir/libs/misc.pm";
   28 require "$prog_dir/libs/config.pm";
   29 require "$prog_dir/libs/global_variables.pm";
   30 require "$prog_dir/libs/nf_print.pm";
   31 
   32 require "$prog_dir/libs/dir.pm";
   33 require "$prog_dir/libs/mp3.pm";
   34 require "$prog_dir/libs/filter.pm";
   35 require "$prog_dir/libs/undo.pm";
   36 require "$prog_dir/libs/html.pm";
   37 
   38 require "$prog_dir/libs/cli/help.pm";
   39 require "$prog_dir/libs/cli/print.pm";
   40 
   41 #--------------------------------------------------------------------------------------------------------------
   42 # load config file if it exists
   43 #--------------------------------------------------------------------------------------------------------------
   44 
   45 
   46 if(-f $main::config_file) 
   47 {
   48     do $main::config_file;  # executes config file
   49 }
   50 
   51 if($main::ZERO_LOG)
   52 {
   53     &clog;
   54 }
   55 
   56 &plog(1, "**** namefix.pl $main::version start *************************************************");
   57 &plog(4, "main: \$prog_dir = \"$prog_dir\"");
   58 
   59 $main::CLI = 1; # set cli mode flag
   60 
   61 #--------------------------------------------------------------------------------------------------------------
   62 # CLI Variables
   63 #--------------------------------------------------------------------------------------------------------------
   64 
   65 my @tmp = ();
   66 my $text = "";
   67 
   68 #-------------------------------------------------------------------------------------------------------------
   69 # 1st run check
   70 #-------------------------------------------------------------------------------------------------------------
   71 
   72 if(!-f $main::config_file)
   73 {
   74     &cli_print("No config file found, Creating.", "<MSG>");
   75     &save_config;
   76 }
   77 
   78 if(!-f $main::casing_file)
   79 {
   80     &cli_print("No Special Word Casing file found, Creating.", "<MSG>");
   81     &save_file($main::casing_file, join("\n", @main::word_casing_arr));
   82 }
   83 
   84 if(!-f $main::killwords_file)
   85 {
   86     &cli_print("No Kill Words file found, Creating.", "<MSG>");
   87     &save_file($main::killwords_file, join("\n", @main::kill_words_arr));
   88 }
   89 
   90 if(!-f $main::killpat_file)
   91 {
   92     &cli_print("No Kill Patterns file found, Creating.", "<MSG>");
   93     &save_file($main::killpat_file, join("\n", @main::kill_patterns_arr));
   94 }
   95 
   96 #-------------------------------------------------------------------------------------------------------------
   97 # Startup options
   98 #-------------------------------------------------------------------------------------------------------------
   99 
  100 $main::dir = $ARGV[$#ARGV];
  101 
  102 if(-d $main::dir)
  103 {
  104     chdir $main::dir;
  105     $main::dir = cwd();
  106     
  107     if(!-d $main::dir)
  108     {
  109         plog(0, "main: $main::dir is not a directory, cowardly refusing to process it");
  110         exit 0;
  111     }
  112     else
  113     {
  114         pop @ARGV;
  115     }
  116 }
  117 else
  118 {
  119     $main::dir = cwd;
  120 }
  121 
  122 #--------------------------------------------------------------------------------------------------------------
  123 # Parse Options
  124 #--------------------------------------------------------------------------------------------------------------
  125 
  126 $main::advance = 1; # since general cleanup is an option, enable advanced mode by default
  127             # and have the general cleanup option  turn it off.
  128 
  129 $main::ERROR_STDOUT = 1;
  130 
  131 for(@ARGV)
  132 {
  133     # found a short option, process it
  134     if($_ !~ /^--/ && $_ =~ /^-/ )
  135     {
  136         &proc_short_opts($_);
  137         next;
  138     }
  139 
  140     if($_ eq "--help")
  141     {
  142         &cli_help("help");
  143         exit 0;
  144     }
  145     if($_ eq "--help-short")
  146     {
  147         &cli_help("short");
  148         exit 0;
  149     }
  150     if($_ eq "--help-long")
  151     {
  152         &cli_help("log");
  153         exit 0;
  154     }
  155     if($_ eq "--help-misc")
  156     {
  157         &cli_help("misc");
  158         exit 0;
  159     }
  160     if($_ eq "--help-adv")
  161     {
  162         &cli_help("adv");
  163         exit 0;
  164     }
  165     elsif($_ eq "--help-mp3")
  166     {
  167         &cli_help("mp3");
  168         exit 0;
  169     }
  170     elsif($_ eq "--help-trunc")
  171     {
  172         &cli_help("trunc");
  173         exit 0;
  174     }
  175     elsif($_ eq "--help-enum")
  176     {
  177         &cli_help("enum");
  178         exit 0;
  179     }
  180     elsif($_ eq "--help-doc")
  181     {
  182         &cli_help("doc");
  183         exit 0;
  184     }
  185     elsif($_ eq "--help-debug")
  186     {
  187         &cli_help("debug");
  188         exit 0;
  189     }
  190     elsif($_ eq "--help-hacks")
  191     {
  192         &cli_help("hacks");
  193         exit 0;
  194     }
  195 
  196     elsif($_ eq "--help-all")
  197     {
  198         &cli_help("all");
  199         exit 0;
  200     }
  201 
  202     #####################
  203     # Main Options
  204     #####################
  205 
  206     elsif($_ eq "--cleanup" || $_ eq "--clean" )
  207     {
  208         $main::advance = 0;
  209     }
  210     elsif($_ eq "--rename" || $_ eq "--ren")
  211     {
  212         $main::testmode = 0;
  213     }
  214     elsif($_ eq "--case")
  215     {
  216         $main::case = 1;
  217     }
  218     elsif($_ eq "--spaces")
  219     {
  220         $main::spaces = 1;
  221     }
  222     elsif($_ eq "--dots")
  223     {
  224         $main::dot2space = 1;
  225     }
  226     elsif($_ eq "--regexp")
  227     {
  228          $main::disable_regexp = 0;
  229     }
  230     elsif(/---remove=(.*)/ || /--rm=(.*)/)
  231     {
  232         $main::replace = 1;
  233         $main::rpwold = $1;
  234     }
  235     elsif(/--replace=(.*)/ | /--rp=(.*)/)
  236     {
  237         if(!$main::replace)
  238         {
  239             plog(0, "main: option replace present but remove option not");
  240             exit;
  241         }
  242         $main::rpwnew = $1;
  243     }
  244     elsif(/--append-front=(.*)/ || /--af=(.*)/ )
  245     {
  246         $main::front_a = 1;
  247         $main::faw = $1;
  248     }
  249     elsif(/--end-front=(.*)/ || /--ea=(.*)/ )
  250     {
  251         $main::end_a = 1;
  252         $main::eaw = $1;
  253     }
  254     elsif($_ eq "--rm-words")
  255     {
  256         $main::kill_cwords = 1;
  257     }
  258 
  259     elsif($_ eq "--rm-pat")
  260     {
  261         $main::kill_sp_patterns = 1;
  262     }
  263     elsif($_ eq "--case-sp")
  264     {
  265         $main::sp_word = 1;
  266     }
  267     elsif($_ eq "--fs-fix")
  268     {
  269         $main::fat32fix = 1;
  270     }
  271 
  272     #####################
  273     # Advanced Options
  274     #####################
  275 
  276     elsif($_ eq "--undo")
  277     {
  278         $main::UNDO = 1;
  279     }
  280 
  281     elsif($_ eq "--recr")
  282     {
  283         $main::recr = 1;
  284     }
  285     elsif($_ eq "--dir")
  286     {
  287         $main::proc_dirs = 1;
  288     }
  289 
  290     elsif($_ eq "--overwrite")
  291     {
  292         $main::overwrite = 1;
  293     }
  294     elsif($_ eq "--all-files")
  295     {
  296         $main::ig_type = 1;
  297     }
  298     elsif(/--filt=(.*)/)
  299     {
  300         $main::filter_string = $1;
  301     }
  302     elsif($_ eq "--filt-regexp")
  303     {
  304         $main::filter_use_re = 1;
  305     }
  306     elsif(/--space-char=(.*)/ || /--spc=(.*)/)
  307     {
  308         $main::space_character = $1;
  309     }
  310     elsif(/--media-types=(.*)/ || /--mt=(.*)/)
  311     {
  312         $main::file_ext_2_proc = $1;
  313     }
  314 
  315     #######################
  316     # Truncate options
  317     ######################
  318 
  319     elsif(/-trunc=(.*)/)
  320     {
  321         $main::truncate = 1;
  322         $main::truncate_to = $1;
  323 
  324     }
  325     elsif(/--trunc-pat=(.*)/)
  326     {
  327         $main::truncate_style = $1;
  328     }
  329     elsif(/--trunc-ins=(.*)/)
  330     {
  331         $main::trunc_char = $1;
  332     }
  333 
  334     ########################
  335     # Enumerate Options
  336     ########################
  337     
  338     elsif($_ eq "--enum")
  339     {
  340         $main::enum = 1;
  341     }
  342     elsif(/--enum-style=(.*)/)
  343     {
  344         $main::enum_opt = $1;
  345     }
  346     elsif(/--enum-zero-pad=(.*)/)
  347     {
  348         $main::enum_pad = 1;
  349         $main::enum_pad_zeros = $1;
  350     }
  351 
  352     #####################
  353     # Misc Options
  354     #####################
  355 
  356     elsif($_ eq "--int")
  357     {
  358         $main::intr_char = 1;
  359     }
  360 
  361     elsif($_ eq "--scene" || $_ eq "--sc")
  362     {
  363         $main::scene = 1;
  364     }
  365 
  366     elsif($_ eq "--unscene" || $_ eq "--usc")
  367     {
  368         $main::unscene = 1;
  369     }
  370 
  371     elsif($_ eq "--uc-all" || $_ eq "--uc")
  372     {
  373         $main::uc_all = 1;
  374     }
  375 
  376     elsif($_ eq "--lc-all" || $_ eq "--lc")
  377     {
  378         $main::lc_all = 1;
  379     }
  380 
  381     elsif($_ eq "--rm-nc" || $_ eq "--rmc")
  382     {
  383         $main::sp_char = 1;
  384     }
  385     elsif($_ eq "--rm-starting-digits" || $_ eq "--rsd")
  386     {
  387         $main::digits = 1;
  388     }
  389     elsif($_ eq "--rm-all-digits" || $_ eq "--rad")
  390     {
  391         $main::rm_digits = 1;
  392     }
  393     elsif($_ eq "--pad-hyphen" || $_ eq "--ph")
  394     {
  395         $main::pad_dash = 1;
  396     }
  397     elsif($_ eq "--pad-num" || $_ eq "--pn")
  398     {
  399         $main::pad_digits = 1;
  400     }
  401     elsif($_ eq "--pad-num-w0" || $_ eq "--p0")
  402     {
  403         $main::pad_digits_w_zero = 1;
  404     }
  405     elsif($_ eq "--pad-nnnn-wx" || $_ eq "--px")
  406     {
  407         $main::split_dddd = 1;
  408     }
  409 
  410     #####################
  411     # Hacks Options
  412     #####################
  413 
  414     elsif($_ eq "--html")
  415     {
  416         $main::HTML_HACK = 1;
  417     }
  418     elsif(/--browser=(.*)/)
  419     {
  420         $main::browser = $1;
  421     }
  422 
  423     #####################
  424     # MP3 Options
  425     #####################
  426 
  427     elsif($_ eq "--id3-guess")
  428     {
  429         $main::id3_mode = 1;
  430         $main::id3_guess_tag = 1;
  431     }
  432     elsif($_ eq "--id3-overwrite")
  433     {
  434         $main::id3_mode = 1;
  435         $main::id3_force_guess_tag = 1;
  436     }
  437     elsif($_ eq "--id3-rm-v1")
  438     {
  439         $main::id3_mode = 1;
  440         $main::id3v1_rm = 1;
  441     }
  442     elsif($_ eq "--id3-rm-v2")
  443     {
  444         $main::id3_mode = 1;
  445         $main::id3v2_rm = 1;
  446     }
  447     elsif(/--id3-art=(.*)/)
  448     {
  449         $main::id3_mode = 1;
  450         $main::id3_art_set = 1;
  451         $main::id3_art_str = $1;
  452     }
  453     elsif(/--id3-tit=(.*)/)
  454     {
  455         $main::id3_mode = 1;
  456         $main:: = $1;
  457     }
  458     elsif(/--id3-tra=(.*)/)
  459     {
  460         $main::id3_mode = 1;
  461         $main:: = $1;
  462     }
  463     elsif(/--id3-alb=(.*)/)
  464     {
  465         $main::id3_mode = 1;
  466         $main::id3_alb_set = 1;
  467         $main::id3_alb_str = $1;
  468     }
  469     elsif(/--id3-gen=(.*)/)
  470     {
  471         $main::id3_mode = 1;
  472         $main::id3_gen_set = 1;
  473         $main::id3_gen_str = $1;
  474     }
  475     elsif(/--id3-yer=(.*)/)
  476     {
  477         $main::id3_mode = 1;
  478         $main::id3_year_set = 1;
  479         $main::id3_year_str = $1;
  480     }
  481     elsif(/--id3-com=(.*)/)
  482     {
  483         $main::id3_mode = 1;
  484         $main::id3_com_set = 1;
  485         $main::id3_com_str = $1;
  486     }
  487 
  488     ##########################
  489 
  490     elsif(/--debug=(.*)/)
  491     {
  492         $main::debug = $1;
  493     }
  494     elsif($_ eq "--debug-stdout")
  495     {
  496         $main::LOG_STDOUT = 1;
  497     }
  498 
  499     #############################
  500     # Document options
  501     #############################
  502 
  503 
  504     elsif($_ eq "--changelog")
  505     {
  506         $text = join("", &readf($main::changelog));
  507         print "$text\n\n";
  508         exit 1;
  509     }
  510     elsif($_ eq "--about")
  511     {
  512         $text = join("", &readf($main::about));
  513         print "$text\n\n";
  514         exit 1;
  515     }
  516     elsif($_ eq "--todo")
  517     {
  518         $text = join("", &readf($main::todo));
  519         print "$text\n\n";
  520         exit 1;
  521     }
  522     elsif($_ eq "--thanks")
  523     {
  524         $text = join("", &readf($main::thanks));
  525         print "$text\n\n";
  526         exit 1;
  527     }
  528     elsif($_ eq "--links")
  529     {
  530         $text = join("", &readf($main::links));
  531         print "$text\n\n";
  532         exit 1;
  533     }
  534     elsif(/--editor=(.*)/)
  535     {
  536         $main::editor = $1;
  537     }
  538     elsif($_ eq "--ed-config")
  539     {
  540         system("$main::editor $main::config_file");
  541         exit 1;
  542     }
  543 
  544     elsif($_ eq "--ed-spcase")
  545     {
  546         system("$main::editor $main::casing_file");
  547         exit 1;
  548     }
  549     elsif($_ eq "--ed-rmwords")
  550     {
  551         system("$main::editor $main::killwords_file");
  552         exit 1;
  553     }
  554     elsif($_ eq "--ed-rmpat")
  555     {
  556         system("$main::editor $main::killpat_file");
  557         exit 1;
  558     }
  559     elsif($_ eq "--show-log")
  560     {
  561         $text = join("", &readf($main::log_file));
  562         print "$text\n\n";
  563         exit 1;
  564     }
  565 
  566     #############################
  567     # Save config options
  568     #############################
  569 
  570     elsif($_ eq "--save-options" || $_ eq "--save-opt" || $_ eq "--save-config")
  571     {
  572         &save_config;
  573         &cli_print("Options Saved, exiting", "<MSG>");
  574         exit 1;
  575     }
  576     else
  577     {
  578         &plog(0, "main: unkown long option \"$_\", cowardly refusing to run.");
  579         exit 0;
  580     }
  581 }
  582 
  583 
  584 #--------------------------------------------------------------------------------------------------------------
  585 # Main
  586 #--------------------------------------------------------------------------------------------------------------
  587 
  588 if(!$main::testmode && !$main::UNDO)
  589 {
  590     &clear_undo;
  591     $main::undo_dir = $main::dir;
  592     &save_file($main::undo_dir_file, $main::dir);
  593 }
  594 
  595 # set main dir, run fixname.....
  596 print "*** Processing dir: $main::dir\n";
  597 
  598 &save_file($main::html_file, " ");  # clear html file
  599 
  600 &html("<table border=1>");
  601 &html("<TR><TD colspan=2><b>Before</b></TD><TD colspan=2><b>After</b></TD></TR>");
  602 
  603 
  604 if($main::UNDO)
  605 {
  606     @main::undo_pre = &readf($main::undo_pre_file);
  607     @main::undo_cur = &readf($main::undo_cur_file);
  608     @tmp = &readf($main::undo_dir_file);
  609     $main::undo_dir = $tmp[0];
  610     &undo_rename;
  611 }
  612 else
  613 {
  614     &run_namefix;
  615 }
  616 
  617 &html("</table>");
  618 
  619 if($main::HTML_HACK)
  620 {
  621     system("$main::browser $main::html_file");
  622 }
  623 
  624 #--------------------------------------------------------------------------------------------------------------
  625 # End
  626 #--------------------------------------------------------------------------------------------------------------
  627 
  628 sub proc_short_opts
  629 {
  630     my $string = shift;
  631     my @tmp = split(undef, $string);
  632 
  633     for(@tmp)
  634     {
  635         #print "tmp = \"$_\"\n";
  636         if(/h/)
  637         {
  638             &cli_help("short");
  639         }
  640         elsif($_ eq "-") { next; }
  641         elsif($_ eq "!") { $main::testmode = 0; }
  642 
  643         elsif($_ eq "c") { $main::case = 1; }
  644         elsif($_ eq "g") { $main::advance = 0; }
  645         elsif($_ eq "o") { $main::dot2space = 1; }
  646         elsif($_ eq "p") { $main::spaces = 1; }
  647         elsif($_ eq "s") { $main::scene = 1; }
  648         elsif($_ eq "u") { $main::unscene = 1; }
  649         elsif($_ eq "x") { $main::disable_regexp = 0; }
  650 
  651         elsif($_ eq "0") { $main::pad_digits_w_zero = 1; }
  652         elsif($_ eq "A") { $main::ig_type = 1; }
  653         elsif($_ eq "C") { $main::sp_word = 1; }
  654         elsif($_ eq "D") { $main::proc_dirs = 1; }
  655         elsif($_ eq "F") { $main::fat32fix = 1; }
  656         elsif($_ eq "H") { $main::pad_dash = 1; }
  657         elsif($_ eq "K") { $main::kill_cwords = 1; }
  658         elsif($_ eq "L") { $main::lc_all = 1; }
  659         elsif($_ eq "N") { $main::pad_digits = 1; }
  660         elsif($_ eq "P") { $main::kill_sp_patterns = 1; }
  661         elsif($_ eq "U") { $main::uc_all = 1; }
  662 
  663         else
  664         {
  665             &plog(0, "main: unkown short option \"$_\", cowardly refusing to run.");
  666             exit 0;
  667         }
  668     }
  669 }   
  670