97 lines
3.0 KiB
Bash
97 lines
3.0 KiB
Bash
# ------------------------------------------------------------------------------
|
|
# File manipulation function
|
|
# This file is part of the init.sh project
|
|
# Copyright (c) 2019-2021 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
|
# ------------------------------------------------------------------------------
|
|
# This file is distributed under 3-clause BSD license.
|
|
# The complete license agreement can be obtained at:
|
|
# https://opensource.org/licenses/BSD-3-Clause
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Define normalised time display, filename friendly
|
|
stdtime()
|
|
{
|
|
date --rfc-3339=seconds | sed -e 's/ /-/' -e 's/://g'
|
|
}
|
|
export -f stdtime
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Backup original installation files
|
|
# (or any old files if runned several time on same file)
|
|
backupdist()
|
|
{
|
|
[[ $# -lt 1 ]] &&
|
|
prnt E "backupdist(): At least one argument is required." &&
|
|
exit 11
|
|
|
|
for file in $@; do
|
|
if [[ -f ${file} ]]; then
|
|
local tmstmp=$(stdtime)
|
|
prnt I "Création d'une sauvegarde de ${file} du $tmstmp..."
|
|
cp -av $file ${file}.dist.${tmstmp}
|
|
if [[ $? -ne 0 ]]; then
|
|
prnt E "backupdist(): Échec de copie du fichier."
|
|
die 12
|
|
fi
|
|
else
|
|
prnt E "backupdist(): Le paramètre fournis ($file) n'est pas un fichier."
|
|
die 7
|
|
fi
|
|
done
|
|
}
|
|
export -f backupdist
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Install file to the host (specific first then general)
|
|
# Todo: implement wildcard support
|
|
installfile()
|
|
{
|
|
local filelist=""
|
|
local i=0
|
|
|
|
[[ $# -lt 2 ]] && (
|
|
prnt E "installfile(): At least two arguments are required."
|
|
die 11
|
|
)
|
|
[[ $(echo $@ | grep "\*\|\?") ]] && (
|
|
prnt E "installfile(): Wildcards are not authorized."
|
|
die 7
|
|
)
|
|
|
|
for arg in $@; do
|
|
if [[ -f $MYPATH/repo/hosts/$HOSTNAME/$arg ]]; then
|
|
filelist="$filelist $MYPATH/repo/hosts/$HOSTNAME/$arg"
|
|
elif [[ -f $MYPATH/repo/common/$arg ]]; then
|
|
filelist="$filelist $MYPATH/repo/common/$arg"
|
|
else
|
|
# Not found in repository, we expect full name
|
|
filelist="$filelist $arg"
|
|
fi
|
|
done
|
|
for i in $filelist; do :; done
|
|
if [[ ! $i == /* ]]; then
|
|
prnt E "installfile(): Target must be on the root filesystem and fuul path must be provided."
|
|
die 13
|
|
fi
|
|
if [[ -d $(dirname $i) ]]; then
|
|
prnt I "Création du répertoire $(dirname $i) d'accueil..."
|
|
mkdir -pv $(dirname $i)
|
|
if [[ $? -ne 0 ]]; then
|
|
prnt E "installfile(): Can't create target dirrectory!"
|
|
die 12
|
|
fi
|
|
prnt I "Copie des fichiers ${filelist}..."
|
|
cp -av $filelist
|
|
if [[ $? -ne 0 ]]; then
|
|
prnt E "installfile(): Couldn't copy some required files!" &&
|
|
die 12
|
|
fi
|
|
}
|
|
export -f installfile
|
|
|
|
# EOF
|