util

Clone or download

Somewhat working start at boot for linux

Modified Files

package com.StreamPi.Util.StartAtBoot;
package com.StreamPi.Util.StartAtBoot;
import com.StreamPi.Util.Exception.MinorException;
import com.StreamPi.Util.Exception.MinorException;
import com.StreamPi.Util.Platform.Platform;
import com.StreamPi.Util.Platform.Platform;
import java.io.BufferedWriter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.File;
import java.io.FileWriter;
import java.io.FileWriter;
public class StartAtBoot {
public class StartAtBoot {
SoftwareType softwareType;
SoftwareType softwareType;
Platform platform;
Platform platform;
public StartAtBoot(SoftwareType softwareType, Platform platform)
public StartAtBoot(SoftwareType softwareType, Platform platform)
{
{
this.softwareType = softwareType;
this.softwareType = softwareType;
this.platform = platform;
this.platform = platform;
}
}
public void create(File runnerFile) throws MinorException
public void create(File runnerFile) throws MinorException
{
{
if(platform == Platform.WINDOWS)
if(platform == Platform.WINDOWS)
createStarterForWindows(runnerFile);
createStarterForWindows(runnerFile);
else if(platform == Platform.LINUX || platform == Platform.LINUX_RPI)
else if(platform == Platform.LINUX || platform == Platform.LINUX_RPI)
createStarterForLinux(runnerFile);
createStarterForLinux(runnerFile);
else if(platform == Platform.MAC)
else if(platform == Platform.MAC)
createStarterForMac(runnerFile);
createStarterForMac(runnerFile);
else if(platform == Platform.UNKNOWN)
else if(platform == Platform.UNKNOWN)
unknownPlatformException();
unknownPlatformException();
}
}
public boolean delete() throws MinorException {
public boolean delete() throws MinorException {
if(platform == Platform.WINDOWS)
if(platform == Platform.WINDOWS)
return deleteStarterForWindows();
return deleteStarterForWindows();
else if (platform == Platform.LINUX || platform == Platform.LINUX_RPI)
else if (platform == Platform.LINUX || platform == Platform.LINUX_RPI)
return deleteStarterForLinux();
return deleteStarterForLinux();
else if(platform == Platform.MAC)
else if(platform == Platform.MAC)
unknownPlatformException();
deleteStarterForMac();
else if(platform == Platform.UNKNOWN)
else if(platform == Platform.UNKNOWN)
unknownPlatformException();
unknownPlatformException();
return false;
return false;
}
}
private void createStarterForLinux(File runnerFile) throws MinorException
private void createStarterForLinux(File runnerFile) throws MinorException
{
{
File initFile = new File("/etc/init.d/streampi_starter"+ softwareType);
File initFile = new File("/etc/init.d/streampi_starter_"+ softwareType);
try
try
{
{
FileWriter fw = new FileWriter(initFile);
FileWriter fw = new FileWriter(initFile);
BufferedWriter bw = new BufferedWriter(fw);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("cd "+runnerFile.getParentFile().getCanonicalPath()+"\n" +
bw.write("#! /bin/sh\n" +
"./"+runnerFile.getName());
"cd "+runnerFile.getAbsoluteFile().getParent()+"\n" +
"sudo ./"+runnerFile.getName()+"\n" +
"exit 0\n");
bw.close();
bw.close();
}
}
catch (Exception e)
catch (Exception e)
{
{
throw new MinorException(e.getMessage());
e.printStackTrace();
throw new MinorException(e.getMessage()+"\nTry running as root and try again");
}
}
}
}
private boolean deleteStarterForLinux()
private boolean deleteStarterForLinux()
{
{
return new File("/etc/init.d/streampi_starter"+ softwareType).delete();
return new File("/etc/init.d/streampi_starter_"+ softwareType).delete();
}
}
private void createStarterForWindows(File runnerFile) throws MinorException
private void createStarterForWindows(File runnerFile) throws MinorException
{
{
File initFile = new File(System.getenv("APPDATA")+"/Microsoft/Windows/Start Menu/Programs/Startup/streampi_starter"+ softwareType +".bat");
File initFile = new File(System.getenv("APPDATA")+"/Microsoft/Windows/Start Menu/Programs/Startup/streampi_starter_"+ softwareType +".bat");
try
try
{
{
FileWriter fw = new FileWriter(initFile);
FileWriter fw = new FileWriter(initFile);
BufferedWriter bw = new BufferedWriter(fw);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("cd "+runnerFile.getParentFile().getCanonicalPath()+"\n" +
bw.write("cd "+runnerFile.getParentFile().getCanonicalPath()+"\n" +
runnerFile.getName());
runnerFile.getName());
bw.close();
bw.close();
}
}
catch (Exception e)
catch (Exception e)
{
{
throw new MinorException(e.getMessage());
throw new MinorException(e.getMessage());
}
}
}
}
private boolean deleteStarterForWindows()
private boolean deleteStarterForWindows()
{
{
return new File(System.getenv("APPDATA")+"/Microsoft/Windows/Start Menu/Programs/Startup/streampi_starter"+ softwareType +".bat").delete();
return new File(System.getenv("APPDATA")+"/Microsoft/Windows/Start Menu/Programs/Startup/streampi_starter_"+ softwareType +".bat").delete();
}
}
private void createStarterForMac(File runnerFile) throws MinorException
private void createStarterForMac(File runnerFile) throws MinorException
{
{
throw new MinorException("Mac Starter feature is not implemented yet.");
throw new MinorException("Mac Starter feature is not implemented yet.");
}
}
private void deleteStarterForMac() throws MinorException
private void deleteStarterForMac() throws MinorException
{
{
throw new MinorException("Mac Starter feature is not implemented yet.");
throw new MinorException("Mac Starter feature is not implemented yet.");
}
}
private void unknownPlatformException() throws MinorException
private void unknownPlatformException() throws MinorException
{
{
throw new MinorException("Cannot implemented starter feature. Unknown platform.");
throw new MinorException("Cannot implemented starter feature. Unknown platform.");
}
}
}
}
module com.StreamPi.Util {
module com.StreamPi.Util {
requires transitive org.kordamp.ikonli.javafx;
requires transitive org.kordamp.ikonli.javafx;
requires transitive org.kordamp.ikonli.fontawesome5;
requires transitive org.kordamp.ikonli.fontawesome5;
requires transitive javafx.base;
requires transitive javafx.base;
requires transitive javafx.controls;
requires transitive javafx.controls;
exports com.StreamPi.Util.Version;
exports com.StreamPi.Util.Version;
exports com.StreamPi.Util.Exception;
exports com.StreamPi.Util.Exception;
exports com.StreamPi.Util.Platform;
exports com.StreamPi.Util.Platform;
exports com.StreamPi.Util.FormHelper;
exports com.StreamPi.Util.FormHelper;
exports com.StreamPi.Util.StartAtBoot;
exports com.StreamPi.Util.StartAtBoot;
exports com.StreamPi.Util.Alert;
exports com.StreamPi.Util.Alert;
exports com.StreamPi.Util.ComboBox;
exports com.StreamPi.Util.ComboBox;
}
}