#!/usr/bin/env python

import os
import sys
import argparse

#list of known formats here can be added
"""
All format lists were taken from wikipedia, not all of them were added due to extensions
not being exclusive to one format such as webm, or raw
Audio 		- 	https://en.wikipedia.org/wiki/Audio_file_format
Images 		- 	https://en.wikipedia.org/wiki/Image_file_formats
Video 		- 	https://en.wikipedia.org/wiki/Video_file_format
Documents 	-	https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions Majority of it is from MS Office
"""

def moveto(file, from_folder, to_folder):
	#print "Moving ",file," to ", to_folder

	if not os.path.exists(to_folder):
            os.makedirs(to_folder)

	from_file	=	os.path.join(from_folder, file)
	to_file		=	os.path.join(to_folder, file)

	os.rename(from_file,to_file)

def classify(formats, output):

	print("Scanning Files")

	directory = os.getcwd()

	for file in os.listdir(directory):
		filename, file_ext = os.path.splitext(file)
		file_ext = file_ext.lower()

		for folder, ext_list in list(formats.items()):

			folder = os.path.join(output, folder)

			if file_ext in ext_list:
				moveto(file, directory, folder)

	print("Done!")

def main(argv):
    description =   "Organize files in your directory instantly, "
    description +=  "by classifying them into different folders"
    parser = argparse.ArgumentParser(description = description)

    parser.add_argument("-st", "--specific-types", type=str, nargs='+',
        help="Move all file extensions, given in the args list, in the current directory into the Specific Folder")

    parser.add_argument("-sf", "--specific-folder", type=str,
        help="Folder to move Specific File Type")

    parser.add_argument("-o","--output", type=str,
        help="Main directory to put organized folders")

    args = parser.parse_args()

    formats = {
		'Music'		 :	 ['.mp3','.aac','.flac','.ogg','.wma','.m4a','.aiff','.wav'],
		'Videos'	 :	 ['.flv','.ogv','.avi','.mp4','.mpg','.mpeg','.3gp','.mov','.wmv','.mkv','.m4v'],
		'Pictures'	 : 	 ['.png','.jpeg','.gif','.jpg','.bmp','.svg','webp','.nef','.hdr','.tiff'],
		'Archives'	 : 	 ['.rar','.zip','.7z','.gz','.bz2', '.tar', '.pkg', '.img','.iso','.eml','.tgz','.hqx'],
		'Documents'	 :	 ['.txt', '.pdf','.doc','.docx','.xls','.xlsv','.xlsx',
	           			  '.ppt','.pptx','.ppsx','.odp','.odt','.ods','.csv','.key','.rtf','.localized'],
	    'Misc'		 :	 ['.torrent','.crdownload','.vcf'],
	    'Executables':   ['.dmg', '.exe', '.apk', '.app'],
	    '3D'         :   ['.stl', '.obj', '.mtl', '.igs', '.gcode', '.fbx', '.3ds', '.blend', '.mix' ],
	    'Graphics'   :   ['.ai','.psd','.ico','.icns'],
	    'Code'       :   ['.py','.ino','.js', '.html', '.css', '.pde', '.php', '.json', '.md', '.cs','.sql','.ini','.xmp','.unitypackage','.opml','.webloc','.log' ]
	}

    if ((args.specific_folder is None) != (args.specific_types is None)):
    	print('Specific Folder and Specific Types should be combined')
    	sys.exit()

    if ((not (args.specific_folder is None)) and (not (args.specific_types is None))):
    	formats = {args.specific_folder : args.specific_types}

    if (args.output is None):
    	args.output = os.getcwd()

    classify(formats, args.output)

    sys.exit()

if __name__ == "__main__":
   main(sys.argv[1:])
