#!/bin/sh
# 
# Copyright (C) 2004-2005 Stefan Westerfeld, stefan space twc 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 General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

#
# usage: timidity-cfg-drumkit-import <timidity_cfg_file> ...
#
#   this script imports drumkits specified in a timidity cfg file, and
#   creates a .bsewave file containing the drumkit
#
# example: timidity-cfg-drumkit-import /etc/timidity/timidity.cfg
#
#   will import the drumkits defined by /etc/timidity/timidity.cfg,
#   and create one output file per drumkit found, and write them
#   to files such as timidity_drumset_0.bsewave
#

for cfg_file in "$@"
do
  # read_cfg is implemented (recursively) to handle source directives
  # (inclusion of another cfg file) in a preprocessor like way
  echo cfg "$(basename $cfg_file | sed s/.cfg$//)"
  read_cfg()
  {
    cat "$(dirname $cfg_file)/$(basename $1)" | while read a b
    do
      if [ "$a" == "source" ]; then
        read_cfg "$b"
      else
        echo "$a" "$b"
      fi
    done
  }
  read_cfg "$cfg_file"
done | grep -v "^#" | awk '
# this awk script implements the keywords from the cfg files,
# and generates appropriate shell commands for bsewavetool, to
# create and import the .pat files

BEGIN {
  dir = ".";
}

{
  # we do not implement the dir directive correctly:
  #
  # we should be searching _all_ directories in the list for sample files
  # however, we currently only search the _last_ directory in the list for sample files
  if ($1 == "dir")
    {
      dir = $2;
      in_drumset = 0;
    }
  else if ($1 == "cfg") # generated by the "preprocessing stage" to indicate the file we are processing
    {
      cfg = $2;
      in_drumset = 0;
    }
  else if ($1 == "drumset") # a new drumset definition starts
    {
      set = $2;
      in_drumset = 1;
      filename = sprintf ("%s_drumkit_%s.bsewave", cfg, set);
      printf ("if test -f \"%s\"; then echo \"***\n*** %s already exists\n***\"; exit 1; fi\n", filename, filename);
      printf ("bsewavetool create %s 1\n", filename);
      printf ("bsewavetool xinfo %s_drumkit_%d.bsewave --wave play-type=plain-wave-1\n", cfg, set, note);
    }
  else if ($1 == "bank") # a tone bank definition starts (we do not process these)
    {
      in_drumset = 0;
    }
  else if (NF >= 2 && in_drumset) # if we are currently parsing a drumset, import chunks:
    {
      note = $1;
      if (note > 5 && note < 120)
        {
          patch_file = $2;
          printf ("bsewavetool add-chunk %s_drumkit_%d.bsewave -m=%d %s/%s\n", cfg, set, note, dir, patch_file);
          printf ("bsewavetool xinfo %s_drumkit_%d.bsewave -m=%d loop-type=none\n", cfg, set, note);
        }
    }
}' | /bin/sh -x

