File::SEPARATOR="\\"


Shoes.app do
	def row(message, default)
    default ||= ''
		flow do
			flow :width => 0.3 do; para message, :align => "right"; end;
			@ret = edit_line default, :width => 0.7
		end
		@ret
	end
  
  def anychange
    @example.replace "For example, #{FileJoin(root_folder_clean, "more", "folders", "file#{@suffix.text}#{@extension.text}")}\n"+
        "will be renamed to #{FileJoin(root_folder_clean, "more", "folders", "file#{@extension.text}")}"
#    @status.append para time
    if(@running)
      @status1.replace "Working"+ ([0, 1, 2, 3].collect { |i| i == @progress_spot ? ' ' : '.' }).join('')
      @progress_spot += 1
      @progress_spot %= 4
      @status2.replace "#{@folders} folders searched, #{@files} files searched, #{@renames} files renamed.\n"+
          "Checking #{@considering}"
    end
  end
  
  def root_folder_clean
    @root_folder.text
  end
  
  def FileJoin(*args)
    File.join(args).gsub("/", File::SEPARATOR)
  end

	stack do
    @input = stack do
      para "What folder do you want to drill down from?"
      para "If you are hitting multiple drives (C:\ + D:\ + \\\\othercomputer\\share\\) then you'll want to run me multiple times."
  		flow do
        flow :width => 0.3 do; para "Root Folder", :align => "right"; end;
        @root_folder = edit_line "C:\\", :width => 0.7
        # button "browse", :width => 0.2 do
          # @root_folder.text = ask_open_file(@root_folder.text)
        # end
      end
      @extension = row("File extension to target", ".mp3")
      @suffix = row("Suffix to remove", "_XXXX")
    end
      
    @example = para "Application booting, yo\n"

    @submit = button "Spin this shit!" do
      spin
    end
    
    @status1 = para "Awaiting your order, sir."
    @status2 = para
    
    @progress_spot = 0
    @folders = 0
    @files = 0
    @renames = 0
    @considering = ''
    @running = false;
end  
  
  animate 3 do
    anychange
  end
  
  def spin
    @running = true
    @input.hide
    @submit.hide
 
    Thread.new do 
      search([@root_folder.text])
      anychange
      @running = false
      @status1.replace "Job Finished! :)"
    end
  end
  
  # Accept an array of filepath components joinable into a sane filepath
  def search(dir)
    Dir.foreach(FileJoin(dir)) do |file|
      longfile = FileJoin(dir + [file])
#      alert("Considering: #{longfile}")
      
      next if(file == '.' or file == '..')
      @considering = longfile
      
      if(File.directory?(longfile))
        search(dir + [file])
      else
        @files += 1
        next unless file =~ /#{@suffix.text}#{@extension.text}$/
        rename(dir + [file])
      end
    end
    @folders += 1
#    sleep 1
  end
  
  # Accept an array of filepath components joinable into a sane filepath
  def rename(file)
    src = FileJoin(file)
    dest = src.gsub(/#{@suffix.text}#{@extension.text}$/, @extension.text)
    
    File.rename(src, dest)
#    alert("hit #{src}")
    @renames += 1
  end
end  