Does your controller support TCPM? (Also referred to as RTCP). This feature allows the tooltip to stay in relation to the part 0. Without it, you are going to have to factor in the machine kinematics for your rotations. I would think that you must have some version of that.
See if you can use M128 (TCPM on) and M129 (TCPM off). Also see if your controller supports M126 (rotates to the next angle with the least movement. For example, if you need to rotate 359°, M126 will cause a -1° move, not a +359° move).
Start there. Once you understand the available features, it should be easier to customize a useable post.
(Unless I totally misunderstood the problem, if so, please let me know).
Dan
(Note: The opinions expressed in this post are my own and are not necessarily those of CNCzone and its management)