#! /usr/bin/python3
# vim: set filetype=python:

# notabs: expand tab characters to spaces in files

# Copyright (C) 2024-2026 by Brian Lindholm.  This file is part of the
# littleutils utility set.
#
# The notabs utility is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later version.
#
# The notabs utility is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# the littleutils.  If not, see <https://www.gnu.org/licenses/>.

import getopt, os, signal, sys

### PREP SIGNAL HANDLER ###
interrupted = False
def handler(signum, frame):
    global interrupted
    interrupted = True
for signal_VAL in (signal.SIGHUP, signal.SIGINT, signal.SIGPIPE, signal.SIGQUIT, signal.SIGTERM):
    signal.signal(signal_VAL, handler)

### GET INPUT ARGUMENTS ###
# print online help
def usage(rc: int) -> None:
    print('notabs 1.4.0')
    print('usage: notabs [-f filelist] [-h(elp)] [-m max_filesize] [-p(ipe)] [-q(uiet)]')
    print('         [-t tabsize] [-v(erbose)] filename ...')
    sys.exit(rc)
# load list of files
def load_list_from_file() -> None:
    if not os.path.isfile(opt_f):  # abort if file does not exist
        print('notabs error: file list %s does not exist' % opt_f, file=sys.stderr)
        sys.exit(1)
    try:
        FILE = open(opt_f, 'r')
    except:  # abort if file cannot be opened for read
        print('notabs error: file list %s cannot be opened' % opt_f, file=sys.stderr)
        sys.exit(1)
    filelist.extend(FILE.read().splitlines())
    FILE.close()
# load list of files from stdin
def load_list_from_stdin() -> None:
    filelist.extend(sys.stdin.read().splitlines())
    sys.stdin.close()
# set defaults
filelist = []
default_filesize_limit = 1024 * 1024 * 1024  # 1 GiB
opt_f = None   # file containing list of files to process
opt_m = default_filesize_limit  # maximum permissible filesize
opt_p = False  # read list of files to process from stdin
opt_q = False  # be quiet
opt_t = 8      # tab size
opt_v = False  # be verbose
# get command-line options
try:
    opts, filelist = getopt.getopt(sys.argv[1:], 'f:hm:pqt:v', 'help')
except getopt.error as msg:
    # print help if bad opts used, then quit
    print(msg)
    usage(1)
# parse options
for o, v in opts:
    if o in ('-h', '--help'): usage(0)
    elif o == '-f': opt_f = str(v)
    elif o == '-m':
        opt_m = int(v)
        if opt_m < 1: opt_m = default_filesize_limit
    elif o == '-p': opt_p = True
    elif o == '-q': opt_q = True
    elif o == '-t': opt_t = int(v)
    elif o == '-v': opt_v = True
# load file list from file and/or stdin if requested
if opt_f != None: load_list_from_file()
if opt_p: load_list_from_stdin()
# make sure we have at least one file to process
if len(filelist) == 0:
    if (not opt_f) and (not opt_p): usage(1)
    sys.exit(0)
# remove leading './' and trim list to unique items
filelist = [x.removeprefix('./') for x in filelist]
seen = set()
unique_filelist = [x for x in filelist if x not in seen and (seen.add(x) or True)]

### MAIN PROGRAM ###
# replace strings in a file
def process_file(filename: str) -> None:
    # skip if file does not exist
    if interrupted: return
    if not os.path.isfile(filename):
        if not opt_q: print('notabs error: %s is not a file' % filename, file=sys.stderr)
        return
    # skip on zero-length input or if size is too large
    size = os.path.getsize(filename)
    if size == 0:
        if opt_v: print('notabs message: skipping zero length %s' % filename, file=sys.stderr)
        return
    if size > opt_m:
        if not opt_q: print('notabs warning: skipping oversized file %s' % filename, file=sys.stderr)
        return
    # open file for read, aborting if it fails
    try:
        FILE = open(filename, 'r')
    except:
        if not opt_q: print('notabs error: %s cannot be opened' % filename, file=sys.stderr)
        return
    try:
        lines = FILE.read().splitlines()
    except:  # skip if the read and split fails (likely due to it being a binary file)
        print('notabs error: %s did not read correctly' % filename, file=sys.stderr)
        return
    FILE.close()
    # process the lines
    newlines = [x.expandtabs(opt_t) for x in lines]
    if interrupted: return
    # skip if there is no change
    if lines == newlines:
        if opt_v: print('%s: unchanged' % filename)
        return
    # re-open file for write and abort if it fails
    try:
        FILE = open(filename, 'w')
    except:
        if not opt_q: print('notabs error: %s cannot be opened for writing' % filename)
        return
    if not opt_q: print('%s: tabs removed' % filename)
    for line in newlines: print(line, file=FILE)
    FILE.close()

# process files
for filename in unique_filelist:
    if interrupted: break
    process_file(filename)
