-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasen
executable file
·87 lines (72 loc) · 2.04 KB
/
easen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#! /bin/sh
#-- easen --#
# Scripts easen one's life.
# Why not easen the job of making a script
# by making another script?
# ~ ADIGEN
### Usage ###
if [ $# = 0 ]; then
cat <<EOF
Usage: $(basename "${0}") script_name
script_name The name of the script to be created.
If the script already exists, it will be edited.
EOF
exit 0
fi
#############
### Variables ###
SCRIPTS_DIR="${HOME}/.local/bin"
AUTHOR="ADIGEN"
SCRIPT_EDITOR="${EDITOR:-vim}"
SCRIPT="${SCRIPTS_DIR}/${1}"
# TEMPLATE="#! /bin/sh
#
# #-- ${1} --#
# # Scripts easen one's life.
# # But we don't know much about what this one does.
# # (DESCRIPTION NOT PROVIDED)
# # Dependencies:
# # - NOT_SET
# # ~ ${AUTHOR}"
TEMPLATE="#! /bin/sh
_usage() {
cat <<EOF
NAME
${1} - DESCRIPTION NOT PROVIDED
USAGE
${1}
DESCRIPTION
Scripts easen one's life.
But we don't know much about what this one does.
(DESCRIPTION NOT PROVIDED)
DEPENDENCIES
- NOT_SET
AUTHOR
${AUTHOR} (https://aadivishnu.com)
EOF
}
case "\$\1" in
-h|h|help|--help)
_usage
exit
;;
esac"
#################
# Check if the script already exists.
# If no, copy the template to the script.
# The double quotes around the template are necessary to
# preserve the newlines in the template.
[ ! -f "${SCRIPTS_DIR}/${1}" ] && echo "${TEMPLATE}" > "${SCRIPT}" && new_script=1
# Make it executable.
chmod +x "${SCRIPT}"
# Edit the script.
${SCRIPT_EDITOR} "${SCRIPT}"
# If the file is the exact same as the template, just delete it.
[ "$(cat ${SCRIPT})" = "${TEMPLATE}" ] && rm "${SCRIPT}" && echo "Deleted ${SCRIPT} because it was the same as the template." && exit 0
# Exit if it is not a new script since we don't need to prompt it's addition to dotfiles.
[[ -z $new_script ]] && exit 0
# If it hasn't exited, meaning the file was saved, prompt the user to add it to their dotfiles.
echo "Add to dotfiles?"
read -st 3 -n 1 ans
#[[ $ans = 'y' ]] && git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME add ${SCRIPT}
[[ $ans = 'y' ]] && git --git-dir=$HOME/.local/bin/.git --work-tree=$HOME/.local/bin add ${SCRIPT}