Measure battery capacity (create a battery capacity measuring circuit)

I would like to build a thing which I can connect to any standard A, AA, AAA or similar battery (rechargeable or non-rechargeable) to measure the capacity of that battery. As a precondition, the battery to measure would be either new (non-rechargeable) or fully charged (rechargeable). The circuit should discharge the battery in a controlled manner while recording the parameters and in the end, when the battery is fully discharged, calculate the capacity.
I suppose I can discharge the battery with a defined limited current or over a defined limited resistance and periodically measure the voltage and/or current. I can somehow imagine the code but not the circuitry I need for this job. Anyone interested to help me?