#!/bin/perl
#
#  makerc for madoka 4.1
#      Copyright(c)1998- cookie / The madoka project
#      This Software is under GNU Public Licence ver.2

print "makerc for madoka 4.1 (c)1998- cookie\n\n";

print 'Input .pircrc location[./.pircrc]:';
chop($pircrc = <STDIN>);
$pircrc = './.pircrc' if $pircrc !~ /\S/;
until (-f $pircrc && -r $pircrc) {
  print "Cannot find (or read): $pircrc\n";
  print 'Input .pircrc location[./.pircrc]:';
  chop($pircrc = <STDIN>);
  $pircrc = './.pircrc' if $pircrc !~ /\S/;
}
unless (open(PIRCRC, $pircrc)) {
  die "Open fail: $pircrc\n";
}
@pircrc = <PIRCRC>;
close(PIRCRC);

print "Input HOMEDIR.[$ENV{HOME}]:";
chop($homedir = <STDIN>);
$homedir = $ENV{HOME} if $homedir !~ /\S/;

# .pircrc setting
foreach (@pircrc) {
  next unless /\S/;
  next if /^\#/;
  s/\s*\n$//;
  if (/^NICK\s+(.+)/) {
    $my_nick = $1;
    $no_away_nick = $my_nick;
  } elsif (/^NAME\s+(.+)/) {
    $my_name = $1;
  } elsif (/^PORT\s+(\d+)/) {
    $cport = $sport = $1;
  } elsif (/^SPORT\s+(\d+)/) {
    $sport = $1;
  } elsif (/^CPORT\s+(\d+)/) {
    $cport = $1;
  } elsif (/^PASSWD\s+(.+)/) {
    $passwd = $1;
  } elsif (/^AWAYMSG\s+(.+)/) {
    $away_message = $1;
    $my_away_message = $away_message;
  } elsif (/^AWAYNICK\s+(.+)/) {
    $away_nick = $1;
  } elsif (/^AJOIN\s+(on|off)/) {
    $auto_join = $1;
    print "not use: AJOIN\n";
  } elsif (/^AKICK\s+(on|off)/) {
    $auto_kick = $1;
    print "not use: AKICK\n";
  } elsif (/^AOPMODE\s+(on|off)/) {
    $auto_get = $1;
  } elsif (/^APRIV\s+(on|off)/) {
    $auto_priv = $1;
  } elsif (/^ARMODE\s+(on|off)/) {
    $auto_repair = $1;
  } elsif (/^ATOPIC\s+(on|off)/) {
    $auto_topic = $1;
  } elsif (/^DCCLOG\s+(on|off)/) {
    $dcc_logrec = $1;
  } elsif (/^DCCCLIENT\s+(on|off)/) {
    $dccclient = $1;
  } elsif (/^DCCDIR\s+(.+)/) {
    $dccdir = $1;
    $dccdir =~ s/^~\//$homedir\//;
  } elsif (/^DCCFILE\s+(.+)/) {
    $dcclogfile = $1;
    $dcclogfile =~ s/^~\//$homedir\//;
  } elsif (/^DCCMODE\s+(on|off)/) {
    $get_dcc = $1;
  } elsif (/^DOWNMES\s+(.*)/) {
    $down_mes = $1;
  } elsif (/^KICKPRIV\s+(on|off)/) {
    $kick_priv = $1;
    print "not use: KICKPRIV\n";
  } elsif (/^LOGDIR\s+(.+)/) {
    $logdir = $1;
    $logdir =~ s/^~\//$homedir\//;
    $logdir .= '/' unless $logdir =~ /\/$/;
  } elsif (/^LOGGZ\s+(.+)/) {
    $log_gzip = $1;
    print "not use: LOGGZ\n";
  } elsif (/^LOGMAIL\s+(.+)/) {
    $log_mail = $1;
    print "not use: LOGMAIL\n";
  } elsif (/^NICKMODE\s+(on|off)/) {
    $nick_mode = $1;
  } elsif (/^RMBAN\s+(\d+)/) {
    $b_count_def = $1;
  } elsif (/^TAILLOG\s+(\d+)/) {
    $taillog = $1;
  } elsif (/^TIMEFMT\s+(\d)/) {
    $timefmt = $1;
    $timefmt = 0 if $timefmt > 2;
  } elsif (/^TOPICLOG\s+(on|off)/) {
    $topic_logrec = $1;
  } elsif (/^TOPICFILE\s+(.+)/) {
    $topiclogfile = $1;
    $topiclogfile =~ s/^~\//$homedir\//;
  } elsif (/^USER\s+(.+)/) {
    $my_user = $1;
  } elsif (/^LOGMAILTO\s+(.+)/) {
    $log_mail_to .= "$1$;";
    print "not use: LOGMAILTO\n";
  } elsif (/^LOGMODE\s+(\d+)/) {
    $logmode = $1;
  } elsif (/^OPTION\s+(.+)/) {
    require $1;
  } elsif (/^USERINFO\s+(.+)/) {
    push(@userinfo, $1);
  } elsif (/^DOWN\s+(\d+)(T)?/) {
    local($dt) = $1;
    local($dt1) = $dt % 24;
    if ($2 eq 'T') {
      ($sec, $min, $hour, $mday, $mon, $year) = localtime(time);
      $mon++;
      $year += 1900;
      $dt = $dt - $hour;
      $dt += 24 if $dt1 <= $hour;
    }
    $down_time = ($dt == 0) ? -1 : $dt + 1;
  } elsif (/^MODECOUNT\s+(\d+)(r)?/) {
    $o_count_def = $1;
    $o_count_random = 1 if $2 eq 'r';
  } elsif (/^(SERVER|SV)\s+(.+)/) {
    local($serv) = $2;
    local($sp) = ($serv =~ /:(\d+)$/);
    $sp = $sport unless $sp;
    push(@sv, $serv);
    push(@sport, $sp);
  } elsif (/^(CHANNEL|NJCHANNEL)\s+(.+)/) {
    local($chan, $mode) = split(/\s+/, $2);
    $chanr = $chanv = $chan;
    if ($chan =~ /^\#.*:\*\.jp$/i) {
      $chan =~ s/^\#/%/;
      $chan =~ s/:\*\.jp$//i;
      $chanv = $chan;
    }
    if ($chan =~ /^%/) {
      $chan =~ s/^%/\#/;
      $chan .= ':*.jp';
      $chanr = $chan;
    }
    if ($chan eq 'PRIV') {
      $chan_n = 'P';
      $logprefix{'P'} = 'priv';
      $fh{'P'} = 'F1';
    } elsif ($chan eq 'DEBUG') {
      $logprefix{'D'} = 'dbg';
      $fh{'D'} = 'F2';
      $debug_mode = 1;
    } elsif (!&ExistList($autojoinchanlist, $chanr)) {
      $chan_n = $chanr;
      &AddList($autojoinchanlist, $chanr) if /^CHANNEL\s+/;
      &AddList($nojoinchanlist, $chanr) if /^NJCHANNEL\s+/;
      $fh{$chanr} = 'F' . $fhn++;
      $automode{$chanr} = $mode if $mode;
    }
  } elsif (/^PREFIX\s+(.+)/ && $chan_n && $no_log ne 'all' &&
	   !&ExistList($no_log, $chan_n)) {
    local($dt) = $1;
    $logdir  = './' unless $logdir;
    $logdir =~ s/^~\//$homedir\//;
    $logdir .= '/' unless $logdir =~ /\/$/;
    if ($dt =~ /\/$/) {
      local($dir, $prefix) = split(/\//, $dt);
      mkdir("$logdir$dir", oct('0755'));
      $logprefix{$chan_n} = "$logdir$dir/$prefix";
    } else {
      $logprefix{$chan_n} = $dt;
    }
    foreach (keys(%logprefix)) {
      $fh{$chan_n} = $fh{$_} if $logprefix{$_} eq $logprefix{$chan_n};
    }
  } elsif (/^KEY\s+(.+)/ && $chan_n) {
    $autokey{$chan_n} = $1;
  } elsif (/^TOPIC\s+(.+)/ && $chan_n) {
    $autotopic{$chan_n} = $1;
  } elsif (/^CHMAILTO\s+(.+)/ && $chan_n && $logprefix{$chan_n}) {
    $log_mail_to{$chan_n} .= "$1$;";
    print "not use: CHMAILTO\n";
  } elsif (/^NOLOG$/ && $no_log ne 'all') {
    if ($chan_n) {
      &AddList($no_log, $chan_n);
      delete $logprefix{$chan_n};
      delete $fh{$chan_n};
    } else {
      $no_log = 'all';
    }
  }
}

# madoka.rc setting
open(MDKRC, ">madoka.rc") || die "ERROR: cannot open madoka.rc\n";
print MDKRC "# madoka 4.1 mdkrc\n# made by support/makerc\n";

# [user]
print "setting [user] section";
print MDKRC "[user]\n";
print ".";
print MDKRC "nick=$my_nick\n" if $my_nick;
print ".";
print MDKRC "name=$my_name\n" if $my_name;
print ".";
if ($my_user) {
  print MDKRC "id=$my_user\n";
} else {
  $id = (getpwuid($<))[0];
  print MDKRC "id=$id\n";
}
print ".";
print MDKRC "password=$passwd\n" if $passwd;
foreach (@userinfo) {
  print ".";
  print MDKRC "info=$_\n";
}
print "\n";
print MDKRC "\n";
print "setting [channel] section";
print MDKRC "[channel]\n";
foreach (split(/$;/, $autojoinchanlist)) {
  next unless $_;
  print ".";
  print MDKRC "nolog=" if $no_log eq 'all';
  print ".";
  print MDKRC "join=$_\n";
  print ".";
  print MDKRC "mode=$automode{$_}\n" if $automode{$_};
  print ".";
  print MDKRC "logfile=$logprefix{$_}%M%D\n" if $logprefix{$_};
  print ".";
  print MDKRC "key=$autokey{$_}\n" if $autokey{$_};
  print ".";
  print MDKRC "topic=$autotopic{$_}\n" if $autotopic{$_};
  print ".";
  print MDKRC "nolog=" if &ExistList($no_log, $_);
}
print MDKRC "join=PRIV\n" if $logprefix{'P'};
print "\n";
print MDKRC "\n";
print "setting [server] section";
print MDKRC "[server]\n";
$n = 0;
foreach (@sv) {
  return unless $_;
  print ".";
  s/:\d+$//;
  print MDKRC "server=$_\n";
  print ".";
  print MDKRC "port=$sport[$n]\n" if $sport[$n];
  $n++;
}
print "\n";
print MDKRC "\n";
print "setting [message] section";
print MDKRC "[message]\n";
print ".";
print MDKRC "away=$away_message\n" if $away_message;
print ".";
print MDKRC "down=$down_mes\n" if $down_mes;
print "\n";
print MDKRC "\n";
print "setting [log] section";
print MDKRC "[log]\n";
print ".";
print MDKRC "dir=$logdir\n" if $logdir;
print ".";
print MDKRC "time=%h:%m:%s\n" if $timefmt == 0;
print MDKRC "time=%h%m%s\n" if $timefmt == 1;
print MDKRC "time=%h%m\n" if $timefmt == 2;
print ".";
print MDKRC "mode=$logmode\n" if $logmode;
print ".";
print MDKRC "topic=$topiclogfile\n" if $topic_logrec && $topiclogfile;
print "\n";
print MDKRC "\n";
print "setting [auto] section";
print MDKRC "[auto]\n";
print ".";
print MDKRC "mode=$auto_repair\n" if $auto_repair;
print ".";
print MDKRC "op=$auto_get\n" if $auto_get;
print ".";
print MDKRC "down=on\n" if $down_time;
print ".";
print MDKRC "topic=$auto_topic\n" if $auto_topic;
print ".";
print MDKRC "priv=$auto_priv\n" if $auto_priv;
print ".";
print MDKRC "dcc=$get_dcc\n" if $get_dcc;
print ".";
print MDKRC "opc=${o_count_def}R\n" if $o_count_def && $o_count_random;
print MDKRC "opc=$o_count_def\n" if $o_count_def && !$o_count_random;
print ".";
print MDKRC "downc=$down_time\n" if $down_time;
print "\n";
print MDKRC "\n";
print "setting [dcc] section";
print MDKRC "[dcc]\n";
print ".";
print MDKRC "dir=$dccdir\n" if $dccdir;
print ".";
print MDKRC "log=$dcclogfile\n" if $dcc_logrec && $dcc_logfile;
print ".";
print MDKRC "get=$get_dcc\n" if $get_dcc;
print ".";
print MDKRC "client=$dccclient\n" if $dccclient;
print "\n";
print MDKRC "\n";
print "setting [client] section";
print MDKRC "[client]\n";
print ".";
print MDKRC "port=$cport\n" if $cport;
print ".";
print MDKRC "tail=$taillog\n" if $taillog;
print ".";
print MDKRC "kanji=on\n";
print "\n";
print MDKRC "\n";
print "setting [plugin] section";
print MDKRC "[plugin]\n";
print "...";
print MDKRC "sub=server/default.mpi\n";
print MDKRC "sub=client/default.mpi\n";
print MDKRC "sub=ctcp/default.mpi\n";
print "\n";

close(MDKRC);
print "madoka.rc setting done\n";
exit;

sub InitList {
  $_[0] = "$;";
}
sub AddList {
  unless (&ExistList) {
    $_[0] .= "$_[1]$;";
    return 1;
  }
  return 0;
}
sub ExistList {
  local($tmp) = $_[1];
  $tmp =~ s/(\W)/\\$1/g;
  if ($_[0] =~ /$;($tmp)$;/i) {
    substr($_[0], index($_[0], "$;$1$;"), length("$;$1$;")) = "$;";
    $_[0] .= "$_[1]$;";
    return 1;
  }
  return 0;
}
