Skip to content

Commit

Permalink
Adding makeReady script and necessary documents
Browse files Browse the repository at this point in the history
  • Loading branch information
PatHurley committed Jun 27, 2020
1 parent 0cbb8bb commit 7864d74
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
51 changes: 51 additions & 0 deletions bin/.scriptResources/template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash
# -----------------------------------------------------------------------------
# Written by: <author>
# <description>
# -----------------------------------------------------------------------------

VERSION=0.1.0
USAGE () {
printf "Usage: $(basename $0) [OPTIONS]... [ARGUMENTS]...
<short description>
OPTIONS.
-h, --help display this help and exit
--version output version information and exit
<further_information>
Exit status:
0 if OK,
1 if minor issue (e.g. cannot parse input),
2 if serious issue (e.g. cannot access file/resource).
<sources>
(i.e. GitHub link, online documentation, etc.)\n"
}

# --- Options processing ------------------------------------------------------

while [[ "$1" =~ ^-.* ]]; do
case $1 in
-h | --help ) shift
USAGE
exit 0
;;
--version ) shift
printf "Script Version:\t$VERSION"
exit 0
;;
*) shift
printf "\n [!] Invalid option $1\n\n"
USAGE
exit 1
;;
esac; shift
done

# --- Body --------------------------------------------------------------------

# SCRIPT LOGIC GOES HERE

exit 0
6 changes: 6 additions & 0 deletions bin/ideas.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1. Auto-update function Write any script but include a function that checks itself for updates.

Example:
Host the script on github and have it check the md5sum of the current file being run against the md5sum of the script on github.
If the file is changed, show a prompt asking the user if he wants to update.
If yes download the latest script and replace the current one.
101 changes: 101 additions & 0 deletions bin/makeReady
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/bin/bash
# -----------------------------------------------------------------------------
# Written by: P.Hurley
# Generates a script using a template and gives it "a+x" permissions.
# -----------------------------------------------------------------------------

VERSION=0.2.0
USAGE () {
printf "Usage: $(basename $0) [OPTIONS]... [ARGUMENTS]...
Takes a string as the only argument and generates a file with that name.
Writes a predefined template to the new file and adds\"a+x\" permissions
automatically.
OPTIONS
-R, --recursive if fileName contains a directory that does not exist,
create the directory and any required parent directory.
-h, --help display this help and exit
--version output version information and exit
ARGUMENTS
\$1 fileName string, used as the name for the file
i.e.
\$ $(basename $0) wonderScript
Exit status:
0 if OK,
1 if minor issue (e.g. cannot parse input),
2 if serious issue (e.g. cannot access file/resource).\n"
}

# --- Options processing ------------------------------------------------------

while [[ "$1" =~ ^-.* ]]; do
case $1 in
-h | --help ) shift
USAGE
exit 0
;;
-i | --ignore ) shift
ignore=true # will not compare LOCAL template.txt md5 to REFERENCE md5 for template.txt
;;
-R | --recursive ) shift
recursive=true # cannot put recursive directory logic here, scriptName is not yet defined
;;
--version ) shift
printf "Script Version:\t$VERSION"
exit 0
;;
*) shift
printf "\n [!] Invalid option $1\n\n"
USAGE
exit 1
;;
esac;
done

# --- Arguments processing ----------------------------------------------------

if [ $1 == "" ]; then
printf "ERROR: Not enough arguments, please provide a string for script name.\n\n"
USAGE; exit 1
elif [ $2 ]; then
printf "ERROR: Too many arguments, please provide only 1 string for script name.\n\n"
USAGE; exit 1
else
scriptName=$1
fi

# --- Body --------------------------------------------------------------------

templatePath=$(find ~ -name "template.txt")
reference_md5="ee235890d98700432abb17988ad95720" # reference MD5 hash of template.txt

# template.txt check
if [ "$ignore" != true ]; then
if [ -f $templatePath ]; then
local_md5=$(md5sum $templatePath | cut -d " " -f 1)
else
printf "ERROR: cannot find template.txt file!\n"
exit 2
fi

if [ "$reference_md5" != "$local_md5" ]; then
printf "WARN: local template.txt does not match reference template.txt continue? [y/n]"; read continue
if [[ ! "$continue" =~ ^(y)$ ]]; then
exit 0
fi
fi
fi

# recursive directory logic
if [ "$recursive" == true ] && [ ! -d "$(dirname "$scriptName")" ]; then
mkdir -p "$(dirname "$scriptName")"
fi

touch "$scriptName"
cat "$templatePath" > "$scriptName"
chmod a+x "$scriptName"

exit 0

0 comments on commit 7864d74

Please sign in to comment.