refactored recursive directory file install function

This commit is contained in:
pyr0ball 2023-04-26 13:33:18 -07:00
parent 5efe466c06
commit e419522fbd

View file

@ -244,38 +244,51 @@ restore-backup(){
install-file(){ install-file(){
local _source="$1" local _source="$1"
local _destination="$2" local _destination="$2"
installed_files+=("${_destination}/${_source##*/}") local _source_root="$3"
local _filename=${_source##*/}
local _destination_file=${_destination}/${_filename#${_source_root}}
installed_files+=("${_destination_file}")
if [[ $update_run == true ]] ; then if [[ $update_run == true ]] ; then
boxline "PRbL updater: added file ${_destination}/${_source##*/} to list" boxline "$scriptname: added file ${_destination_file} to list"
else else
if [[ $dry_run == true ]] ; then if [[ $dry_run == true ]] ; then
boxline "DryRun: cp $_source $_destination" boxline "DryRun: cp -p $_source $_destination_file"
else else
cp $_source $_destination && boxline "Installed ${_source##*/}" || warn "Unable to install ${_source##*/}" cp -p $_source $_destination_file && boxline "Installed ${_filename}" || warn "Unable to install ${_filename}"
fi fi
echo "${_destination}/${_source##*/}" >> $rundir/installed_files.list echo "${_destination_file}" >> $rundir/installed_files.list
fi fi
} }
install-dir() { install-dir() {
local _source="$1" local _source="$1"
local _destination="$2" local _destination="$2"
installed_dirs+=$_source installed_dirs+=("$_source -> $_destination")
echo "$_destination" >> $rundir/installed_dirs.list # Iterate through all files in the source directory recursively
# Install the current directory while IFS= read -r -d '' source_file; do
if [[ $update_run == true ]] ; then # Construct the destination file path by removing the source directory path
boxline "PRbL updater: added directory ${_destination}/${_source##*/} to list" # and appending it to the destination directory path
else local _filename="${source_file#${_source}}"
# Loop through subdirectories local destination_file="${_destination}/${source_file#${_source}}"
for item in "$_source"/*; do # Create the parent directory of the destination file if it doesn't exist
if [[ -d "$item" ]]; then # Log the destination file path to the logfile
# If item is a directory, recursively install it #echo "$destination_file" >> "$logfile"
install-dir "$item" "$_destination" installed_files+=($destination_file)
if [[ $update_run == true ]] ; then
boxline "$scriptname: added file ${destination_file} to list"
else else
install-file "$item" "$_destination" if [[ $dry_run == true ]] ; then
done # Create the destination directory if it doesn't exist
boxline "DryRun: mkdir -p $(dirname $destination_file)"
boxline "DryRun: cp -p ${_source}${_filename} $destination_file"
else
# Create the destination directory if it doesn't exist
mkdir -p "$(dirname "$destination_file")"
cp -p ${_source}${_filename} $destination_file && boxline "Installed ${_filename}" || warn "Unable to install ${_filename}"
fi
echo "${destination_file}" >> $rundir/installed_files.list
fi fi
fi done < <(find "$_source" -type f -print0)
} }
# install-dir(){ # install-dir(){
@ -308,13 +321,14 @@ userinstall(){
fi fi
# Copy functions first # Copy functions first
install-file ${rundir}/PRbL/functions ${installdir}/functions install-file ${rundir}/PRbL/functions ${installdir}
# Copy bashrc scripts to home folder # Copy bashrc scripts to home folder
#cp -r ${rundir}/lib/skel/* $HOME/ #cp -r ${rundir}/lib/skel/* $HOME/
for file in "$rundir"/lib/skel/* ; do install-dir ${rundir}/lib/skel/ $HOME
install-dir $file $HOME # for file in $(ls -a -I . -I .. ${rundir}/lib/skel/) ; do
done # install-dir ${rundir}/lib/skel/$file $HOME
# done
# Check for dependent applications and warn user if any are missing # Check for dependent applications and warn user if any are missing
if ! check-deps ; then if ! check-deps ; then
@ -334,7 +348,7 @@ userinstall(){
# If vim is installed, add config files for colorization and expandtab # If vim is installed, add config files for colorization and expandtab
if [[ $viminstall != null ]] ; then if [[ $viminstall != null ]] ; then
mkdir -p ${HOME}/.vim/colors mkdir -p ${HOME}/.vim/colors
install-file $rundir/lib/vimfiles/crystallite.vim ${HOME}/.vim/colors/crystallite.vim install-file $rundir/lib/vimfiles/crystallite.vim ${HOME}/.vim/colors
take-backup $HOME/.vimrc take-backup $HOME/.vimrc
install-file $rundir/lib/vimfiles/vimrc.local $HOME/.vimrc install-file $rundir/lib/vimfiles/vimrc.local $HOME/.vimrc
fi fi
@ -401,13 +415,10 @@ globalinstall(){
# If the selected user is set to true # If the selected user is set to true
if [[ "${result[idx]}" == "true" ]] ; then if [[ "${result[idx]}" == "true" ]] ; then
#cp -r ${rundir}/lib/skel/* /etc/skel/ #cp -r ${rundir}/lib/skel/* /etc/skel/
for file in `find ${rundir}/lib/skel/ -print | tail -n +2` ; do install-dir ${rundir}/lib/skel/ /home/${selecteduser}/
if [[ -d "$file" ]] ; then # for file in $(ls -a -I . -I .. ${rundir}/lib/skel/) ; do
install-dir $file /home/${selecteduser} # install-dir ${rundir}/lib/skel/$file $HOME
else # done
install-file $file /home/${selecteduser}
fi
done
if [[ $(cat /home/${selecteduser}/.bashrc | grep -c prbl) == 0 ]] ; then if [[ $(cat /home/${selecteduser}/.bashrc | grep -c prbl) == 0 ]] ; then
take-backup /home/${selecteduser}/.bashrc take-backup /home/${selecteduser}/.bashrc
echo -e "$bashrc_append" >> /home/${selecteduser}/.bashrc && boxborder "bashc.d installed..." || warn "Malformed append on ${lbl}/home/${selecteduser}/.bashrc${dfl}. Check this file for errors" echo -e "$bashrc_append" >> /home/${selecteduser}/.bashrc && boxborder "bashc.d installed..." || warn "Malformed append on ${lbl}/home/${selecteduser}/.bashrc${dfl}. Check this file for errors"
@ -422,7 +433,7 @@ globalinstall(){
detectvim detectvim
if [[ $viminstall != null ]] ; then if [[ $viminstall != null ]] ; then
install-file $rundir/lib/vimfiles/crystallite.vim /usr/share/vim/${viminstall}/colors/crystallite.vim install-file $rundir/lib/vimfiles/crystallite.vim /usr/share/vim/${viminstall}/colors
take-backup /etc/vim/vimrc.local take-backup /etc/vim/vimrc.local
install-file $rundir/lib/vimfiles/vimrc.local /etc/vim/vimrc.local install-file $rundir/lib/vimfiles/vimrc.local /etc/vim/vimrc.local
fi fi