#!/usr/bin/perl
########################################################################
# File:           rootfiles/cfedit.pl                                  #
# myLinux Server: Copyright (c) 2004 Michael Oberg                     #
# Version:        0.98                                                 #
# Author:         Michael Oberg <michael.oberg@mylinuxproject.de>      #
#                                                                      #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or    #
# (at your option) any later version.                                  #
#                                                                      #
# This program is distributed in the hope that it will be useful,      #
# but WITHOUT ANY WARRANTY; without even the implied warranty of       #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         #
# GNU General Public License for more details.                         #
#                                                                      #
# You should have received a copy of the GNU Public License along      #
# with this package; if not, write to the Free Software Foundation,    #
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.       #
########################################################################

sub umlaut {
  my ($str) = @_;
  $str =~ s/Ä/Ae/g;
  $str =~ s/Ö/Oe/g;
  $str =~ s/Ü/Ue/g;
  $str =~ s/ä/ae/g;
  $str =~ s/ö/oe/g;
  $str =~ s/ü/ue/g;
  $str =~ s/ß/ss/g;
  $str =~ s/ /_/g;
  return $str;
}

$location_template =
  qq|hdrzaptel="loadzone=nl\\ndefaultzone=nl\\n"\n|
. qq|hdrzapata="[channels]\\nlanguage=de\\noverlapdial=no|
. qq|\\nechocancel=yes\\nimmediate=no\\npridialplan=local|
. qq|\\nprilocaldialplan=local\\nfaxdetect=both\\nhidecallerid=no|
. qq|\\nswitchtype=euroisdn\\n"\n|
. qq|hdrsip="[general]\\nport=5060\\nbindaddr = 0.0.0.0\\ncontext = sip|
. qq|\\nsrvlookup = yes\\nrecordhistory = yes\\nmaxexpirey = 3600|
. qq|\\ndefaultexpirey = 180\\ndisallow = all\\nallow = gsm|
. qq|\\nallow = ulaw\\nallow = alaw|
. qq|\\n;externip = ... in case Asterisk cannot detect this, because of NAT|
. qq|\\n;localnet = ... in case Asterisk cannot detect this, because of routing|
. qq|\\nuseragent = Asterisk PBX\\nnat = yes\\n"\n|
. qq|hdriax="[general]\\nport=5036\\nbindaddr=0.0.0.0\\ndelayreject=yes|
. qq|\\namaflags=billing\\naccountcode=iax\\nlanguage=de\\nbandwith=low|
. qq|\\ndisallow=all\\nallow=gsm\\nallow=alaw\\nallow=ulaw|
. qq|\\njitterbuffer=yes\\ntrunkfreq=6000\\nauthdebug=yes\\ntos=lowdelay\\n"\n|
. qq|hdrexten="[general]\\nstatic=yes\\nwriteprotect=yes\\n"\n|
. qq|iprefix=""\n|
. qq|lprefix=""\n|
. qq|hostname=""\n|
. qq|phones=""\n|
. qq|locprefix=""\n|
. qq|localcbc=""\n|
. qq|nationalcbc=""\n|
. qq|mobilecbc=""\n|
. qq|internationalcbc=""\n|
. qq|trust="no"\n|;

$location_struct
= qq|hzt	"Header zaptel.conf"	TXT:hdrzaptel\n|
. qq|hzp	"Heapder zapata.conf"	TXT:hdrzapata\n|
. qq|hs		"Header sip.conf"	TXT:hdrsip\n|
. qq|hi		"Header iax.conf"	TXT:hdriax\n|
. qq|he		"Header extensions.conf"	TXT:hdrexten\n|
. qq|ip		"International Prefix"	VAL:iprefix\n|
. qq|pr		"National Prefix"	VAL:lprefix\n|
. qq|hn		"Asterisk Hostname"	VAL:hostname\n|
. qq|ph		"List of Phone Numbers"	TXT:phones\n|
. qq|lp		"List of neighbor prefixes"	TXT:locprefix\n|
. qq|lc		"Call by Call number for local calls"	VAL:localcbc\n|
. qq|nc		"Call by Call number for national calls"	VAL:nationalcbc\n|
. qq|mc		"Call by Call number for mobile calls"	VAL:mobilecbc\n|
. qq|ic		"Call by Call number for international calls"	VAL:internationalcbc\n|
. qq|et		"Trust relationship"	KEY:trust	CHECK:"./trust.pl \$hostname \$trust"\n|;

#$LIST = `cat locationlist.conf`;
$LIST = $ARGV[0];
#$LIST =~ /locationlist[ \t]*=[ \t]*"([^"]*)"/;
#$LIST = $1;
$locno = 0;
open (MENU, "> location.struct");
print MENU "[Asterisk Configuration]\n";
foreach $loc (split /\\n/, $LIST) {
  $locname = &umlaut ($loc);
  if (! -e "$locname.conf") {
    open (FILE, "> $locname.conf");
    print FILE $location_template;
    close FILE;
  }
  open (FILE, "> $locname.struct");
  print FILE "[$loc]\n" . $location_struct;
  close FILE;

  print MENU qq|m$locno "Location $loc" RUN:"cfedit.pl $locname.struct $locname.conf"\n|;
  $locno++;
}
close MENU;
