#!/bin/sh
#
# This file is part of OpenMediaVault.
#
# @license   http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author    Volker Theile <volker.theile@openmediavault.org>
# @copyright Copyright (c) 2009-2026 Volker Theile
#
# OpenMediaVault is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# OpenMediaVault is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenMediaVault. If not, see <http://www.gnu.org/licenses/>.

. /etc/default/openmediavault

OMV_UPGRADE_RELEASENAME=${OMV_UPGRADE_RELEASENAME:-"Synchrony"}
OMV_UPGRADE_APTSOURCESLIST=${OMV_UPGRADE_APTSOURCESLIST:-"/etc/apt/sources.list.d/openmediavault.list"}
OMV_UPGRADE_DEBIAN_RELEASENAME_CURRENT=${OMV_UPGRADE_DEBIAN_RELEASENAME_CURRENT:-"bookworm"}
OMV_UPGRADE_DEBIAN_RELEASENAME_NEXT=${OMV_UPGRADE_DEBIAN_RELEASENAME_NEXT:-"trixie"}

# Upgrade OpenMediaVault source list file.
relname=$(echo ${OMV_UPGRADE_RELEASENAME} | tr "[:upper:]" "[:lower:]" | tr -d "[:space:]")
cat <<EOF > ${OMV_UPGRADE_APTSOURCESLIST}
deb [signed-by=/usr/share/keyrings/openmediavault-archive-keyring.gpg] http://packages.openmediavault.org/public ${relname} main
deb [signed-by=/usr/share/keyrings/openmediavault-archive-keyring.gpg] http://openmediavault.github.io/packages/ ${relname} main

## Uncomment the following line to add software from the proposed repository.
# deb [signed-by=/usr/share/keyrings/openmediavault-archive-keyring.gpg] http://packages.openmediavault.org/public ${relname}-proposed main
# deb [signed-by=/usr/share/keyrings/openmediavault-archive-keyring.gpg] http://openmediavault.github.io/packages/ ${relname}-proposed main

## This software is not part of OpenMediaVault, but is offered by third-party
## developers as a service to OpenMediaVault users.
# deb [signed-by=/usr/share/keyrings/openmediavault-archive-keyring.gpg] http://packages.openmediavault.org/public ${relname} partner
# deb [signed-by=/usr/share/keyrings/openmediavault-archive-keyring.gpg] http://openmediavault.github.io/packages/ ${relname} partner
EOF

# Remove the OS security repository.
rm -f /etc/apt/sources.list.d/openmediavault-debian-security.list
rm -f /etc/apt/sources.list.d/openmediavault-os-security.list

if [ -e "/etc/apt/sources.list" ]; then
  # Upgrade Debian source list file.
	sed -i -r "s/${OMV_UPGRADE_DEBIAN_RELEASENAME_CURRENT}(-lts)?/${OMV_UPGRADE_DEBIAN_RELEASENAME_NEXT}/g" /etc/apt/sources.list
	# Remove and uncomment the OS security repository.
  sed -i -e '/^[^#].+security.debian.org/s/^/#/' /etc/apt/sources.list
  # Uncomment the Debian backports repository.
  sed -i -r "/^[^#].+${OMV_UPGRADE_DEBIAN_RELEASENAME_CURRENT}-backports/s/^/#/" /etc/apt/sources.list
fi

# Modify default release.
if [ -e "/etc/apt/apt.conf" ]; then
	sed -i -r "s/${OMV_UPGRADE_DEBIAN_RELEASENAME_CURRENT}(-lts)?/${OMV_UPGRADE_DEBIAN_RELEASENAME_NEXT}/g" /etc/apt/apt.conf
fi

exit 0
