Take a look at Nutyx or some smaller projects like aryalinux, here:
https://bitbucket.org/chandrakantsin...linux/src/4.0/
Try building LFS (the standard book), try adding pkgtools, try building nutyx, try to build LFS with pkgtools, try to rebuild a few packages, try to upgrade a few packages, ...
After a few months and a few dozen builds you get might the hang of it and could try to script it.
You might find some help integrating pkgtools from nobodino's effort on (re)building slackware from scratch.
Try one approach though and stick to it.
Another intersting project was lfscript but a bit outdated. To build it you need an older host distro.
Step one is deciding your host distro and adding the needed tools.