#!/bin/bash
# By stffn@dibidut.dk

####
# The list of UGENS in the format: #UGEN:oldUGEN:newUGEN

#UGEN:sinosc:SinOsc
#UGEN:std:Std
#UGEN:gain:Gain
#UGEN:foobar:FooBar
#UGEN:add:MORE


####
# The script

if [ "${1}" = "" ]; then
    echo "Usage ${0} filename.ch"
    echo "Fx use with a fo loop to run on all .ch files in dir:"
    echo "for i in *.ch; do ${0} \${i}; done"
    exit 1
fi

cp ${1} /tmp/${1}.-1.tmp
lst=( $(grep -e ^#UGEN ${0}) )
i="0"
while [ ${i} -lt ${#lst[@]} ]; do
  old=$(echo ${lst[${i}]} | cut -d : -f2)
  new=$(echo ${lst[${i}]} | cut -d : -f3)
  sed -e "s/${old}/${new}/g" /tmp/${1}.$((${i}-1)).tmp > /tmp/${1}.${i}.tmp
  let i+=1
done;

cp /tmp/${1}.$((${#lst[@]}-1)).tmp ./${1}

Echo "Have a look in /tmp/ if something went wrong. ${1}.-1.tmp is the original file."