diff --git a/publish.sh b/publish.sh index 6233342..9d7eaa5 100755 --- a/publish.sh +++ b/publish.sh @@ -27,6 +27,12 @@ export INSTALL_LOCAL="false" [ -f "${SCRIPT_DIR}/output/dropshell" ] || die "Build failed." +# Get version from the built binary (format: YYYY.MMDD.HHMM) +VERSION=$("${SCRIPT_DIR}/output/dropshell" version | tr -d '[:space:]') +[ -z "$VERSION" ] && die "Failed to get version from dropshell" + +echo "Publishing dropshell version ${VERSION} for ${ARCH}" + # download the sos binary mkdir -p "${TEMP_DIR}" trap 'rm -rf "${TEMP_DIR}"' EXIT @@ -34,8 +40,23 @@ trap 'rm -rf "${TEMP_DIR}"' EXIT curl -L -o "${SOS}" "https://getbin.xyz/sos" chmod +x "${SOS}" -# upload arch-specific binary -"${SOS}" upload "getbin.xyz" "${SCRIPT_DIR}/output/${PROJECT}" "${PROJECT}:latest-${ARCH}" +# Create metadata file with version information +METADATA_FILE="${TEMP_DIR}/metadata.json" +cat > "${METADATA_FILE}" <= remote_version) + { + info << "Dropshell is already up to date." << std::endl; + return 0; + } + + if (!remote_version.empty() && currentver < remote_version) + { + info << "New version available, downloading..." << std::endl; + } + } + else + { + debug << "Version information not found in metadata, proceeding with download." << std::endl; + } + } + + if (!should_download) + { + return 0; + } + + // Download new version from getbin.xyz + std::string url = "https://getbin.xyz/dropshell:latest-" + arch; std::filesystem::path temp_file = local_temp_folder.path() / "dropshell"; + bool download_okay = download_file(url, temp_file); if (!download_okay) { - error << "Failed to download new version of dropshell." << std::endl; + error << "Failed to download new version of dropshell from " << url << std::endl; return -1; } // make executable chmod(temp_file.c_str(), 0755); - // check if the new version is the same as the old version - uint64_t new_hash = hash_file(temp_file); - uint64_t old_hash = hash_file(exe_path); - if (new_hash == old_hash) - { - info << "Confirmed dropshell is the latest version." << std::endl; - return 0; - } - - std::string runvercmd = exe_path.string() + " version"; - std::string currentver = _exec(runvercmd.c_str()); + // Get version of downloaded binary to double-check runvercmd = temp_file.string() + " version"; std::string newver = _exec(runvercmd.c_str()); + newver = trim(newver); + // Final version check if (currentver >= newver) { - info << "Current dropshell version: " << currentver << ", published version: " << newver << std::endl; - info << "Release version is not newer, no update needed." << std::endl; + info << "Downloaded version (" << newver << ") is not newer than current (" << currentver << ")" << std::endl; + info << "No update needed." << std::endl; return 0; } - // move the new version to the old version. - std::filesystem::rename(exe_path, exe_path.parent_path() / "dropshell.old"); + info << "Updating from version " << currentver << " to " << newver << std::endl; + + // Backup old version and replace with new + std::filesystem::path backup_path = exe_path.parent_path() / "dropshell.old"; + if (std::filesystem::exists(backup_path)) + { + std::filesystem::remove(backup_path); + } + + std::filesystem::rename(exe_path, backup_path); std::filesystem::rename(temp_file, exe_path); - // remove the old version. - std::filesystem::remove(exe_path.parent_path() / "dropshell.old"); + // Remove the backup after successful replacement + std::filesystem::remove(backup_path); - // execute the new version - execlp("bash", "bash", "-c", (exe_path.parent_path() / "dropshell").string() + "install", (char *)nullptr); + info << "Update complete. Restarting dropshell..." << std::endl; + + // Execute the new version + execlp("bash", "bash", "-c", (exe_path.string() + " install").c_str(), (char *)nullptr); error << "Failed to execute new version of dropshell." << std::endl; return -1; }