A reader asks what he can do to avoid the mess that I had to face.
Maybe you should ask someone who has had a successful marriage.
I suggest trying to maintain good relationships with your wife and your kids. Keep a record of the good things that you do, so that you can prove it later. Ignore your wife's adultery; the court doesn't care about that.
There are a bunch of books for fathers facing family court. I posted a free link to a draft of one a couple of days ago. The books have a lot of good advice.