/*************************************************************************
 *  DFAtoWTel.rexx                             by Henning Hucke          *
 *                                                                       *
 *                                                                       *
 *  Convert the DFA-Database into a WilhelmTel-Telephonebook             *
 *                                                                       *
 *  SYNTAX                                                               *
 *      DFAtoWTel FILE/A                                                 *
 *                                                                       *
 *  FUNCTION                                                             *
 *      This script checks all entries of the current DFA data file and  *
 *      writes all entries which contain phonenumbers into the given     *
 *      file conforming the format which is used by the telephonesoft    *
 *      WilhelmTel shiped with the ISDN-Master card of bsc.              *
 *                                                                       *
 *  REQUIREMENTS                                                         *
 *      The only 'requirement' is, that you _must_ mirror the groupnames *
 *      into this script if you change them within DFA.                  *
 *                                                                       *
 *  CONFIGURATION                                                        *
 *      To configure this script to your taste, edit the initialization  *
 *      of the two variables                                             *
 *          GROUPNAME.x - where 1 <= x <= 8. The groupnames you use.     *
 *************************************************************************/

/* Presets */

RC          = 0
NUMOFGROUPS = 8
NULLCHARS  = '1234567890_.()[]{}/*-+'

/*************************************************************************
**  main
*/
if ~show(ports, DFA) then
do
    say "Start DFA first."
    exit 0
end

options FAILAT 100
options RESULTS

PARSE ARG 'FILE 'TelFile

/* Open File for write */

if ~open(TelBook, TelFile, W) then do
  say '***ERROR: Cant open' TelFile 'for write access!'
  exit 20
end

/* Get the names of the groups */

counter = 1
do while (RC = 0) & (counter <= NUMOFGROUPS)
  address "DFA" getprefs 'GROUPNAME'||counter
  GROUPNAME.counter = strip(translate(Result,'',NULLCHARS))
  counter = counter + 1
end

/* Write out header and group information */

call writeln(TelBook, '##')
call writeln(TelBook, '##  WilhelmTel PhoneBook (Generated from DFA-Database by DFAtoWTel)')
call writeln(TelBook, '##  Last Updated on' translate(date(E),'-','/')',' time())
call writeln(TelBook, '##')
call writeln(TelBook, '')
call writeln(TelBook, '')
call writeln(TelBook, '##')
call writeln(TelBook, '##  Phonebook Groups')
call writeln(TelBook, '##')
call writeln(TelBook, '')

counter = 1
do while GROUPNAME.counter ~= 'GROUPNAME.'||counter
  call writeln(TelBook, ' GD="'||GROUPNAME.counter||'"')
  counter = counter + 1
end

call writeln(TelBook, '')
call writeln(TelBook, '')
call writeln(TelBook, '##')
call writeln(TelBook, '##  PhoneBook Entries')
call writeln(TelBook, '##')
call writeln(TelBook, '')

/* Sort entries by name and firstname */

address "DFA" sort sort1 NAME sort2 FIRST

/* Disable listview refresh of DFAEditor */

address "DFA" gui output off input off

address "DFA" first stem p.

do while RC = 0
    if p.address.10 ~= '' then do
	call writech(TelBook, 'N="'||strip(p.address.1 p.address.2)||'"')
	call writech(TelBook, ' A="'||p.address.4'"')
	call writech(TelBook, ' C="'||strip(p.address.5 p.address.6)||'"')
	call writech(TelBook, ' P="'||p.address.10'"')
	call writech(TelBook, ' G='||p.address.16*2 + p.address.17*4 + p.address.18*8 + p.address.19*16 + p.address.20*32 + p.address.21*64 + p.address.22*128 + p.address.23*256)
	call writech(TelBook, ' F=0')
	call writeln(TelBook, ' COM="'||p.address.15||'"')
    end

    address "DFA" next stem p.
end

/* Enable listview refresh of DFAEditor */

address "DFA" first

address "DFA" gui output on input on

call close(TelBook)

exit 0
