Files
profile/profile.d/updates.sh
2025-06-19 14:40:02 +02:00

136 lines
4.7 KiB
Bash

#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
# Protected by the BSD3 license. Please read bellow for details.
#
# * Redistribution and use in source and binary forms,
# * with or without modification, are permitted provided
# * that the following conditions are met:
# *
# * Redistributions of source code must retain the above
# * copyright notice, this list of conditions and the
# * following disclaimer.
# *
# * Redistributions in binary form must reproduce the above
# * copyright notice, this list of conditions and the following
# * disclaimer in the documentation and/or other materials
# * provided with the distribution.
# *
# * Neither the name of the copyright holder nor the names
# * of any other contributors may be used to endorse or
# * promote products derived from this software without
# * specific prior written permission.
# *
# * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# * OF SUCH DAMAGE.
# ------------------------------------------------------------------------------
export BASE_URL="https://git.geoffray-levasseur.org/fatalerrors/profile"
export UPDT_URL="$BASE_URL/raw/branch/master"
export ARCH_URL="$BASE_URL/archive/master.tar.gz"
# ------------------------------------------------------------------------------
# Check for profile updates
# ------------------------------------------------------------------------------
check_updates()
{
if [[ $1 == "-q" ]]; then
# Quiet mode is mostly used internally when profile_upgrade is called
quiet=1
fi
disp I "Checking for updates..."
local vfile="/tmp/version"
wget "$UPDT_URL/version" -O $vfile >/dev/null 2>&1 || {
disp E "Can't download version file, impossible to proceed!"
return 5
}
if [[ -s $vfile ]]; then
local lastver=$(cat $vfile)
if [[ $lastver != $PROFVERSION ]]; then
disp I "You have version $PROFVERSION installed. Version $lastver is available."
[[ $quiet ]] && disp I "You should upgrade to last version when possible."
result=1
else
disp I "Your version is up-to-date."
result=0
fi
rm -f $vfile
else
disp E "Impossible to read temporary file, impossible to proceed."
fi
unset lastver vfile
return $result
}
# ------------------------------------------------------------------------------
# Apply update to profile
# ------------------------------------------------------------------------------
profile_upgrade()
{
check_updates -q
local need_update=$?
[[ $need_update -ne 1 ]] && {
disp "No update available."
return 0
}
if [[ -s $MYPATH/profile.sh ]]; then
disp E "Installation path detection failed, cannot upgrade automatically."
return 1
fi
if [[ -d $MYPATH/.git ]]; then
disp I "Git installation detected, applying git pull."
local curdir=$(pwd)
cd $MYPATH
git pull || {
disp E "Git pull failed, upgrade not applyed."
cd "$curdir"
return 2
}
disp I "Successfully upgraded using git."
cd "$curdir"
else
disp I "No Git detected. Downloading and applying upgrade from archive..."
local tmpdir="/tmp/profile_upg.$$"
mkdir -p "$tmpdir" || {
disp E "Failed to create temporary directory."
return 4
}
local archive="$tmpdir/profile.tar.gz"
wget -q "$ARCH_URL" -O "$archive" || {
disp E "Failed to download archive."
rm -rf "$tmpdir"
return 5
}
tar -xzf "$archive" -C "$tmpdir" || {
disp E "Archive extraction failed."
rm -rf "$tmpdir"
return 6
}
disp I "Installing new version..."
cp -r "$tmpdir"/profile/* "$MYPATH"/ || {
disp E "Failed to copy new files to $MYPATH."
rm -rf "$tmpdir"
return 7
}
disp I "Upgrade complete. You should now logout and login again."
rm -rf "$tmpdir"
fi
}
# EOF