Auto Increment Field

Hi im struggling to setup an auto increment field

i have a field called project no on the opportunity entity which i need to populate with the client code from the company record (comp_clientcode) and if it is a master opportunity (oppo_type = 'master') then add a PO01 

so if the client code for the company is TEST01 and its the first master opportunity created against that company then the project code needs setting to TEST01_PO01 and this needs to be incremented by 1 whenever a new master oppo is created against that company

ive tried setting up a trigger to try and set this but cant seem to be able to get it to work - any ideas or any sample code anyone can share please