Create target directory if required

Remove hardcoded target directory
This commit is contained in:
Pratik Tripathy
2021-07-06 20:42:18 +05:30
parent 2a4d72f10f
commit 926831ad2b

View File

@@ -1,20 +1,19 @@
#!/usr/bin/env bash #!/usr/bin/env bash
usage() { usage() {
if [ -n "$1" ]; then if [ -n "$1" ]; then
echo "" echo ""
echo -e "${CRED}$1${CEND}\n" echo -e "${CRED}$1${CEND}\n"
fi fi
echo "Applies all settings stored in the script's directory to your home directory" echo "Applies all settings stored in the script's directory to your home directory"
echo "" echo ""
echo "Usage: $0 [-q|--quiet] [-l|--create-links]" echo "Usage: $0 [-q|--quiet] [-l|--create-links]"
echo " -q, --quiet No screen outputs" echo " -q, --quiet No screen outputs"
echo " -l, --create-links Creates soft-links to files in the current directory instead of copying them" echo " -l, --create-links Creates soft-links to files in the current directory instead of copying them"
echo "" echo ""
echo "Example: bash ./$0 -q --create-links" echo "Example: bash ./$0 -q --create-links"
} }
################################## ##################################
@@ -26,49 +25,51 @@ QUIET="n"
CREATE_LINKS="n" CREATE_LINKS="n"
while [[ "${#}" -gt 0 ]]; do while [[ "${#}" -gt 0 ]]; do
case $1 in case $1 in
-q|--quiet) -q | --quiet)
QUIET="y" QUIET="y"
shift shift
;; ;;
-l|--create-links) -l | --create-links)
CREATE_LINKS="y" CREATE_LINKS="y"
shift shift
;; ;;
-h|--help) -h | --help)
echo echo
usage usage
echo echo
exit 0 exit 0
;; ;;
*) *)
usage "Unknown parameter passed: $1" "h" usage "Unknown parameter passed: $1" "h"
exit 1 exit 1
;; ;;
esac esac
done done
main () { main() {
TS=$(date '+%d_%m_%Y-%H_%M_%S') TS=$(date '+%d_%m_%Y-%H_%M_%S')
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE")" > /dev/null 2>&1 && pwd)"
find . -type f ! -name "$0" ! -path '*/.idea/*' ! -path '*/.git/*' ! -name 'LICENSE' ! -name 'README.md' -print0 | while IFS= read -r -d '' file find . -type f ! -name "$0" ! -path '*/.idea/*' ! -path '*/.git/*' ! -name 'LICENSE' ! -name 'README.md' -print0 | while IFS= read -r -d '' file; do
do
# Replaces `.` with `~` in the found file names # Replaces `.` with `~` in the found file names
# target_file="${file/./~}" target_file="${file/./~}"
target_file="${file/.//media/pratik/Projects/Code/dotfiles-test}"
if [[ -f "${target_file}" ]] if [[ -f "${target_file}" ]]; then
then
mv "$target_file" "${target_file}_${TS}" && [[ "$QUIET" == "n" ]] && echo "Existing setting renamed to ${target_file}_${TS}" mv "$target_file" "${target_file}_${TS}" && [[ "$QUIET" == "n" ]] && echo "Existing setting renamed to ${target_file}_${TS}"
fi fi
target_directory=$(dirname "${target_file}")
if [[ ! -d "${target_directory}" ]]; then
mkdir -p "${target_directory}" && [[ "$QUIET" == "n" ]] && echo "Directory ${target_directory} created"
fi
if [[ "$CREATE_LINKS" == "y" ]]; then if [[ "$CREATE_LINKS" == "y" ]]; then
ln -s "${file/./${SCRIPT_DIR}}" "${target_file}" && [[ "$QUIET" == "n" ]] && echo "Linked ${target_file}" ln -s "${file/./${SCRIPT_DIR}}" "${target_file}" && [[ "$QUIET" == "n" ]] && echo "Linked ${target_file}"
else else
cp "${file/./${SCRIPT_DIR}}" "${target_file}" && [[ "$QUIET" == "n" ]] && echo "Copied ${target_file}" cp "${file/./${SCRIPT_DIR}}" "${target_file}" && [[ "$QUIET" == "n" ]] && echo "Copied ${target_file}"
fi fi
done; done
} }
main "$@" main "$@"