diff --git a/bootstrap.sh b/bootstrap.sh old mode 100644 new mode 100755 index 7018f62..8d3621b --- a/bootstrap.sh +++ b/bootstrap.sh @@ -41,6 +41,47 @@ pre_substep() { echo -e "\e[1m[$STEP_COUNT.$SUBSTEP_COUNT] <====== $1 =======>\e[0m" } +# Copies a file from $1 to $2. +# +# If $2 exists, checks if $1 == $2 and asks what to do. +# From there, the user providing "O" will overwrite (proceed with copy), +# "S" will skip the file and leave the destination as-is, and anyting else +# will skip the copy altogether (i.e. "S"). +copy_file() { + source_path=$1 + dest_path=$2 + + if [ ! -e "$dest_path" ]; then + cp $source_path $dest_path + echo "Copied $source_path > $dest_path" + return 0 + fi + + difference="$(diff $source_path $dest_path)" + + if [ -z "$difference" ]; then + echo "$dest_path already exists, but is the same as $source_path - skipping." + return 0 + fi + + echo "Differences detected between $source_path and $dest_path:" + diff --color $source_path $dest_path + read -p "$dest_path already exists. What should be done? [O:overwrite,S:skip] " action + + if [[ $action == "O" ]]; then + cp $source_path $dest_path + echo "Overwrote $source_path > $dest_path" + return 0 + fi + + if [[ $action == "S" ]]; then + echo "Skipped and left $dest_path as is." + return 0 + fi + + echo "Unrecognized action, doing nothing." +} + ########################################################## # # 2. Steps @@ -113,10 +154,10 @@ install_and_configure_starship() { pre_substep "Copy configuration in configuration directory" STARSHIP_CONFIGURATION_PATH=~/.config/starship.toml if [ -f $STARSHIP_CONFIGURATION_PATH ]; then - cp $STARSHIP_CONFIGURATION_PATH $STARSHIP_CONFIGURATION_PATH.old + copy_file $STARSHIP_CONFIGURATION_PATH $STARSHIP_CONFIGURATION_PATH.old fi - cp ./files/starship.toml $STARSHIP_CONFIGURATION_PATH + copy_file ./files/starship.toml $STARSHIP_CONFIGURATION_PATH } install_nvm() {