#! /usr/bin/perl
#
# Samba Multibyte convert tool
# copyright (c) 2000, Hiroshi MIURA 
# All rights reserved by Hiroshi MIURA
# 
# usage: smbutfconv [-l][-h][-p  </where/to/some/hardlink/path/>][-d] 
#            </where/to/conv> [ </the/other/to/conv> [ <another/to/conv> .... ]]
#
#   -p:  default path => same path with original.
#
require 5;
use Getopt::Std;
use File::stat;
use Jcode::Unicode;

$version=1.0;

getopts("dhlp:a:");
&Show_Usage($comn) if ($opt_h);

#---------------------------------------------
# check command name which it called.
# 
if ($opt_a) {
    $comn=$opt_a;
} else {
    $comn=$0;
    $comn=~s|(.*/)*(.+)|$2|;
}

## who am I? :)
## definition of mb convert 

if     ($comn eq "euctoutf8"){
    $convmb= sub {
	local(*nname) = @_;
	my $name=$nname;
        Jcode::euc_utf8(*nname);
    };

 }elsif ($comn eq "utf8toeuc"){
    $convmb= sub {
	local(*nname) = @_;
        my $name=$nname;
        Jcode::utf8_euc(*nname);
      };

 }else {
    &Show_Usage();
    exit 1;
}

#========================================================
# main routine.
#

umask 0000;

if ($opt_l || $opt_p) { # when want to link files
       &lnconv(@ARGV);
} else {                # when rename files
   while ($arg = shift @ARGV) {
       $arg =~ s/(\.|\.\.)\/+/\1/o;
       &fnconv($arg); 
   }
}
exit;


#===========================================================
# subroutines.
#
#-------------------------------------------
# link arguments with file name conversion.
#

sub lnconv() {

  open(FILEN, "find @_ -print |") or die "Cannot open directories\n"; 

  while (<FILEN>){
      chomp();
      $nname=$name=$_;
      &$convmb(*nname);
      $nname="$opt_p/$nname" if ($opt_p);
      
      if ( -d $name) {                
	  my $st = stat($name);
	  if ($opt_d) {
	      printf STDERR "mkdir %s %o\n", $nname, $st->mode & 07777;
	  } else {
	      if (mkdir $nname, $st->mode & 07777) {
		  utime $st->atime, $st->mtime, $nname;
	      } else {
		  print STDERR $!;
	      }
	  }
      } else {
	  if ($opt_d) {
	      print STDERR "convert $name to  $nname\n";
	  } else {
	      unless (link $name, $nname) {
		  print STDERR "fail link: operation not permitted.\n";
	      }
	  }
      }
  }
}


#--------------------------------------------------------
# rename argument's basename  with &$convmb
#

sub fnconv() {
    local($name)=@_; 

    if ( -d $name) {  
	$athash{"$name"}=stat($name)->atime;
	$mthash{"$name"}=stat($name)->mtime;
	local($dh)="DH_$name";
	opendir $dh, $name;
	while ($dentory=readdir $dh) {
	    next if ($dentory eq "." || $dentory eq "..");  # ignore "." and ".."
	    &fnconv("$name/$dentory");            # recursive call
	}
	closedir $dh;
	$nname = &renfn($name);
	if ($opt_d) {
	    printf "utime %d, %d, %s\n", $athash{"$name"},$mthash{"$name"},$nname unless (($name eq ".") || ($name eq ".."));
	} else {
	    utime $athash{"$name"}, $mthash{"$name"}, $nname unless (($name eq ".") || ($name eq ".."));
	}
	delete $mthash{"$name"};
	delete $athash{"$name"};

    } else {                # if name is file then only rename it.
	&renfn($name);
    }
}

#--------------------------------------------------------
# rename argument's basename  with &$convmb
#
#
sub renfn() {
    local($name)=@_;
    local($basedir, $nname)=("", $name);

    if ($name =~ m|(.*/)([^/]*)|o) {    # split dir and fname
	$basedir="$1";
	$nname  ="$2";
    } 

    &$convmb(*nname);
    $nname=$basedir . $nname;	

    if ($opt_d) {  
	print STDERR "rename $name to  $nname\n";
    } else {       
	unless (rename $name, $nname) {
	    print STDERR "fail rename: operation not permitted.\n";
	}
    }
    $nname;
}

#---------------------------------------------------------

sub Show_Usage {
    printf "This is samba companion tool. smbchartool Ver.%d\n",$version;
    print <<__EOL__;
This product is distoributed under the GNU Public License Vertion 2.

Usage: command [-h][-d][-l][-a <action>] [-p <linkpath>] <convpath>
  -h: show this help message
  -d: debug option. Don't actual move and removing, only display how to do
  -l: make hard link, not move it
  -p: make link to another directory
     (it may be use with -l)
  -a: point out an action

Available command names or actions are....
 euctoutf8, utf8toeuc

Copyright (c) 2000, Hiroshi MIURA <miura\@samba.gr.jp>
__EOL__
exit;
}
