Merge pull request #7 from jiriks74/main

Update to main
This commit is contained in:
Jiří Štefka 2023-02-23 08:31:22 +01:00 committed by GitHub
commit de2c6b1f75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

122
start.sh
View file

@ -7,7 +7,7 @@
# and acknowledge the original script and author. # # and acknowledge the original script and author. #
############################################################################################################# #############################################################################################################
CURRENT_VERSION="1.0.0" CURRENT_VERSION="v1.0.2"
# -------------------------------------------------- # --------------------------------------------------
# You shouldn't need to change anything in this file # You shouldn't need to change anything in this file
@ -19,7 +19,7 @@ REPO_OWNER="jiriks74"
REPO_NAME="start_papermc.sh" REPO_NAME="start_papermc.sh"
# API URL # API URL
api_url="https://api.papermc.io/v2/projects/paper/versions/$select_version/builds" api_url=""
# Check for dependencies # Check for dependencies
function check_dependencies { function check_dependencies {
@ -356,54 +356,31 @@ function launch_server {
java $java_launchoptions -jar "$(basename ./paper-*.jar)" $mc_launchoptions java $java_launchoptions -jar "$(basename ./paper-*.jar)" $mc_launchoptions
} }
# Function to update the script
function check_self_update {
# Send a request to the GitHub API to retrieve the latest release
response=$(curl -sL "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/latest")
# Extract the latest version from the response
LATEST_VERSION=$($response | jq -r ".tag_name")
# Compare the current version with the latest version
if [[ "$CURRENT_VERSION" != "$LATEST_VERSION" ]]; then
echo "An update is available!"
echo "Current version: $CURRENT_VERSION"
echo "Latest version: $LATEST_VERSION"
echo
echo "The script will continue in 15 seconds."
read -t 15 -p "Do you want to read the release notes? [y/N]"
if [ "$REPLY" == "y" ] || [ "$REPLY" == "Y" ]; then
# Extract the release notes from the response
RELEASE_NOTES=$(echo "$response" | jq -r ".body")
# Print the release notes
echo "$RELEASE_NOTES" | less
# Ask the user if they want to update
echo "The script will continue in 15 seconds."
echo "If you decide to update it is your responsibility to check the release notes for any breaking changes."
read -t 15 -p "Do you want to update? [y/N] " update
if [ "$update" == "y" ] || [ "$update" == "Y" ]; then
download_self_update
else
echo "Skipping update."
return
fi
fi
fi
# Download the update for the script # Download the update for the script
function self_update { function self_update {
# Download the latest version of the script # Download the latest version of the script
echo "Updating script..." echo "Updating script..."
if curl --fail -sLJO "https://github.com/$REPO_OWNER/$REPO_NAME/releases/download/$LATEST_VERSION/start.sh"; then # Download the file into start_new.sh
chmod +x script.sh curl -sLJ -w '%{http_code}\n' "https://github.com/$REPO_OWNER/$REPO_NAME/releases/download/$LATEST_VERSION/start.sh" > start_new.sh
# Check if the download was successful by checking the last line of the file for 200
if [[ $(cat start_new.sh | tail -n 1) == 200 ]]; then
# Remove the last line of the file
sed -i '$d' start_new.sh
# Make the file executable
chmod +x start_new.sh
# Remove the old script
rm start.sh
echo "Removed old script"
# Rename the new script
mv start_new.sh start.sh
echo "Renamed new script"
echo "Script updated successfully." echo "Script updated successfully."
echo echo
read -p "Do you want to restart the script? [y/N] " restart read -p "Do you want to restart the script? [y/N] " restart
if [ "$restart" == "y" ] || [ "$restart" == "Y" ]; then if [ "$restart" == "y" ] || [ "$restart" == "Y" ]; then
echo "Restarting..." echo "Restarting..."
exec "./script.sh" "$@" # Execute the new script
exec "./start.sh" "$@"
exit 0 exit 0
else else
echo "Exiting..." echo "Exiting..."
@ -411,6 +388,51 @@ function self_update {
fi fi
else else
echo "Failed to update script." echo "Failed to update script."
rm start_new.sh
exit 5
fi
}
# Function to update the script
function check_self_update {
# Send a request to the GitHub API to retrieve the latest release
response=$(curl -sL "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/latest")
if [[ $response =~ "API rate limit exceeded" ]]; then
echo "API limit exceeded. Will not check for updates."
else
# Extract the latest version from the response
LATEST_VERSION=$(echo $response | jq -r ".tag_name")
# Compare the current version with the latest version
if [[ "$CURRENT_VERSION" != "$LATEST_VERSION" ]]; then
echo "An update is available!"
echo "Current version: $CURRENT_VERSION"
echo "Latest version: $LATEST_VERSION"
echo
echo "The script will continue without updating in 15 seconds."
echo "If you decide to update it is your responsibility to check the release notes for any breaking changes."
read -t 15 -p "Do you want to read the release notes? [y/N]"
if [ "$REPLY" == "y" ] || [ "$REPLY" == "Y" ]; then
# Extract the release notes from the response
RELEASE_NOTES=$(echo "$response" | jq -r ".body")
# Print the release notes
echo "$RELEASE_NOTES" | less
# Ask the user if they want to update
echo
echo "The script will continue without updating in 15 seconds."
echo "If you decide to update it is your responsibility to check the release notes for any breaking changes."
read -t 15 -p "Do you want to update? [y/N] " update
if [ "$update" == "y" ] || [ "$update" == "Y" ]; then
self_update
else
echo "Skipping update."
return
fi
fi
fi
fi fi
} }
@ -420,7 +442,13 @@ function load_config {
if [ ! -f launch.cfg ]; then if [ ! -f launch.cfg ]; then
echo "Config file does not exist." echo "Config file does not exist."
echo "Downloading the default config file..." echo "Downloading the default config file..."
if curl --fail -sLJO "https://github.com/$REPO_OWNER/$REPO_NAME/releases/download/$CURRENT_VERSION/launch.cfg"; then # Download the default config file for the current version
curl -sLJ -w '%{http_code}' "https://github.com/$REPO_OWNER/$REPO_NAME/releases/download/$CURRENT_VERSION/launch.cfg" > launch.cfg
# Check if the download was successful by checking the last line of the file for 200
if [[ $(cat launch.cfg | tail -n 1) == 200 ]]; then
# Remove the last line of the file
sed -i '$d' launch.cfg
echo echo
read -p "Do you want to edit the config file? [y/N] " edit_config read -p "Do you want to edit the config file? [y/N] " edit_config
if [ "$edit_config" == "y" ] || [ "$edit_config" == "Y" ]; then if [ "$edit_config" == "y" ] || [ "$edit_config" == "Y" ]; then
@ -445,6 +473,7 @@ function load_config {
# Load config # Load config
source launch.cfg source launch.cfg
api_url="https://api.papermc.io/v2/projects/paper/versions/$select_version/builds/"
} }
# Main function # Main function
@ -452,6 +481,9 @@ function main {
# Check for script updates # Check for script updates
check_self_update check_self_update
# Load config
load_config
# Check dependencies # Check dependencies
check_dependencies check_dependencies
@ -474,7 +506,8 @@ function main {
# Check for updates on GitHub # Check for updates on GitHub
if [[ "$1" == "--redownload" ]]; then if [[ "$1" == "--redownload" ]]; then
self_update self_update
else if [[ "$1" == "--help" ]]; then fi
if [[ "$1" == "--help" ]]; then
echo "Usage: ./script.sh [OPTION]" echo "Usage: ./script.sh [OPTION]"
echo "Starts the Minecraft server." echo "Starts the Minecraft server."
echo echo
@ -486,7 +519,8 @@ else if [[ "$1" == "--help" ]]; then
echo "If the file does not exist, it will be downloaded automatically when the script is run and you will be asked if you want to edit it." echo "If the file does not exist, it will be downloaded automatically when the script is run and you will be asked if you want to edit it."
echo echo
echo "For more information, see:" echo "For more information, see:"
echo "https://github.com/$GITHUB_USER/$GITHUB_REPO" echo "https://github.com/$REPO_OWNER/$REPO_NAME"
exit 0
else else
# Run the main function # Run the main function
main main