From 7864d746f4c54fa7d762b909c335163509898b77 Mon Sep 17 00:00:00 2001 From: "P.Hurley" <21112107+PatHurley@users.noreply.github.com> Date: Fri, 26 Jun 2020 23:18:38 -0400 Subject: [PATCH] Adding makeReady script and necessary documents --- bin/.scriptResources/template.txt | 51 +++++++++++++++ bin/ideas.txt | 6 ++ bin/makeReady | 101 ++++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 bin/.scriptResources/template.txt create mode 100644 bin/ideas.txt create mode 100755 bin/makeReady diff --git a/bin/.scriptResources/template.txt b/bin/.scriptResources/template.txt new file mode 100644 index 0000000..1c0762f --- /dev/null +++ b/bin/.scriptResources/template.txt @@ -0,0 +1,51 @@ +#!/bin/bash +# ----------------------------------------------------------------------------- +# Written by: +# +# ----------------------------------------------------------------------------- + +VERSION=0.1.0 +USAGE () { +printf "Usage: $(basename $0) [OPTIONS]... [ARGUMENTS]... + + +OPTIONS. + -h, --help display this help and exit + --version output version information and exit + + + +Exit status: + 0 if OK, + 1 if minor issue (e.g. cannot parse input), + 2 if serious issue (e.g. cannot access file/resource). + + +(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 diff --git a/bin/ideas.txt b/bin/ideas.txt new file mode 100644 index 0000000..86c1793 --- /dev/null +++ b/bin/ideas.txt @@ -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. diff --git a/bin/makeReady b/bin/makeReady new file mode 100755 index 0000000..1590b05 --- /dev/null +++ b/bin/makeReady @@ -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