A sketch to upgrade working bootloader?

I understand if the 328P has no bootloader installed, or if it corrupted, you would need a programming device of some kind to flash the bootloader. But if there is an existing working bootloader of an older version, it seems you should be able to flash firmware to the device which contains within it an image of Optiboot, and on first run that program would check to make sure Optiboot is not already installed, and if not, erase the bootloader area, then copy the new bootloader from the image to the bootlader area, then shut down. No programming device needed.

Is there a 328P limitation of some kind that prevents a program operating in flash memory from erasing and rewriting the bootloader area (which I understand is just a section of regular flash memory)? If not, is there any other reason why this won't work?

Some discussion on the topic here:

ShermanP:
I understand if the 328P has no bootloader installed, or if it corrupted, you would need a programming device of some kind to flash the bootloader. But if there is an existing working bootloader of an older version, it seems you should be able to flash firmware to the device which contains within it an image of Optiboot, and on first run that program would check to make sure Optiboot is not already installed, and if not, erase the bootloader area, then copy the new bootloader from the image to the bootlader area, then shut down. No programming device needed.

Is there a 328P limitation of some kind that prevents a program operating in flash memory from erasing and rewriting the bootloader area (which I understand is just a section of regular flash memory)? If not, is there any other reason why this won't work?

on AVR only code in bootloader area can write to flash. you would need a copy function in the last page of the flash.