This is a job for switch triggers.
For each group, create waypoints for what you want them to do. Basically that will mean a hold waypoint at each defensive postition.
Now, for each defensive position create a trigger, make it type switch, and synchro it with all the hold waypoints in that position. Make the size of the trigger cover the postition and set to East present (or whatever your defending loons are).
In the On Activation field of the trigger write the conditions that you want. It might look something like this
count units thislist < 10
or this
(count units grp1 < 5) and (count units grp2 < 5) and (count units grp3 < 5)
Syntax not guaranteed.
In the init field of the group leader for each group write
"_x allowfleeing 0" forEach units group this
which will prevent them from running away when you don't want them to. Alternatively, don't bother ..... they may then flee when you don't want them to, but that will make the whole thing more unpredictable and realisitic. There is also a good chance they will run towards their next waypoint, if there have been no enemy in the that direction. They are more likely to run that way if they know that there are friendlies there and if that is where they started from. In other words, if there is time in the mission, insert these loons near the final postition and get them to move through the other positions until they get to their own place. Loons tend to run away towards where they came from, as long as its safe. The will also rally after a while.
I would suggest that, when some of these groups get down to 2 or 3 loons, you join them together. Small groups of AI are not very good. Use the command join in a trigger activated with the count command like the ones above.