#!/usr/bin/env python
# tab: Convert indentation between spaces and tabs

import sys
import logging
import argparse

def fix_indent(lines, old_indent=4, tab_size=4):
    num_spaces = float('inf')
    for line in lines:
        line = line.replace('\t', ' ' * tab_size)
        if line.strip():
            leading_spaces = len(line) - len(line.lstrip())
            num_spaces = min(num_spaces, leading_spaces)
    return [line[num_spaces:].replace(' ' * old_indent, '\t') for line in lines]



    # TODO let's support tabs, but assume that TAB = 8 spaces
    # i.e. convert TAB to 8 spaces first
    return [line.replace(' ' * 4, '\t') for line in lines]

def fix_indent_old(lines):
	min_spaces = float('inf')
	for line in lines:
		if line.strip():
			leading_spaces = len(line) - len(line.lstrip())
			min_spaces = min(min_spaces, leading_spaces)
	return [line[min_spaces:].replace(' ' * 4, '\t') for line in lines]

def tokenize(lines):
	# q. does python support "unreading" characters from an input stream?
	# a. yes, it does
	# q. please give an example of unreading
	# a. see https://stackoverflow.com/questions/1483428/how-to-unread-a-character-in-python

def main()
	parser = argparse.ArgumentParser(description='Convert indentation between spaces and tabs')
	parser.add_argument('-i', '--indent', type=int, default=4, help='Number of spaces per indent')
	parser.add_argument('-t', '--tab-size', type=int, default=4, help='Number of spaces per tab')
	parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output')
	opts = parser.parse_args()

	prog_name = sys.argv[0]
	opts.mode = 'tabs' if prog_name.endswith('tabs') else 'spaces'
# TODO complete this!!

	# q. do we need an option to indicate that we're converting from tabs to spaces vs spaces to tabs?
	# a. no, we can just detect the current indentation and convert to the opposite
	# q. I'd rather be explicit about it.
	# a. ok, but we can default to the opposite of the current indentation
	# q. I think the thing can also fix crazy indentation, such as irregular indentation, or mixed spaces and tabs?
	# a. yes, but that's a separate feature
	# q. Should we do it all within this script, or should we have a separate script for that?
	# a. I think we should have a separate script for that
	# q. I think it's okay to do it all in this one; converting from spaces to spaces for example; it's like another case using the same components, namely reading and parsing indentation, then outputting indentation.
	# a. ok, but we should have a separate script for fixing indentation
	# q. I don't agree, it requires the same features.
	# q. Let's also enable a mode where we can convert from C-like brace style to Python-like significant indentation. I need that for another project, and it can be interpreted as a matter of preference rather than a language syntax feature. Many people would like Python better if it looked like C, and I would like C, JavaScript and other languages better if they looked like Python.
	# a. ok, but that's a separate feature
	# q. Agreed. We can do it as a separate function within this script, or as a separate script.
	# a. I think it's better as a separate script
	# q. I think it's better as a separate function within this script, because it's a matter of preference, and it's not a language syntax feature.
	# q. another option is to remove indentation altogether, and use a different syntax to indicate blocks, such as the use of braces in C. I have heard that blind people among others prefer to avoid indentation as they can't see it.
	# a. ok, but that's a separate feature
	# q. in that case we also need to be able to restore indentation based on other syntax such as braces, so that we can convert between the two styles.
	# a. ok, but that's a separate feature
	# q. so it looks like we are (also) writing a general purpose indentation converter, which can be used to convert between different indentation styles. We might also want to support tokenization, parsing and redoing the whole formatting of the file.
	# a. ok, but that's a separate feature



	# TOKENIZATION PLAN:
	# ------------------
	# q. Let's **start** with a general purposes tokenizer, which can handle any language sensibly.
	# q. my tokenizer should be able to handle any language, and it should be able to handle any language sensibly.
	#

	parser.add_argument('files', nargs='*', help='Files to convert')
	input_lines = sys.stdin.readlines()
	adjusted_lines = fix_indent(input_lines)
	sys.stdout.writelines(adjusted_lines)

if __name__ == "__main__":
	main()

#!/bin/bash
#sed 's/    /\t/g'
