#!/bin/sh
#
# csffile - give info on csf file(s).
# PD by RJM 960529
#
# it's not terribly robust, and is likely to screw up on non-csfs.
#
# also, it's SLOOOOOWWWW.

if [ "$1" = "" ]; then
  echo 'usage: csffile file1.csf [file2.csf ...]'
  exit 1
fi

plural=n
if [ "$2" != "" ]; then
  plural=y
fi

while [ "$1" != "" ]; do
  if [ ! -f "$1" ]; then
    echo csffile: can\'t read \'$1\' 1>&2
  else
    if [ $plural = y ]; then printf '---\n%s:\n' "$1" ; fi
    
    # get some stats
    blocklist_line=`grep -v '^#' $1 | grep -n '^*blocklist$' |cut -d : -f 1`
    blocks_line=`grep -v '^#' $1 | grep -n '^*blocks$' |cut -d : -f 1`
    tempo=`grep '^*tempo ' $1 |cut -d ' ' -f 2`
    if [ "$tempo" = "" ]; then tempo=125; fi
    block_len=`expr $blocks_line - $blocklist_line`
    sec_len=`expr $block_len \* $tempo / 16`
    stereo1=`grep '^*s ' $1`
    stereo2=`grep '^*S$' $1`
    
    # print some stuff
    if [ "$stereo1" = "" -a "$stereo2" = "" ]; then
      echo mono
    else
      echo stereo
    fi
    echo tempo: $tempo
    printf 'approx len %dm:%02ds\n' `expr $sec_len / 60` `expr $sec_len % 60`
  fi
  
  shift
done
