Created
November 30, 2017 11:48
-
-
Save kou1okada/74b9693f6028544f9edc4dafb7506116 to your computer and use it in GitHub Desktop.
cyghome - Cygwin HOME directory utility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# cyghome - Cygwin HOME directory utility | |
# Copyright (c) 2017 Koichi OKADA. All rights reserved. | |
# This script is distributed under the MIT license. | |
# | |
(( 5 <= DEBUG )) && set -x | |
function help () | |
{ | |
cat<<-EOD | |
Usage: ${SCRIPT_NAME} [options] COMMAND | |
Options: | |
-h, --help | |
-t TARGET default: "${TARGET}" | |
Commands: | |
setup Move HOME to TARGET and link TARGET to HOME | |
resetup Link TARGET to HOME | |
restore Move TARGET to HOME | |
ln FILES ... Link FILES to HOME/ | |
EOD | |
} | |
SCRIPT_FILE="${0##*/}" | |
SCRIPT_NAME="${SCRIPT_FILE%.*}" | |
USERPROFILE="$(cygpath -u "$USERPROFILE")" | |
TARGET="$USERPROFILE/HOME" | |
PWD="$(realpath .)" | |
ARGS=() | |
while (( 0 < $# )); do | |
case "$1" in | |
-h|--help) | |
OPT_H="$1" | |
shift | |
;; | |
-t) | |
TARGET="$2" | |
shift 2 | |
;; | |
*) | |
ARGS+=( "$1" ) | |
shift | |
;; | |
esac | |
done | |
set -- "${ARGS[@]}" | |
if [ -n "$OPT_H" ] || (( $# < 1 )); then | |
help | |
exit | |
fi | |
TARGET="$(cygpath -u "$TARGET")" | |
function mtime () | |
{ | |
find "$1" -type f -or -type l -exec date -r {} +"%Y%m%d_%H%M%S" \; \ | |
| sort | tail -n1 | |
} | |
function cyghome_setup () | |
{ | |
if [ -e "$TARGET" ]; then | |
cat<<-EOD >&2 | |
Error: TARGET already exists. | |
EOD | |
exit 1 | |
fi | |
if [ -e "$HOME/USERPROFILE" -a ! -L "$HOME/USERPROFILE" ]; then | |
cat<<-EOD >&2 | |
Error: "$HOME/USERPROFILE" exists but it is not symbolic link. | |
EOD | |
exit 1 | |
fi | |
cat<<-EOD | |
mv -v "$HOME" "$TARGET" | |
ln -v -s "$TARGET" "$HOME" | |
[ ! -e "$HOME/USERPROFILE" ] && ln -v -s "$USERPROFILE" "$HOME/USERPROFILE" | |
EOD | |
} | |
function cyghome_resetup () | |
{ | |
if [ ! -e "$TARGET" ]; then | |
cat<<-EOD >&2 | |
Error: TARGET does not exist. | |
EOD | |
exit 1 | |
fi | |
cat<<-EOD | |
mv -v "$HOME" "${HOME},$(mtime "$HOME")" | |
ln -v -s "$TARGET" "$HOME" | |
EOD | |
} | |
function cyghome_restore () | |
{ | |
if [ "${PWD:0:${#TARGET}}" = "$TARGET" ]; then | |
cat<<-EOD >&2 | |
Error: Current directry is on TARGET. | |
Move to out of "$TARGET", before execute "${SCRIPT_FILE}". | |
EOD | |
exit 1 | |
fi | |
if [ ! -e "$TARGET" ]; then | |
cat<<-EOD >&2 | |
Error: TARGET does not exist. | |
EOD | |
exit 1 | |
fi | |
cat<<-EOD | |
mv -v "$HOME" "${HOME},$(mtime "$HOME")" | |
mv -v "$TARGET" "$HOME" | |
EOD | |
} | |
function cyghome_ln () | |
{ | |
local i name | |
for i; do | |
if [ ! -e "$i" ]; then | |
cat<<-EOD >&2 | |
Warning: "$i" does not exist. Skip. | |
EOD | |
continue | |
fi | |
name="${i##*/}" | |
cat<<-EOD | |
[ ! -e "$HOME/$name" ] && ln -v -s "$(realpath "$i")" "$HOME/$name" | |
EOD | |
done | |
} | |
if [ "${PWD:0:${#HOME}}" = "$HOME" ]; then | |
cat<<-EOD >&2 | |
Error: Current directry is on HOME. | |
Move to out of "$HOME", before execute "${SCRIPT_FILE}". | |
EOD | |
exit 1 | |
fi | |
SUBCMD="$1" | |
shift | |
CMD="${SCRIPT_NAME}_${SUBCMD}" | |
if ! type "$CMD" >& /dev/null; then | |
cat<<-EOD >&2 | |
Error: "$SUBCMD" is not valid subcommand. | |
EOD | |
exit 1 | |
fi | |
"$CMD" "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment