public static void main(String[] args) throws InterruptedException, IOException {
		new File(output_dir, "").mkdirs();
		
		FirefoxProfile profile = Utils.createFireFoxProfile();
		profile.setPreference("browser.download.dir", download_dir.getPath());
		
		WebDriver driver = Utils.createFireFoxDriver(profile);
		
		String mainWin = driver.getWindowHandle();
        
		Utils.openTvdb(driver, "`qި");
    	
    	driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL+"n");
    	driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL+"n");

    	driver.findElement(By.partialLinkText("C L t ")).click();
        driver.findElement(By.partialLinkText("`qި")).click();
        
        String[] newWindows = new String[2]; 
        int nWin = 0;
        for(String hWnd : driver.getWindowHandles()) {
        	if(!hWnd.equals(mainWin)) {
        		newWindows[nWin] = hWnd;
        		nWin++;
        		if(nWin == 2)
        			break;
        	}
        }
        
        for(WebElement link: driver.findElements(By.partialLinkText("`qި"))) {
        	// click the link with SHIFT pressed to open it in the new window
//        	new Actions(driver).keyDown(Keys.SHIFT).click(link).keyUp(Keys.SHIFT).perform();
        	String url = link.getAttribute("href");
        	String linkText = link.getText();
        	
            downloadTables(driver, "`qި", url, linkText, EnumSet.allOf(OutputType.class), newWindows);
//            downloadTables(driver, "`qި", url, linkText, EnumSet.of(OutputType.EXCEL), newWindows);
            driver.switchTo().window(mainWin);
        }
        
        driver.switchTo().window(newWindows[0]);
        driver.close();
        driver.switchTo().window(newWindows[1]);
        driver.close();
        driver.switchTo().window(mainWin);
        
        SortedMap<String, List<String>> tableUnits = new TreeMap<String, List<String>>();
        Set<String> unitSet = new HashSet<String>();
        
        Utils.obtain`qި(tableUnits, unitSet);
        
        for(String unit: unitSet) {
    		new File(output_dir, "/"+unit+"/`qި").mkdir();
        }
        
        // Copy table files to the unit in charge according to the table-unit mapping
        for(String tableName: tableUnits.keySet()) {
        	if(tableUnits.get(tableName).isEmpty()) {
        		System.err.println("["+tableName+"]: no unit in charge");
        		continue;
        	}
        	
        	for(OutputType type: EnumSet.allOf(OutputType.class)) {
            	File tableFile = new File(output_dir, "`qި/"+type.name+"/"+tableName+type.ext);
            	for(String unit: tableUnits.get(tableName)) {
            		File unitDir = new File(output_dir, "/"+unit+"/`qި");
            		
            		try {
            			File targetFile = new File(unitDir, tableFile.getName());
            			
            			if(!targetFile.exists()) {
        					FileUtils.copyFile(tableFile, targetFile);
            			}
    					System.out.println("["+tableFile.getName()+"] => ["+unit+"]");
    				} catch (IOException e) {
    					System.err.println("["+tableFile.getName()+"] => ["+unit+"]: failed: "+e.getMessage());
    				}
            	}
        	}
        }
     
        ((JavascriptExecutor)driver).executeScript("alert('Done!')");
	}