File bundle_file = getAbsoluteBundleFile();
if ( !bundle_file.exists()){
throw( new PlatformManagerException( "Failed to write set run-at-login, bundle not found" ));
}
File f = getLoginPList();
if ( f.exists()){
convertToXML( f );
}else{
try{
PrintWriter pw = new PrintWriter( new OutputStreamWriter( new FileOutputStream( f ), "UTF-8" ));
try{
pw.println( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
pw.println( "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" );
pw.println( "<plist version=\"1.0\">" );
pw.println( "<dict>" );
pw.println( "</dict>" );
pw.println( "</plist>" );
}finally{
pw.close();
}
}catch( Throwable e ){
throw( new PlatformManagerException( "Failed to write output file", e ));
}
}
try{
List<String> lines = new ArrayList<String>();
LineNumberReader lnr = new LineNumberReader( new InputStreamReader(new FileInputStream( f ), "UTF-8" ));
int dict_line = -1;
int auto_launch_line = -1;
int target_index = -1;
String target = bundle_file.getAbsolutePath();
try{
while( true ){
String line = lnr.readLine();
if ( line == null ){
break;
}
lines.add( line );
if ( dict_line == -1 && containsTag( line, "<dict>" )){
dict_line = lines.size();
}
if ( auto_launch_line == -1 && containsTag( line, "AutoLaunchedApplicationDictionary" )){
auto_launch_line = lines.size();
}
if ( line.contains( target )){
target_index = lines.size();
}
}
if ( dict_line == -1 ){
throw( new PlatformManagerException( "Malformed plist - no 'dict' entry" ));
}
if ( auto_launch_line == -1 ){
lines.add( dict_line, "\t<key>AutoLaunchedApplicationDictionary</key>" );
auto_launch_line = dict_line+1;
lines.add( auto_launch_line, "\t<array>" );
lines.add( auto_launch_line+1, "\t</array>" );
}
}finally{
lnr.close();
}
if ( run ){
if ( target_index != -1 || auto_launch_line == -1 ){
return;
}
target_index = auto_launch_line+1;
lines.add( target_index++, "\t\t<dict>" );
lines.add( target_index++, "\t\t\t<key>Path</key>" );
lines.add( target_index++, "\t\t\t<string>" + target + "</string>" );
lines.add( target_index++, "\t\t</dict>" );
}else{
if ( target_index == -1 ){
return;
}
while( !containsTag( lines.get( target_index ), "</dict>" )){
lines.remove( target_index );
}
lines.remove( target_index );
target_index--;
while( !containsTag( lines.get( target_index ), "<dict>" )){
lines.remove( target_index );
target_index--;
}
lines.remove( target_index );
}
File backup = new File( f.getParentFile(), f.getName() + ".bak" );
if ( backup.exists()){
backup.delete();
}
if ( !f.renameTo( backup )){
throw( new PlatformManagerException( "Failed to backup " + f ));
}
boolean ok = false;
try{
PrintWriter pw = new PrintWriter( new OutputStreamWriter( new FileOutputStream( f ), "UTF-8" ));
try{
for ( String line: lines ){
pw.println( line );
}
}finally{
pw.close();
if ( pw.checkError()){
throw( new PlatformManagerException( "Failed to write output file" ));
}
ok = true;
}
}finally{
if ( !ok ){
backup.renameTo( f );
}
}
}catch( PlatformManagerException e ){
throw( e );
}catch( Throwable e ){
throw( new PlatformManagerException( "Failed to write output file", e ));
}
}