#!/usr/bin/perl
# tk2tk5csv -- stdin filter to convert a tk2 CSV format to tk5 CSV format
# For users of http://parnass.org/'s tk5
# Copyright       : http://www.fsf.org/copyleft/gpl.html
# Author          : Dan Jacobson -- http://jidanni.org/comm/radio/icom_r5/
# Created On      : Fri Nov 24 11:37:16 2006
# Last Modified By: Dan Jacobson
# Last Modified On: Thu Nov 30 12:56:34 2006
# Update Count    : 7
# Status          : better than nothing

#Tk2:Bank,Ch,MHz,Mode,Step,Offset,Duplex,TSQL,CTCSS,             Skip,        Label
#Tk5:Mem,    MHz,Mode,Step,Offset,Duplex,TSQL,CTCSS,DCS,Polarity,Skip,Bank,Ch,Label
use strict;
use warnings;
while (<>) {
    if ( $. == 1 ) {
        print
"Mem,MHz,Mode,Step,Offset,Duplex,TSQL,CTCSS,DCS,Polarity,Skip,Bank,Ch,Label\n";
        next;
    }
    my (
        $Bank,   $Ch,   $MHz,   $Mode, $Step, $Offset,
        $Duplex, $TSQL, $CTCSS, $Skip, $Label
    ) = split ",";
    my ( $DCS, $Polarity ) = ( "023", "n" );    ##then adjust output by hand
##  $Offset =~ don't worry, let tk5 and radio handle this.
    $Duplex =~ s/^ $//;                         ##tidy up
    $TSQL   =~ s/tsql/t/;
    $Label =~
      s/".{1,6}/\U$&/;    ##then adjust by hand. Didn't \L any trailer though.
    my $Mem = $Bank * 100 + $Ch;
    $Bank = chr( ord("A") + $Bank - 1 );
    print join ",", $Mem, $MHz, $Mode, $Step, $Offset, $Duplex, $TSQL, $CTCSS,
      $DCS, $Polarity, $Skip, $Bank, $Ch, $Label;
}

## Alas, Parnass' tk2 and tk5 do not export/import everything to .CSV.
## This struck home when my R2 got stolen and I replaced it with a R5.
## E.g., All one can do is manually copy each PROG00-24 between the
## two programs item by item with the mouse. With a perl script I
## could produce a great TV channel list for the R5, but how then to
## import? What perhaps is needed is a --batch-mode with no GUI, where
## everything could be exported/imported via .CSV (and some items
## found in ~/.tk5rc).
## Perhaps I can use binary editing of .tr5 files as an interface...
