#!/bin/bash # Author: Linyos Torovoltos github.com/linyostorovovoltos global currentVersion := '"1.11.1'" global multiline := '"0'" # flag that indicates multiline option proc getConfiguredClient { if command -v curl &> /dev/null ; { global configuredClient := '"curl'" } elif command -v wget &> /dev/null ; { global configuredClient := '"wget'" } else { echo "Error: This tool requires either curl or wget to be installed." > !2 return 1 } } proc update { # Author: Alexander Epstein https://github.com/alexanderepstein # Update utility version 1.2.0 # To test the tool enter in the defualt values that are in the examples for each variable global repositoryName := '"Bash-Snippets'" #Name of repostiory to be updated ex. Sandman-Lite global githubUserName := '"alexanderepstein'" #username that hosts the repostiory ex. alexanderepstein global nameOfInstallFile := '"install.sh'" # change this if the installer file has a different name be sure to include file extension if there is one global latestVersion := $[httpGet https://api.github.com/repos/$githubUserName/$repositoryName/tags | grep -Eo '"name":.*?[^\\]",'| head -1 | grep -Eo "[0-9.]+] #always grabs the tag without the v option if [[ $currentVersion == "" || $repositoryName == "" || $githubUserName == "" || $nameOfInstallFile == "" ]]{ echo "Error: update utility has not been configured correctly." > !2 exit 1 } elif [[ $latestVersion == "" ]]{ echo "Error: no active internet connection" > !2 exit 1 } else { if [[ "$latestVersion" != "$currentVersion" ]] { echo "Version $latestVersion available" echo -n "Do you wish to update $repositoryName [Y/n]: " read -r answer if [[ "$answer" == "Y" || "$answer" == "y" ]] { cd ~ || do { echo 'Update Failed' ; exit 1 ; } if [[ -d ~/$repositoryName ]] { rm -r -f $repositoryName || do { echo "Permissions Error: try running the update as sudo"; exit 1; } ; } git clone "https://github.com/$githubUserName/$repositoryName" || do { echo "Couldn't download latest version" ; exit 1; } cd $repositoryName || do { echo 'Update Failed' ; exit 1 ;} git checkout "v$latestVersion" !2 > /dev/null || git checkout $latestVersion !2 > /dev/null || echo "Couldn't git checkout to stable release, updating to latest commit." chmod a+x install.sh #this might be necessary in your case but wasnt in mine. ./$nameOfInstallFile "update" || exit 1 cd .. rm -r -f $repositoryName || do { echo "Permissions Error: update succesfull but cannot delete temp files located at ~/$repositoryName delete this directory with sudo"; exit 1; } } else { exit 1 } } else { echo "$repositoryName is already the latest version" } } } proc httpGet { matchstr $configuredClient { curl { curl -A curl -s @Argv} wget { wget -qO- @Argv} } } proc makeqr { global input := $[echo $input | sed s/" "/%20/g] ## replace all spaces in the sentence with HTML encoded space %20 httpGet qrenco.de/$input ## get a response for the qrcode } proc makeMultiLineQr { if [[ $configuredClient != "curl" ]] { ## prevent usage without curl it is unreliable echo "Multiline currently only supports curl!" return 1 } else { printf "%s" "$ifsjoin(Argv)" | curl -F-='<'- qrenco.de } } proc checkInternet { echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null !2 > !1 if test $Status -eq 0 { return 0 } else { echo "Error: no active internet connect" > !2 return 1 } } proc usage { echo "Qrify" echo "Usage: qrify [stringtoturnintoqrcode]" echo "Description: Converts strings or urls into a qr code." echo " -u Update Bash-Snippet Tools" echo " -m Enable multiline support (feature not working yet)" echo " -h Show the help" echo " -v Get the tool version" echo "Examples:" echo " qrify this is a test string" echo " qrify -m two\\\\nlines" echo " qrify github.com # notice no http:// or https:// this will fail" } getConfiguredClient || exit 1 checkInternet || exit 1 while getopts m:hvu*: option { matchstr $(option) { v { echo "Version $currentVersion" && exit 0 } u { update && exit 0 || exit 1 } h { usage && exit 0 } m { global multiline := '"1'" && echo "Error this is not a supported feature yet" && exit 1 } * { usage && exit 0 } } } if [[ $# == "0" ]] { usage exit 0 } elif [[ $1 == "help" ]] { usage exit 0 } elif [[ $1 == "update" ]]{ update || exit 1 exit 0 } else { if [[ $multiline == "0" ]]{ global input := $[printf '%s ' @Argv] makeqr || exit 1 exit 0 } else { makeMultiLineQr $(@:2) || exit 1 ## if multiline that means a flag existed so start from the second argument exit 0 } } (CommandList children: [ (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:currentVersion) op:Equal rhs:{(DQ (1.11.1))} spids:[7])] spids: [7] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:multiline) op:Equal rhs:{(DQ (0))} spids:[12])] spids: [12] ) (FuncDef name: getConfiguredClient body: (BraceGroup children: [ (If arms: [ (if_arm cond: [ (Sentence child: (C {(command)} {(-v)} {(curl)}) terminator: ) (Sentence child: (SimpleCommand redirects: [ (Redir op_id: Redir_Great fd: -1 arg_word: {(/dev/null)} spids: [38] ) ] ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:configuredClient) op: Equal rhs: {(DQ (curl))} spids: [47] ) ] spids: [47] ) ] spids: [-1 44] ) (if_arm cond: [ (Sentence child: (C {(command)} {(-v)} {(wget)}) terminator: ) (Sentence child: (SimpleCommand redirects: [ (Redir op_id: Redir_Great fd: -1 arg_word: {(/dev/null)} spids: [62] ) ] ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:configuredClient) op: Equal rhs: {(DQ (wget))} spids: [71] ) ] spids: [71] ) ] spids: [53 68] ) ] else_action: [ (SimpleCommand words: [ {(echo)} {(DQ ("Error: This tool requires either curl or wget to be installed."))} ] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[86])] ) (ControlFlow token: arg_word:{(1)}) ] spids: [77 95] ) ] spids: [26] ) spids: [22 25] ) (FuncDef name: update body: (BraceGroup children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:repositoryName) op: Equal rhs: {(DQ (Bash-Snippets))} spids: [119] ) ] spids: [119] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:githubUserName) op: Equal rhs: {(DQ (alexanderepstein))} spids: [128] ) ] spids: [128] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:nameOfInstallFile) op: Equal rhs: {(DQ (install.sh))} spids: [137] ) ] spids: [137] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:latestVersion) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(httpGet)} {(https) (Lit_Other ":") (//api.github.com/repos/) ($ VSub_Name "$githubUserName") (/) ($ VSub_Name "$repositoryName") (/tags) } ) (C {(grep)} {(-Eo)} {(SQ <"\"name\":.*?[^\\\\]\",">)}) (C {(head)} {(-1)}) (C {(grep)} {(-Eo)} {(DQ ("[0-9.]+"))}) ] negated: False ) ] ) left_token: spids: [147 183] ) } spids: [146] ) ] spids: [146] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalOr left: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$currentVersion")} right: {(DQ )} ) right: (LogicalOr left: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$repositoryName")} right: {(DQ )} ) right: (LogicalOr left: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$githubUserName")} right: {(DQ )} ) right: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$nameOfInstallFile")} right: {(DQ )} ) ) ) ) ) terminator: ) ] action: [ (SimpleCommand words: [ {(echo)} {(DQ ("Error: update utility has not been configured correctly."))} ] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[239])] ) (C {(exit)} {(1)}) ] spids: [-1 230] ) (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$latestVersion")} right: {(DQ )} ) ) terminator: ) ] action: [ (SimpleCommand words: [{(echo)} {(DQ ("Error: no active internet connection"))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[270])] ) (C {(exit)} {(1)}) ] spids: [248 261] ) ] else_action: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobNEqual left: {(DQ ($ VSub_Name "$latestVersion"))} right: {(DQ ($ VSub_Name "$currentVersion"))} ) ) terminator: ) ] action: [ (C {(echo)} {(DQ ("Version ") ($ VSub_Name "$latestVersion") (" available"))}) (C {(echo)} {(-n)} { (DQ ("Do you wish to update ") ($ VSub_Name "$repositoryName") (" [Y/n]: ")) } ) (C {(read)} {(-r)} {(answer)}) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalOr left: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(DQ ($ VSub_Name "$answer"))} right: {(DQ (Y))} ) right: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(DQ ($ VSub_Name "$answer"))} right: {(DQ (y))} ) ) ) terminator: ) ] action: [ (AndOr children: [ (C {(cd)} {(TildeSubPart prefix:"")}) (BraceGroup children: [ (Sentence child: (C {(echo)} {(SQ <"Update Failed">)}) terminator: ) (Sentence child: (C {(exit)} {(1)}) terminator: ) ] spids: [367] ) ] op_id: Op_DPipe ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolUnary op_id: BoolUnary_d child: {(Lit_Tilde "~") (/) ($ VSub_Name "$repositoryName") } ) ) terminator: ) ] action: [ (Sentence child: (AndOr children: [ (C {(rm)} {(-r)} {(-f)} {($ VSub_Name "$repositoryName")}) (BraceGroup children: [ (Sentence child: (C {(echo)} { (DQ ( "Permissions Error: try running the update as sudo" ) ) } ) terminator: ) (Sentence child: (C {(exit)} {(1)}) terminator: ) ] spids: [411] ) ] op_id: Op_DPipe ) terminator: ) ] spids: [-1 399] ) ] spids: [-1 429] ) (AndOr children: [ (C {(git)} {(clone)} { (DQ ("https://github.com/") ($ VSub_Name "$githubUserName") (/) ($ VSub_Name "$repositoryName") ) } ) (BraceGroup children: [ (Sentence child: (C {(echo)} {(DQ ("Couldn't download latest version"))}) terminator: ) (Sentence child: (C {(exit)} {(1)}) terminator: ) ] spids: [445] ) ] op_id: Op_DPipe ) (AndOr children: [ (C {(cd)} {($ VSub_Name "$repositoryName")}) (BraceGroup children: [ (Sentence child: (C {(echo)} {(SQ <"Update Failed">)}) terminator: ) (Sentence child: (C {(exit)} {(1)}) terminator: ) ] spids: [469] ) ] op_id: Op_DPipe ) (AndOr children: [ (SimpleCommand words: [ {(git)} {(checkout)} {(DQ (v) ($ VSub_Name "$latestVersion"))} ] redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [496] ) ] ) (AndOr children: [ (SimpleCommand words: [ {(git)} {(checkout)} {(DQ ($ VSub_Name "$latestVersion"))} ] redirects: [ (Redir op_id: Redir_Great fd: 2 arg_word: {(/dev/null)} spids: [510] ) ] ) (C {(echo)} { (DQ ( "Couldn't git checkout to stable release, updating to latest commit." ) ) } ) ] op_id: Op_DPipe ) ] op_id: Op_DPipe ) (C {(chmod)} {(a) (Lit_Other "+") (x)} {(install.sh)}) (AndOr children: [ (C {(./) ($ VSub_Name "$nameOfInstallFile")} {(DQ (update))}) (C {(exit)} {(1)}) ] op_id: Op_DPipe ) (C {(cd)} {(..)}) (AndOr children: [ (C {(rm)} {(-r)} {(-f)} {($ VSub_Name "$repositoryName")}) (BraceGroup children: [ (Sentence child: (C {(echo)} { (DQ ( "Permissions Error: update succesfull but cannot delete temp files located at ~/" ) ($ VSub_Name "$repositoryName") (" delete this directory with sudo") ) } ) terminator: ) (Sentence child: (C {(exit)} {(1)}) terminator: ) ] spids: [564] ) ] op_id: Op_DPipe ) ] spids: [-1 358] ) ] else_action: [(C {(exit)} {(1)})] spids: [583 591] ) ] spids: [-1 299] ) ] else_action: [ (C {(echo)} {(DQ ($ VSub_Name "$repositoryName") (" is already the latest version"))} ) ] spids: [594 605] ) ] spids: [279 608] ) ] spids: [104] ) spids: [100 103] ) (FuncDef name: httpGet body: (BraceGroup children: [ (Case to_match: {(DQ ($ VSub_Name "$configuredClient"))} arms: [ (case_arm pat_list: [{(curl)}] action: [(C {(curl)} {(-A)} {(curl)} {(-s)} {(DQ ($ VSub_At "$@"))})] spids: [630 631 644 -1] ) (case_arm pat_list: [{(wget)}] action: [(C {(wget)} {(-qO-)} {(DQ ($ VSub_At "$@"))})] spids: [647 648 657 -1] ) ] spids: [621 627 660] ) ] spids: [618] ) spids: [614 617] ) (FuncDef name: makeqr body: (BraceGroup children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:input) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(echo)} {($ VSub_Name "$input")}) (C {(sed)} {(s/) (DQ (" ")) (/) (Lit_Other "%") (20/g)}) ] negated: False ) ] ) left_token: spids: [673 690] ) } spids: [672] ) ] spids: [672] ) (C {(httpGet)} {(qrenco.de/) ($ VSub_Name "$input")}) ] spids: [669] ) spids: [665 668] ) (FuncDef name: makeMultiLineQr body: (BraceGroup children: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobNEqual left: {($ VSub_Name "$configuredClient")} right: {(DQ (curl))} ) ) terminator: ) ] action: [ (C {(echo)} {(DQ ("Multiline currently only supports curl!"))}) (ControlFlow token: arg_word:{(1)}) ] spids: [-1 729] ) ] else_action: [ (Pipeline children: [ (C {(printf)} {(DQ ("%s"))} {(DQ ($ VSub_Star "$*"))}) (C {(curl)} {(-F-) (Lit_Other "=") (EscapedLiteralPart token:) (-)} {(qrenco.de)} ) ] negated: False ) ] spids: [747 772] ) ] spids: [711] ) spids: [707 710] ) (FuncDef name: checkInternet body: (BraceGroup children: [ (Pipeline children: [ (C {(echo)} {(-e)} { (DQ ("GET http://google.com HTTP/1.0") (EscapedLiteralPart token:) (EscapedLiteralPart token:) ) } ) (SimpleCommand words: [{(nc)} {(google.com)} {(80)}] redirects: [ (Redir op_id: Redir_Great fd: -1 arg_word: {(/dev/null)} spids: [802] ) (Redir op_id:Redir_GreatAnd fd:2 arg_word:{(1)} spids:[806]) ] ) ] negated: False ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {($ VSub_QMark "$?")} {(-eq)} {(0)} {(Lit_Other "]")}) terminator: ) ] action: [(ControlFlow token: arg_word:{(0)})] spids: [-1 823] ) ] else_action: [ (SimpleCommand words: [{(echo)} {(DQ ("Error: no active internet connect"))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[840])] ) (ControlFlow token: arg_word:{(1)}) ] spids: [831 849] ) ] spids: [781] ) spids: [777 780] ) (FuncDef name: usage body: (BraceGroup children: [ (C {(echo)} {(DQ (Qrify))}) (C {(echo)} {(DQ ("Usage: qrify [stringtoturnintoqrcode]"))}) (C {(echo)} {(DQ ("Description: Converts strings or urls into a qr code."))}) (C {(echo)} {(DQ (" -u Update Bash-Snippet Tools"))}) (C {(echo)} {(DQ (" -m Enable multiline support (feature not working yet)"))}) (C {(echo)} {(DQ (" -h Show the help"))}) (C {(echo)} {(DQ (" -v Get the tool version"))}) (C {(echo)} {(DQ ("Examples:"))}) (C {(echo)} {(DQ (" qrify this is a test string"))}) (C {(echo)} { (DQ (" qrify -m two") (EscapedLiteralPart token:) (EscapedLiteralPart token:) (nlines) ) } ) (C {(echo)} {(DQ (" qrify github.com # notice no http:// or https:// this will fail"))}) ] spids: [858] ) spids: [854 857] ) (AndOr children:[(C {(getConfiguredClient)})(C {(exit)} {(1)})] op_id:Op_DPipe) (AndOr children:[(C {(checkInternet)})(C {(exit)} {(1)})] op_id:Op_DPipe) (While cond: [(C {(getopts)} {(m) (Lit_Other ":") (hvu) (Lit_Other "*") (Lit_Other ":")} {(option)})] body: (DoGroup children: [ (Case to_match: {(DQ (${ VSub_Name option))} arms: [ (case_arm pat_list: [{(v)}] action: [ (AndOr children: [ (C {(echo)} {(DQ ("Version ") ($ VSub_Name "$currentVersion"))}) (C {(exit)} {(0)}) ] op_id: Op_DAmp ) ] spids: [987 988 1003 -1] ) (case_arm pat_list: [{(u)}] action: [ (AndOr children: [ (C {(update)}) (AndOr children: [(C {(exit)} {(0)}) (C {(exit)} {(1)})] op_id: Op_DPipe ) ] op_id: Op_DAmp ) ] spids: [1006 1007 1023 -1] ) (case_arm pat_list: [{(h)}] action: [(AndOr children:[(C {(usage)})(C {(exit)} {(0)})] op_id:Op_DAmp)] spids: [1026 1027 1037 -1] ) (case_arm pat_list: [{(m)}] action: [ (AndOr children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:multiline) op: Equal rhs: {(DQ (1))} spids: [1043] ) ] spids: [1043] ) (AndOr children: [ (C {(echo)} {(DQ ("Error this is not a supported feature yet"))}) (C {(exit)} {(1)}) ] op_id: Op_DAmp ) ] op_id: Op_DAmp ) ] spids: [1040 1041 1062 -1] ) (case_arm pat_list: [{(Lit_Other "*")}] action: [(AndOr children:[(C {(usage)})(C {(exit)} {(0)})] op_id:Op_DAmp)] spids: [1065 1066 1076 -1] ) ] spids: [976 984 1079] ) ] spids: [973 1081] ) ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Pound "$#")} right: {(DQ (0))} ) ) terminator: ) ] action: [(C {(usage)}) (C {(exit)} {(0)})] spids: [-1 1099] ) (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Number "$1")} right: {(DQ (help))} ) ) terminator: ) ] action: [(C {(usage)}) (C {(exit)} {(0)})] spids: [1109 1124] ) (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Number "$1")} right: {(DQ (update))} ) ) terminator: ) ] action: [(AndOr children:[(C {(update)})(C {(exit)} {(1)})] op_id:Op_DPipe) (C {(exit)} {(0)})] spids: [1134 1148] ) ] else_action: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Name "$multiline")} right: {(DQ (0))} ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:input) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [(C {(printf)} {(SQ <"%s ">)} {(DQ ($ VSub_At "$@"))})] ) left_token: spids: [1185 1195] ) } spids: [1184] ) ] spids: [1184] ) (AndOr children:[(C {(makeqr)})(C {(exit)} {(1)})] op_id:Op_DPipe) (C {(exit)} {(0)}) ] spids: [-1 1181] ) ] else_action: [ (AndOr children: [ (C {(makeMultiLineQr)} { (DQ (BracedVarSub token: suffix_op: (Slice begin:(ArithWord w:{(Lit_Digits 2)})) spids: [1218 1222] ) ) } ) (C {(exit)} {(1)}) ] op_id: Op_DPipe ) (C {(exit)} {(0)}) ] spids: [1212 1240] ) ] spids: [1164 1242] ) ] )