Several shell scripts in a wrapper pretending to be automation. User assumes all risk. http://functions.sh/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

294 lines
7.3 KiB

  1. # !/bin/bash
  2. # @author: @jakimfett
  3. # @description: minimalism, sorta
  4. # @goal: satisfy mel's curiousity
  5. # @practice: never do anything for just one reason
  6. # @advice: when stuck, do something functionally/tactically frivelous with long term strategic benefit.
  7. # @link: https://en.wikipedia.org/wiki/The_Story_of_Mel
  8. # @addendum: http://archive.is/jDFuO
  9. #
  10. # @todo double check input sanitization#
  11. # @todo (better)feature flags mebbe?
  12. # @todo load functions.sh if it exists on the system
  13. # @todo loop user input bits until valid/exit path found
  14. # @todo command line params for config bits
  15. # @todo split actual install into install.sh, obviously
  16. # @todo scan docs for missing/broken links, combine with archive.is replacement?
  17. ###
  18. # Author's comment (2018.07.14.2119.gmt.gregorian):
  19. # this is a *very* dangerous tool
  20. # educating yourself mitigates the (karmic) stupidity tax
  21. # remove the following line(s) from execution to violate (some|all) warrenties
  22. #exit 1
  23. ###
  24. # hey
  25. # --- # --- # --- # --- #
  26. # submolecular life looks a lot like code.
  27. # --- # --- # --- # --- # --- # --- # --- #
  28. # huh. that's odd.
  29. # --- # --- # --- # --- # --- # --- # --- # --- # --- # --- # --- #
  30. # some prerequisites
  31. # @todo autoinstall, or autocompile?
  32. declare depList=('git' 'ssh' 'rsync' 'mlocate' 'traceroute' 'traceroute6' 'dig')
  33. declare depListDev=('man-db' 'etckeeper')
  34. declare -A missingDep
  35. declare -A autoInstall=('man-db')
  36. declare -A autoCompile=('capn-proto')
  37. # grab the epoc timestamp (for determining how long stuff took to run)
  38. declare -A stateVar
  39. stateVar['start']="$(date +%s)"
  40. # we can convert this to a human-readable format later.
  41. # exit code definitions
  42. declare -A exits
  43. exits['done']=0
  44. exits['general']=1
  45. exits['dependency']=2
  46. exits['permissions']=3
  47. exits['debug']=4
  48. exits['failmuffins']=5
  49. exits['userInput']=6
  50. exits['aog']=7
  51. # done with exit codes
  52. # Edit these to break things
  53. declare -A config
  54. # where did we put the rest of our parts?
  55. config['installPath']="$(realpath ~/)/src/f.sh/"
  56. # location for the base directory installation
  57. # realpath makes cross platform easier
  58. config['invokedFrom']="$(pwd)/"
  59. config['self']="${0:1:${#0}}"
  60. # how to present time to the user(s)
  61. config['dateTimeFormat']='%Y.%m.%d:%T (%Z, %z)'
  62. # logging bits
  63. #
  64. # @default.loglevel.hardcoded
  65. # display by default messages with log level greater than this value
  66. config['logLevel']=3
  67. # see how we [stack](/doc/stacks/readme.md) the file path?
  68. # config['logPath']="${config['installPath']}logs"
  69. config['logPath']="${config['invokedFrom']}logs"
  70. if [ ! -d ${config['logPath']} ]; then
  71. mkdir -p ${config['logPath']}
  72. fi
  73. config['logFile']="${config['logPath']}${config['self']}.$$.log"
  74. echo "Logging to: ${config['logFile']}"
  75. if [ ! -w ${config['logFile']} ]; then
  76. touch ${config['logFile']}
  77. fi
  78. # done with configuration values (for now...)
  79. logThis "Script start: $(dateConvert ${stateVar['start']}) " 1
  80. exit
  81. ###
  82. # Author's comment (2018.07.14.2119.gmt.gregorian):
  83. # this is a *very* dangerous tool
  84. # educating yourself mitigates the (karmic) stupidity tax
  85. # remove the following line(s) from execution to violate (some|all) warrenties
  86. #exit 1
  87. # getting feedback from the user is super important
  88. # make sure that the functions we need actually exist first
  89. if [ ${isInstalled} -ne 1 ];then
  90. askUser "This system seems to lack functions.sh, download/verify/install?"
  91. echo $?
  92. if [ ${userInputYes} ];then
  93. if [ ${isInstallDir} ];then
  94. wget https://functions.sh/ -o ./install.sh
  95. md5sum verify ./install.sh
  96. if [ ${installerVerified} ];then
  97. eval ./install.sh
  98. else
  99. logThis "Installer failed verifiecation, please download manually." 0
  100. fi
  101. else
  102. logThis "Base install cannot be found." 0 'tmp/fsh.install.log'
  103. fi
  104. ###
  105. # hey
  106. # --- # --- # --- # --- #
  107. # submolecular life looks a lot like code.
  108. # --- # --- # --- # --- # --- # --- # --- #
  109. # huh. that's odd.
  110. # --- # --- # --- # --- # --- # --- # --- # --- # --- # --- # --- #
  111. # mobility, finally!
  112. cd "${config['installPath']}"
  113. ls -lah
  114. echo "#=- -=#"
  115. echo "#=- -=#"
  116. echo "#=- -=#"
  117. echo "#=- -=#"
  118. echo "#=- -=#"
  119. echo "#=- -=#"
  120. echo "#=- -=#"
  121. echo "#=- -=#"
  122. echo "Hold on, here we go..."
  123. # @todo - set all variables via user-facing setup questions
  124. # Set the command character.
  125. commandChar='-'
  126. echo
  127. # Commands, short and long
  128. declare -A enabledCommands commandDescript
  129. enabledCommands['h']="help" # display the help
  130. commandDescript["${enabledCommands['h']}"]="Displays usage/command info for the script."
  131. enabledCommands['i']="install" # update the script
  132. commandDescript["${enabledCommands['i']}"]="Installs functions.sh to the local system."
  133. enabledCommands['p']="purge" # update the script
  134. commandDescript["${enabledCommands['p']}"]="Remove all traces of functions.sh from the local system."
  135. # Sanitized parameter array.
  136. declare -a saneParam
  137. # sanitize and process short params
  138. # short params are a single command character followed by one or more character(s)
  139. # intended as shorthand and ease-of-prototyping flags
  140. function processShortParams {
  141. # fail early if there's nothing passed to the function
  142. if [ "${@}" ]; then
  143. # Local variable for manipulating/using input
  144. local inputParamString="${@}"
  145. # Iterate through the length of the command string
  146. for (( i=0; i<${#inputParamString}; i++ )); do
  147. # ignore the command character
  148. if [[ "${commandChar}" =~ "${inputParamString:$i:1}" ]]; then
  149. continue
  150. fi
  151. # Add the character in question to the parameters list if it is part of the enabledCommands array
  152. if [[ "${enabledCommands[${inputParamString:$i:1}]}" ]]; then
  153. saneParam[${#saneParam[@]}]="${enabledCommands[${inputParamString:$i:1}]}"
  154. fi
  155. done
  156. fi
  157. }
  158. function processLongParams {
  159. # fail early if there's nothing passed to the function
  160. if [ "${@}" ]; then
  161. # Local variable for manipulating/using input
  162. local inputParamString="${@}"
  163. # Remove the command sequence from the string
  164. if [ "${commandChar}${commandChar}" == "${inputParamString:0:2}" ]; then
  165. saneParam[${#saneParam[@]}]="${inputParamString:2:${#inputParamString}}"
  166. fi
  167. fi
  168. }
  169. # Display script usage information and flags.
  170. function usageHelp {
  171. doneList['help']="task begin: `./when.sh`"
  172. echo
  173. echo "Commands available in f.sh:"
  174. echo -e " <empty>|--help|-h \t ${commandDescript["help"]}"
  175. echo -e " --install|-i \t\t ${commandDescript["install"]}"
  176. echo -e " --purge|-p \t\t ${commandDescript["purge"]}"
  177. echo
  178. # @todo - calculate run time?
  179. doneList['help']="${doneList['help']}, task complete: `./when.sh`"
  180. }
  181. # Decision input from the user
  182. function userInput {
  183. read -p "execute the previous command(s)? (y/n/?): " rawInput
  184. case "${rawInput}" in
  185. [yY])
  186. echo `./when.sh`
  187. ;;
  188. [nN])
  189. echo "program will now exit"
  190. exit 1
  191. ;;
  192. *)
  193. echo "input '${rawInput}' not recognized, program will exit."
  194. echo
  195. exit 1
  196. ;;
  197. esac
  198. }
  199. for flag in "${@}"; do
  200. case "${flag}" in
  201. # parse any short flags (single command char)
  202. ${commandChar}[a-zA-Z0-9_]*)
  203. processShortParams "${flag}"
  204. #echo "shortParam"
  205. ;;
  206. # parse long params (double command char)
  207. ${commandChar}${commandChar}[a-zA-Z0-9_]*)
  208. processLongParams "${flag}"
  209. echo
  210. #echo "longParam"
  211. ;;
  212. *)
  213. echo "command line input '${flag}' not understood."
  214. echo
  215. ;;
  216. esac
  217. done
  218. ## unsorted --v
  219. # if no parameters have been passed, display the help
  220. if [ "${#saneParam}" == 0 ]; then
  221. usageHelp
  222. fi