How to import TauDEM D8 direction into GRASS GIS

TauDEM D8 direction (d8) uses the following encoding:

taudem-fdr
Figure 1: TauDEM D8 encoding

This is GRASS GIS drainage (drain) encoding:

grass-fdr
Figure 2: GRASS GIS drainage encoding
# import D8
r.in.gdal input=d8.tif output=d8

# convert D8 to drain
r.mapcalc ex="drain=if(d8-1,d8-1,8)"

# convert drain back to D8
# use abs() for possibly negative directions from outside the DEM
r.mapcalc ex="d82=1+abs(drain)%8"