#! /usr/bin/env nix-shell
#! nix-shell -i fish -p fish

set root_directories /srv/server/pool/multimedia/media/Library/comics \
    /srv/server/pool/multimedia/media/Library/manga/manga \
    /srv/server/pool/multimedia/media/Library/webtoons
set blacklist 'NOBLESSE|Air Gear'
for root_dir in $root_directories
    for dir in (find $root_dir -mindepth 1 -type d)
        cd $dir
        fd -q -tfile $blacklist
        if test $status -eq 0
            continue
        end
        fd -q -tfile '((\d+|\d+\.\d+)(\.cbz|\.pdf))$'
        if test $status -eq 1
            continue
        end
        set max_chapter (find -not -name '*Annual*' -print0 | string match -ar '(\d+|\d+\.\d+)(?=\.cbz|\.pdf)' | sort -h | tail -n 1)
        set min_chapter (find -not -name '*Vol.*' -not -name '*TPB*' -print0 |
            string match -ar '(\d+|\d+\.\d+)(?=\.cbz|\.pdf)' |
            sort -h |
            head -n 1)
        set ext (fd . | string match -r '(\.cbz|\.pdf)' | tail -n 1)
        set base_name (fd . | string match -r '(.*(?<!Annual )(#|Vol\.|Ch\.))(?=(\d+|\d+\.\d+)(\.cbz|\.pdf))' | head -n 1)
        for i in (seq $min_chapter $max_chapter)
            set theorical_file (echo $base_name$i$ext)
            if test -e $theorical_file
                continue
            end
            pwd
            echo $theorical_file
        end
    end
end
