Two ways. Both require CapnBry's WeaponQuickSwap at
http://capnbry.net/wow/
Method 1: If you grab CapnBry's WeaponQuickSwap (near bottom of
http://capnbry.net/wow/ ) you can make a macro like this:
/script local o,t=GetInventoryItemLink("player",17),"target" if UnitExists(t) and UnitPowerType(t)==0 and not o then WeaponSwap("
Looming Gavel","
Crest of Darkshire") elseif o then WeaponSwap("
Whirlwind Axe") end
/cast Shield Bash
Change the bits in bold to your weapons/shield. Then use this macro in place of Shield Bash. You can change /cast Shield Bash to /cast Sunder Armor or even /cast Heroic Strike and use in place of those abilities instead.
Due to the cooldown, if it has to do a weapon swap it won't perform the "cast" right away, so you'll need to hit the button again to actually shield bash if a swap occurs. You may find it better to use Heroic Strike instead of Shield Bash, so you can swap back to 2h once you're on a non-caster target.
Method 2: I was going to copy-paste a small set of files but I kinda fleshed the mod out. When it's approved download AutoShieldSwap. It will do exactly what you're asking.
/autoshieldswap [normal main] [normal off] [caster main] [cast off]
will create two sets of weapons. When you target an enemy caster it will use the caster set. When you target an enemy non-caster it will use the normal set.