/*
This java file will remove entries occuring twice in your m3u playlist.
(Where double means: stored in exactly the same place!)
*/
import java.util.*;
import java.io.*;
 
public class removeDoublesFromAnM3uPlaylist{
 
public static void main(String[] args){
String fn="";
for(int i=0;i<args.length;i++)//just in case filename includes spaces
  fn+=args[i];
File inf=new File(fn);
PrintWriter out = null;
BufferedReader data=null;
String read ="///";
if((args.length==0)||(!inf.exists())){
  System.err.println("This programme requires one parameter: The m3u playlist to change!");
  System.exit(0);
  }
System.out.println("Now removing doubles from your playlist; result will be stored in "+fn+".new");
try{
data=new BufferedReader(
     new FileReader(
     inf));
out = new PrintWriter(
      new OutputStreamWriter(
      new FileOutputStream(
      new File(fn+".new"))));
Vector hadThisAlready=new Vector(1000,1000);
int count=0;
while(read!=null && read.length()>0){
  read=data.readLine();
  if(read!=null){
   if((read.startsWith("#"))&&(read.length()>10)){
    if(hadThisAlready.contains(read)){
      System.out.println("Removing:\n"+read);
      read=data.readLine();
      System.out.println(read);
      count++;
      }
    else{
      out.println(read);
      hadThisAlready.add(read);
      }
    }
   else
    out.println(read);
  }
}
out.close();
System.out.println("Successfully finished; "+count+" doubles removed.");
}catch(Exception e){System.err.println("Something went wrong! "+e);}

}

} 
