#!/usr/bin/python3
"""
Simple source-code line count script, used for dev metrics only
"""
import os, sys
from glob import glob
srcenc = 'utf8'  

# python and kivy-lang 
mains = sorted(glob('*.py')) + sorted(glob('*.kv'))

extras0 = glob('tools/*/*.py')
extras0+= glob('tools/*.desktop')
extras0+= glob('pc-build/build-app-exe/build4.0/*.py')
extras0+= glob('pc-build/build-app-exe/build4.0/*.spec') 
extras0+= glob('pc-build/build-source/*.py')

# *.spec are gray (generated and edited)
extras1 = glob('frigcal-pc/*.desktop')
extras1+= glob('_dev-misc/UPLOAD-KEY/*.sh')
extras1+= glob('*.spec') + glob('*.xml')
extras1+= glob('_website/*.py') + glob('_website/*.sh')   # copied from mbp2025 here
extras1+= glob('_website/Frigcal/*.py')
extras1+= glob('_website/Frigcal/+screenshots/*.sh')
extras1+= glob('_website/Frigcal/+screenshots/_etc/*/*.sh')

# omit special-case dup for open-source publication
# moot in FC: source-cde package is in a separate dir
if os.path.exists('_website/Frigcal/code'):
    extras1.remove('_website/Frigcal/main.py')

# in-app docs (but not _website docs and not generated settings.JSON)
extras2 = sorted(glob('*.txt') + glob('*.html'))

# don't count embedded systems, even if forked/tweaked
# in FC, this means icalendar, pytz, and distro

countself = True     # count this scipt? pasting new output mods the output (run twice)
tally = count = 0    # number lines, files

def segment(files, accum=True):
    global tally, count
    stally, scount = 0, 0
    for fname in files:   # not sorted()        
        if countself or not fname.startswith('__sloc'):
            fobj = open(fname, 'r', encoding=srcenc)
            lcnt = len(fobj.readlines())
            fobj.close()
            stally += lcnt
            scount += 1
            print(f'{fname} => {lcnt:,}')
    print(f'Segment sloc in {scount:,} file(s): {stally:,}\n')
    if accum:
        tally += stally
        count += scount

segment(mains)
segment(extras0, accum=True)
segment(extras1, accum=True)
segment(extras2, accum=True)

print(f'Total sloc in {count:,} files: {tally:,}')
print('(Sans some doc .txts, generated .jsons, website .htmls, showcode mods, build-tool mods)')

if sys.platform.startswith('win') and sys.stdout.isatty():
    input('Press Enter')    # if clicked, more or less


r"""
================================================================================
Example output (current counts/manifest; 3.0 was 7808 lines, 23 files):

__sloc__.py => 124
common.py => 276
hangless.py => 231
icsfiletools.py => 381
main.py => 3,240
monthgui.py => 2,226
prestart_pc_tweaks.py => 422
searchgui.py => 277
settings.py => 338
settingsgui.py => 533
storage.py => 1,879
allpopups.kv => 595
allscreens.kv => 447
common.kv => 246
main.kv => 240
Segment sloc in 15 file(s): 11,455

tools\convert-legacy-colors\new_color_names.py => 19
tools\convert-legacy-colors\map_calendar_colors.py => 29
tools\convert-legacy-colors\_make-convert_legacy_colors-zipfile.py => 33
tools\convert-legacy-colors\convert_legacy_colors.py => 354
tools\convert-legacy-colors\map_category_colors.py => 76
tools\combine-calendars\combine-calendars.py => 188
tools\frigcal-linux.desktop => 40
pc-build/build-app-exe/build4.0\_winver-maker.py => 21
pc-build/build-app-exe/build4.0\build.py => 1,001
pc-build/build-app-exe/build4.0\_Linux--Frigcal.spec => 56
pc-build/build-app-exe/build4.0\_macOS--Frigcal.spec => 73
pc-build/build-app-exe/build4.0\_Windows--Frigcal.spec => 70
pc-build/build-source\build.py => 174
Segment sloc in 13 file(s): 2,134

_dev-misc/UPLOAD-KEY\set-env-vars--PRIOR.sh => 27
_dev-misc/UPLOAD-KEY\set-env-vars.sh => 40
buildozer.spec => 502
manifest-insert-application-attrs.xml => 1
_website\_publish.sh => 103
_website\_generate.sh => 25
Segment sloc in 6 file(s): 698

about-template.txt => 66
help-message.txt => 184
runcounter.txt => 1
terms-of-use.txt => 15
Segment sloc in 4 file(s): 266

Total sloc in 38 files: 14,553
(Sans some doc .txts, generated .jsons, website .htmls, showcode mods, build-tool mods)
[EOF]

================================================================================
"""
