Prefix CID with a fixed value by trunk route

Is there a way to prefix the extension number with a fixed value by trunk group?

Example:
My extension is 7331 in both cases.

If I make a call that goes out on an Intracompany route where we use an internal 7 digit dial plan, I’d like to prefix the extension with (for example) 772 so the resulting CID is 7727331.

If I make a call that goes out on a route that eventually hits the PSTN, I’d like to prefix the extension with (for example) 1212555 so the resulting CID is 12125557331, which would be a number within our DID range.

I know that CID can replace station based CID on a route (trunk) basis, but I don’t want to force a specific number - I want to pass something that will allow a callback whether it is on-net or PSTN.

This isn’t going to be a full answer, but this document might get you started:

How to execute a custom dial plan fragment before sending a call to a trunk (for playing an announcement, etc.)

The idea would be to look at which trunk the call is going out on, then (I think) prepend the appropriate prefix to the variable CALLERID(num) (see How to change incoming CallerID for example of how this is done with incoming calls).

Shouldn’t be that difficult, really.