Construct transformation matrix based on source and destination triangle

Hi Friends !

I want to construct a transformation matrix based on a source triangle and a destination triangle.
The reason for this is that I am cobbling together a script to export the faces of a mesh to the Quake1 map format and I am struggling with the texture mapping.

More specifically, the quake format I am using for describing mapping is the ‘updated’ valve 220 format used for HL1 (not the source port):
https://developer.valvesoftware.com/wiki/MAP_file_format

The challenge for me is to look at a triangle in UV space and the corresponding triangle in 3d space and construct a transformation matrix so I can map the UV box (0,0) to (1,1) in 3d.

For a more tangible example, you can think of this problem as an orientation challenge: to construct a transformation matrix between 3 source points and 3 destination points which includes transformation, rotation, scale and shear.
Transformation and scale are relatively straightforward, but I am struggling with the rotation matrix and I have yet to get a clue on how to tackle shear.

I could really use some pointers here, pretty please.

fyi, the export of geometry is done, and surprisingly easy, given the fact that geometry is represented by brushes:

( for this image I converted each face into a pyramid with a triangular base and a very shallow peak, the peak is oriented to the inside -the negative normal- of the face. )

FYI, I found out that another user has created a quake 1 map exporter already:

Tip: triangulate the mesh before exporting, unless you are sure the polygons are flat…

texturemapping exprted, yay !