sapphrc/lib/sapphrc/sapphrc-profile
2024-05-30 23:17:14 -06:00

79 lines
2.1 KiB
Bash

#!/bin/bash
sapphrc_pcreate(){
mkdir -p "${sapphrc_profile_rcdir}"
touch "${sapphrc_profile}/"{include,exclude}
echo '#!/bin/bash'|tee "${sapphrc_profile_rc}" > /dev/null
echo ".sapphrc/${PROFILE_NAME}"|tee "${sapphrc_profile}/include" > /dev/null
chmod +x "${sapphrc_profile_rc}"
echo "Successfully created '${PROFILE_NAME}'"
sapphrc_join "${PROFILE_NAME}"
}
sapphrc_premove(){
echo "WARNING: really remove profile '${PROFILE_NAME}'?"
echo "~${sapphrc_profile#$HOME} and all its contents will be deleted!"
read -p "Confirm removal (y/N): " confirm_remove
if [[ "${confirm_remove}" =~ ^([yY][eE][sS]|[yY])$ ]];then
sapphrc_pleave
echo "removing '${PROFILE_NAME}'"
rm -r "${sapphrc_profile}"
echo "Profile '${PROFILE_NAME}' has been removed. Be sure to leave the profile on other machines/accounts to avoid synchronization issues."
fi
}
sapphrc_pjoin(){
if grep -q ^"${PROFILE_NAME}"$ "${sapphrc_config}/active_profiles";then
echo "Already joined ${PROFILE_NAME}, nothing to do."
else
echo "${PROFILE_NAME}"|tee -a "${sapphrc_config}/active_profiles" > /dev/null
echo "Successfully joined '${PROFILE_NAME}'!"
fi
}
sapphrc_pleave(){
if grep -q ^"${PROFILE_NAME}"$ "${sapphrc_config}/active_profiles";then
sed -i '/^'"${PROFILE_NAME}"'$/d' "${sapphrc_config}/active_profiles"
[ -e "${HOME}/.sapphrc/${PROFILE_NAME}" ] && rm "${HOME}/.sapphrc/${PROFILE_NAME}"
echo "Successfully left '${PROFILE_NAME}'!"
else
echo "You have not joined '${PROFILE_NAME}', nothing to do."
fi
}
sapphrc_plist(){
echo "${bold}Active profiles:${normal}"
cat "${sapphrc_config}/active_profiles"
echo "${bold}Available profiles:${normal}"
ls "${sapphrc_config}/backups/" | cat
}
sapphrc_profile(){
PROFILE_ACTION="${1}"
case "$PROFILE_ACTION" in
create|remove|join|leave)
validate_backupdir
shift 1
validate_profile $@
;;&
create)
sapphrc_pcreate $@
;;
remove)
sapphrc_premove $@
;;
join)
sapphrc_pjoin $@
;;
leave)
sapphrc_pleave $@
;;
list)
sapphrc_plist
;;
*|usage)
usage
;;
esac
}