#!/usr/bin/perl -w
# This script is intended to make fidogate areas & areas.bbs files
# from exported fastecho 1.46 (dos/os2/win*) configuration files. May work w/
# other fastecho versions, but tested only w/ version 1.46 . Also it will 
# create simplest sh script for creating inn conferences.
# Also you should look at areas_remake.pl - it may help in advancing your 
# fidogate areas.bbs
# (c) Olli (olli@digger.org.NOSPAMru,remove NOSPAM before mailing)
# greetz: i_L, Corvin, kati, vika
# Thanx : Peter V. Chernikoff (peter@digger.org.ru,remove NOSPAM before mailing)
# for help in moving my node to Linux. =)
# HOWTO:export FE configs,set correct paths & AKAs in vars, then run.
# You should CHECK RESULTS! I wrote this for MY config & didn't read dox on
# FE configs, thus even if this does work for me it CAN give you wrong results.
# Beware that "dead" passthrough areas (exported only to uplink) are never 
# placed by this script in fidogate areas or areas.bbs.
# Known BUGS: Not a bug - a future ;) - some areas may duplicate - if you 
# export them to diffrent links. Edit resulting areas & areas.bbs to satisfy
# your needs.
# Look at log file twice.=)
$mnt="/os2";
$news_user="news";
$ctlinnd_path="/usr/local/news/bin";
$replace_char="_"; # &$`'!  - must be converted for ctlinnd to work right way.
$sh_path="/bin/sh";
$fe_dir="$mnt/m/fe";
$dos_drive="C";
$local_export="\.998";
$local_export1="\.100";
$fe_areas="$fe_dir/areas.bbs";
$fgate_path="/tmp/fidogate";
$fg_areas="$fgate_path/areas";
$fg_a_areas="$fgate_path/local_areas";
$fg_areas_bbs="$fgate_path/areas.bbs";
$inn_ctld_sh="$fgate_path/inn_area_make.sh";
$inn_ctld_a_sh="$fgate_path/inn_loc_area_make.sh";
$log_file="$fgate_path/log_file";
$unlink_file="$fgate_path/unlink_it.txt";
local (%aka_hash,%zone_hash);
# set AKA dependenses
$aka_hash{"2:"}="2:5020/1234";
$aka_hash{"275:"}="275:5020/1234";
$aka_hash{"963:"}="963:1/1234";
$aka_hash{"777:"}="777:5020/1234";
$primary_aka="2:5020/1234";
# I was too lazy to rebuild $zone =)
$zone_hash{"2:"}="2";
$zone_hash{"275:"}="275";
$zone_hash{"963:"}="963";
$zone_hash{"777:"}="777";
### internal vars. DO NOT EDIT lines below.
$l_area="";
$links="";
$area="";
$zone="";
$aka_found=0;
local ( %p_links_hash=(),%links_hash=(),@loc_area_arr=(),@links_arr);

############### proggie goes:
system `mkdir $fgate_path 2>/dev/null`;
open( AREAS,   "<$fe_areas" ) || die "Can't read FE areas $fe_areas: $!.\n";
open( LOGF,    ">$log_file") || die "Can't write to $log_file: $!.\n";
open( FGBBS,   ">$fg_areas_bbs" ) || die "Can't write to $fg_areas_bbs: $!.\n"; 
open( FG_AR,   ">$fg_areas" ) || die "Can't write to $fg_areas: $!.\n";
open( FG_A_AR,   ">$fg_a_areas" ) || die "Can't write to $fg_a_areas: $!.\n";
open( INNC_SH, ">$inn_ctld_sh" ) || die "Can't write to $fg_areas_bbs: $!.\n";
open( INNC_A_SH, ">$inn_ctld_a_sh" ) || die "Can't write $inn_ctld_a_sh: $!.\n";
open( UNLINK,  ">$unlink_file" ) || die "Can't write to $unlink_file: $!.\n";
print "Log will be in $log_file.\n";
print "fidogate areas will be in $fg_areas\n";
print "fidogate areas.bbs will be in $fg_areas_bbs\n";
print "areas you should unsubscribe (dead ones) will be in $unlink_file\n";
print "you may use send_unsibscribe.pl to broadcast this file to all links.\n";
print "sh script for making local inn areas'll be in $inn_ctld_a_sh\n";
print "$inn_ctld_a_sh should be changed before use.\n";
print "Also this will create $fg_a_areas. In SOME cases you MAY want to\
edit it and append this file to $fg_areas\n";
print "sh script for making inn areas'll be in $inn_ctld_sh\n\n";
print "\t\t\t.USE WITH CARE.\n\n";
print FGBBS "!\n";
print INNC_SH "\#!$sh_path\n";
while(<AREAS>) 
{
next if (/^;/);
s/\r//g;
chomp;
if (/^P\s/)
 {/^P\s+\S+\s+/;
  $links=$';
  /^P\s+(\S+)\s+/;
  $area=$1;
  @links_arr=split(/ /, $links);
  $p_links_hash{$area}="@links_arr";
  next;
 } # end of work on passthrough 
if (/^\!/)  
 {/^\!\S+\s+/;
  print LOGF "Read:$_\n";
  $_=$';
  /^(\S+)/;
  $area=$1;
  if (/^(\S+)\s+/)
   {print LOGF "Area $area,links are:\n$'\n\n";
    $links=$';
    if (/$local_export/) 
     {print LOGF "LOCAL export for $area \n";
      push (@loc_area_arr,$area);
      next;
     }
    if (/$local_export1/)
     {print LOGF "LOCAL export for $area \n";
      push (@loc_area_arr,$area);
      next;
     }
    if (!($links)) 
     {print LOGF "$area seem 2 be local.Skipping.\n";
      push (@loc_area_arr,$area);
      next;
     }
    @links_arr=split(' ', $links);
    $links_hash{$area}="@links_arr";
    next;
   }
  print LOGF "\n$area is local. Skipping.\n";
  push (@loc_area_arr,$area);
  next;
 } # exported
if (/^\$/)
 {/^\$\S+\s+/;
  print LOGF "$_\n";
  $_=$';
  /^(\S+)/;
  $area=$1;
  if (/^(\S+)\s+/)
   {print LOGF "Area $area,links are:\n$'\n\n";
    $links=$';
    if (/$local_export/) 
     {print LOGF "LOCAL export for $area \n";
      push (@loc_area_arr,$area);
      next;
     }
    if (/$local_export1/)
     {print LOGF "LOCAL export for $area \n";
      push (@loc_area_arr,$area);
      next;
     }
    if (!($links))
    {print LOGF "$area seem 2 be local.Skipping.\n";
     push (@loc_area_arr,$area);
     next;
    }
   @links_arr=split(' ',$links);
   $links_hash{$area}="@links_arr";
   next;
   }
    print LOGF "\n$area is local. Skipping.\n";
    push (@loc_area_arr,$area);
    next;
 } # exported
if (/^$dos_drive/)
 {/^$dos_drive\S+\s+/;
  $_=$';
  print LOGF "\n$_\n";
  /^(\S+)/;
  $area=$1;
  if (/^(\S+)\s+/)
  {print LOGF "Area $area,links are:\n$'\n\n";
   $links=$';
   if (/$local_export/) 
    {print LOGF "LOCAL export for $area \n";
     push (@loc_area_arr,$area);
     next;
    }
   if (!($links))
    {print LOGF "$area seem 2 be local.Skipping.\n";
     push (@loc_area_arr,$area);
     next;
    }
  @links_arr=split(' ',$links);
  $links_hash{$area}="@links_arr";
  next;
  }
  print LOGF "\n$area is local. Skipping.\n";
  push (@loc_area_arr,$area);
  next;
 } # end of exported
} # EOF <AREAS>
foreach $area (keys %p_links_hash)
{print LOGF "For area $area links are $p_links_hash{$area}\n";
 $links=$p_links_hash{$area};
 if (!($links=~/ /)) 
  {print UNLINK "-$area\n";
   print LOGF "This is a dead area.Skipping.\n\n"}
  else
   {foreach $zone (keys %aka_hash)
    {$links=$p_links_hash{$area}; # Have 2 do,because next rewrites $links.
     if ($links=~/$zone/) 
     {print LOGF "For area $area you use AKA $aka_hash{$zone}\n";
      print FGBBS "X $area -z $zone_hash{$zone} -\# -a $aka_hash{$zone} $p_links_hash{$area}\n";
      $aka_found=1;
      next;
     }
    }
    unless ($aka_found) {print "Can't find AKA for $area"}
    print LOGF "\n";
    }
} # end of passthrough adding.
foreach $area (keys %links_hash)
{print LOGF "For area $area links are $links_hash{$area}\n";
 $links=$links_hash{$area};
 foreach $zone (keys %aka_hash)
 {$links=$links_hash{$area}; # Have 2 do,because next rewrites $links.
  if ($links=~/$zone/)
   {print LOGF "For area $area you use AKA $aka_hash{$zone}\n";
    print FGBBS "X $area -z $zone_hash{$zone} -a $aka_hash{$zone} $links_hash{$area}\n";
    $aka_found=1;
    next;
   }
 }				       
 unless ($aka_found) {print "Can't find AKA for $area"}
 print LOGF "\n";
} # end of exported adding.

foreach $area (keys %links_hash)
{foreach $zone (keys %aka_hash)
 {$links=$links_hash{$area}; # Have 2 do,because next rewrites $links.
  if ($links=~/$zone/)
   {$_=$area;
    tr/A-Z/a-z/;
    s/\$/$replace_char/g;
    s/\&/$replace_char/g;
    s/\`/$replace_char/g; #` idiotic perl syntax highliting in mcedit..
    s/\'/$replace_char/g;
    s/\!/$replace_char/g;
    $l_area=$_;
    print FG_AR "$area\t$l_area -a $aka_hash{$zone} -8\n";
    print INNC_SH "su $news_user -c \"$ctlinnd_path/ctlinnd newgroup $l_area\"\n";
    $aka_found=1;
    next;
   }
 }
 unless ($aka_found) {print "Can't find AKA for $area"}
} # end of areas writing.
foreach $area (@loc_area_arr)
{$_=$area;
 s/\$/$replace_char/g;
 s/\&/$replace_char/g;
 s/\`/$replace_char/g; #`
 s/\'/$replace_char/g;
 s/\!/$replace_char/g;
 tr/A-Z/a-z/;
 $l_area=$_;
 print FG_A_AR "$area\t$l_area -a $primary_aka -8\n";
 print INNC_A_SH "su $news_user -c \"$ctlinnd_path/ctlinnd newgroup $l_area\"\n";
 } # end of adding local areas to inn.
system `chmod +x $inn_ctld_sh 2>/dev/null`;
system `chmod +x $inn_ctld_a_sh 2>/dev/null`;
close(AREAS);
close(INNC_SH);
close(INNC_A_SH);
close(FG_AR);
close(FG_A_AR);
close(LOGF);
exit;
