From 539de59e83e2f2cfb92c5a55a3a0da55a0b4cce5 Mon Sep 17 00:00:00 2001 From: michael Date: Fri, 18 Aug 2023 00:23:17 +0100 Subject: [PATCH] Add uninstall.sh: Update install.sh to write vars to m269 config file --- install.sh | 35 +++++++++++++++++++++++++---------- uninstall.sh | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 10 deletions(-) create mode 100755 uninstall.sh diff --git a/install.sh b/install.sh index fb3a63d..2a59558 100755 --- a/install.sh +++ b/install.sh @@ -21,6 +21,7 @@ CHECK="allowed.py m269.json" FILES="$CSS $REQS $CHECK" COURSE=m269-23j VENV=~/venvs/$COURSE +CONFIG_VARS=("VENV" "COURSE") # find out under which shell this script is running parent_shell=$(ps -o command $PPID) @@ -117,6 +118,8 @@ else cp -a $CHECK "$FOLDER" fi +CONFIG_VARS+=("FOLDER") + echo "Creating Python environment $VENV... (this will take a bit)" python3.10 -m venv --prompt $COURSE $VENV @@ -141,25 +144,37 @@ ALLOWED="python3.10 \"$FOLDER/allowed.py\" -c \"$FOLDER/m269.json\"" if [ $shell = "fish" ] then - FILE=~/.config/fish/config.fish + SHELL_CONFIG_FILE=~/.config/fish/config.fish else - FILE=~/.${shell}rc + SHELL_CONFIG_FILE=~/.${shell}rc fi +CONFIG_VARS+=("SHELL_CONFIG_FILE") + if [ $shell = "csh" ] || [ $shell = "tcsh" ] then - echo "alias $COURSE '$M269.csh'" >> $FILE - echo "alias nb '$NB'" >> $FILE - echo "alias allowed '$ALLOWED'" >> $FILE + # tee writes to the file *and* std out + echo "alias $COURSE '$M269.csh'" >> $SHELL_CONFIG_FILE + echo "alias nb '$NB'" >> $SHELL_CONFIG_FILE + echo "alias allowed '$ALLOWED'" >> $SHELL_CONFIG_FILE else if [ $shell = "fish" ] then - echo "alias $COURSE='$M269.fish'" >> $FILE + echo "alias $COURSE='$M269.fish'" >> $SHELL_CONFIG_FILE else - echo "alias $COURSE='$M269'" >> $FILE + echo "alias $COURSE='$M269'" >> $SHELL_CONFIG_FILE fi - echo "alias nb='$NB'" >> $FILE - echo "alias allowed='$ALLOWED'" >> $FILE + echo "alias nb='$NB'" >> $SHELL_CONFIG_FILE + echo "alias allowed='$ALLOWED'" >> $SHELL_CONFIG_FILE fi -echo "All done. Go to $SITE for further instructions." \ No newline at end of file +M269_CONFIG_FILE=$FOLDER/.m269rc +CONFIG_VARS+=("M269_CONFIG_FILE") + +# Write the name=value pairs to m269 config file +for var_name in "${CONFIG_VARS[@]}"; do + var_value="${!var_name}" + echo "$var_name=$var_value" >> "$M269_CONFIG_FILE" +done + +echo "All done. Go to $SITE for further instructions." diff --git a/uninstall.sh b/uninstall.sh new file mode 100755 index 0000000..ee52b4b --- /dev/null +++ b/uninstall.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +# Confirm uninstallation +echo "Warning: After uninstallation, you will need a Jupyter environment to open and run your notebooks." +read -p "Proceed with uninstallation? [y/N] " response +case $response in + [yY]) + ;; + *) + exit 1 + ;; + esac + +# Assume variables VENV, COURSE, FOLDER, SHELL_CONFIG_FILE, +# will be in the m269 config file +source .m269rc +if [[ $? -ne 0 ]]; then + echo "Failed to source the configuration file " + exit 1 +fi + +# Remove the virtual environment +echo "Removing the virtual environment..." +rm -rf "$VENV" + +# Remove allowed.py and m269.json from current directory +echo "Removing allowed.py and m269.json from $FOLDER... " +rm -f $FOLDER/allowed.py $FOLDER/m269.json + +# Remove aliases from the configuration file +echo "Removing shortcut commands from $SHELL_CONFIG_FILE..." + +ALIASES=("alias nb" "alias allowed" "alias $COURSE") + +cp "$SHELL_CONFIG_FILE" "$SHELL_CONFIG_FILE".backup +for alias in "${ALIASES[@]}"; do + sed -i "/^$alias/d" "$SHELL_CONFIG_FILE" +done + +# Remove lines from ~/.jupyter/custom/custom.css + +# Special characters need to be escaped for use in regex +START_DELIM="\/\* Start of M269-23J notebook styling. \*\/" +END_DELIM="\/\* End of M269-23J notebook styling. \*\/" +CSS_FILE=~/.jupyter/custom/custom.css + +cp "$CSS_FILE" "$CSS_FILE".backup +echo "Removing custom styling from $CSS_FILE ..." +sed -i "/$START_DELIM/,/$END_DELIM/d" "$CSS_FILE"