What exactly is your question? What have you tried so far?
We're not going to write your whole script for you, if that's what you're asking for, especially with only a vague description of what it should do. But we will advise you and help you to overcome any difficulties you come across.
Here are a few useful bash scripting references to help you get started.
The first one is an excellent tutorial for beginners, and you should definitely read through the second one too for a more detailed conceptual understanding. Also check out the last one, to learn how to avoid many common mistakes.
My advice is to start by figuring out how to do each individual step independently on the command line. Then you can build a script around them to automate the process.
Good luck to you!