#!/usr/bin/perl -w

###########################################################################
# Program: 
#   EvPatch.pl
#
# Author:
#   Gary LaRocco  (mr_rock@cydathria.com)
#
# Date:
#   14-Jan-02
#
# Usage:  
#    EvPatch.pl -b bank# (1-3) -p patch# (1-128) -s scr_syx [-d dest_syx]
#
# Description
#   Changes the bank and patch numbers for DSI Evolver SysEx program dumps
#
###########################################################################

    use Getopt::Long;
 
# Process the command line

    GetOptions("bank=i",'patch=i','src=s','dest=s');

    if ((! $opt_bank) | (! $opt_patch) | (! $opt_src)) {
        print "Usage: $0 -b bank# (1-3) -p patch# (1-128) -s scr_syx [-d dest_syx]\n";
        exit;
    }

# If no destination file is specified, then make it the same as the source

    $opt_dest = $opt_src if (! $opt_dest);

# This is the Evolver sysex program dump header.

    $sysexhead = 'F0'  # System Exclusive (SysEx)
               . '01'  # DSI ID
               . '20'  # Evolver ID
               . '01'  # File Version
               . '02'; # Program Data

# Read in the sysex program dump

    open SYX, "<", $opt_src or die "Can't find file $SYX: $!\n";
    read SYX, $syxdata, 228 or die "Can't read from file $SYX: $!\n";
    close SYX;

# Make sure the dump is from the Evolver 

    die "$opt_src is not an Evolver SysEx program dump\n" if (uc(unpack "H10", $syxdata) ne $sysexhead);

# The bank and patch are stored as zero based values so decrement each by one

    $opt_bank--;
    $opt_patch--;

# Update the dump with the new bank and patch values

    substr($syxdata,5,1) = pack "C", $opt_bank;
    substr($syxdata,6,1) = pack "C", $opt_patch;

# Write out the modified dump

    open SYX, ">", $opt_dest or die "Can't open file $SYX: $!\n";
    binmode SYX;
    print SYX $syxdata;
    close SYX;

# We're done!

    exit;


