install.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #!/usr/bin/env bash
  2. #===========
  3. # g6scripts
  4. #===========
  5. # file: install
  6. # created: 2020-01-15
  7. # updated: 2020-01-16
  8. # description: loops through files in ./src & installs them to configured destination
  9. #---------
  10. # GLOBALS
  11. #---------
  12. ln=0
  13. dir=0
  14. all=0
  15. force=0
  16. verbose=0
  17. default_destination=$HOME/.local/bin
  18. if [[ $USER == "root" ]]; then default_destination=/usr/local/bin/; fi
  19. source=./src/posix/*.*
  20. depreciated_src=./src/posix/depreciated/*.*
  21. scripts=()
  22. depreciated=0
  23. #-----------
  24. # FUNCTIONS
  25. #-----------
  26. ## print_help = prints the help dialog and exits
  27. print_help () {
  28. echo "usage: ./install [ARGUMENTS] [SCRIPT]"
  29. echo ""
  30. echo "SCRIPTS"
  31. echo " Optionally, you can specify a script to install."
  32. echo " Any specified scripts must be the filepath to the"
  33. echo " script (not just the filename)."
  34. echo ""
  35. echo "ARGUMENTS"
  36. echo " -f, --force overwrites files when installing"
  37. echo " -r, --reinstall re-install scripts already existing in"
  38. echo " destination directory"
  39. echo " -d, --dest [DEST] set the install destination"
  40. echo " -a, --all install ALL the scripts ('y' to all prompts)"
  41. echo " -l, --link 'ln' the script (not cp)."
  42. echo " -h, --help print this dialog"
  43. }
  44. install_script () {
  45. src=$1
  46. dest=$2
  47. cmd=
  48. if [[ $ln -eq 0 ]]; then
  49. cmd=$(install -vp $src $dest)
  50. else # ln the file
  51. cmd=$(chmod +x $src && echo ""; ln -vsf $(readlink -f $src) $dest)
  52. fi
  53. if [[ ! $cmd ]]; then
  54. echo "something went wrong"; exit 1;
  55. else
  56. echo $cmd
  57. fi
  58. }
  59. ## install_yn = installs $1 to $2, prints $3 if user asks for "info"
  60. install_yn () {
  61. src=$1 # path of the file to install
  62. dest=$2 # path to install the file to
  63. depr=$3 # if installing a depreciated script or not
  64. ask=0
  65. while [[ $ask -eq 0 ]]; do
  66. if [[ $force -eq 0 ]]; then
  67. printf "install %s [yes/no/info]? " $src
  68. if [[ $all -eq 0 || $depr -eq 1 ]]; then
  69. if [[ -e $dest && $reinstall -eq 1 ]]; then
  70. yn="y"
  71. printf "\treinstalling...\n"
  72. else
  73. read yn
  74. fi
  75. else
  76. yn="y"
  77. echo "y"
  78. fi
  79. if [[ $yn == "y" || $yn == "Y" || $yn == "yes" ]]; then
  80. install_script $src $dest
  81. ask=1
  82. if [[ $reinstall ]]; then
  83. chmod +x "$src"
  84. fi
  85. elif [[ $yn == "n" || $yn == "N" || $yn == "no" ]]; then
  86. printf "\tskipping...\n"
  87. ask=1
  88. elif [[ $yn == "i" || $yn == "I" || $yn == "info" ]]; then
  89. echo ""
  90. sh $(readlink -f $src) -h | sed 's/^/ /'
  91. echo ""
  92. #grep -i "description\|dependencies\|arguments\|depreciated" $script
  93. fi
  94. else
  95. install_script $src $dest
  96. ask=1
  97. fi
  98. done
  99. }
  100. #------
  101. # MAIN
  102. #------
  103. ## parse args
  104. while [[ $# -gt 0 ]]; do
  105. case $1 in
  106. -f|--force)
  107. force=1
  108. echo "forcing overwrite on existing files"
  109. shift
  110. ;;
  111. -l|--link)
  112. ln=1
  113. echo "will only create symolic links to scripts, this will make the scripts in ./src/ executable"
  114. shift
  115. ;;
  116. -d|--dest)
  117. dir=1
  118. destination=$2
  119. echo "install destination = $destination"
  120. shift
  121. shift
  122. ;;
  123. -a|--all)
  124. all=1
  125. echo "installed ALL scripts (not including depreciated)"
  126. shift
  127. ;;
  128. -r|--reinstall)
  129. reinstall=1
  130. echo "reinstalling currently existing files"
  131. shift
  132. ;;
  133. -h|--help)
  134. print_help
  135. exit
  136. ;;
  137. *) # assume it's a specifed script to install
  138. scripts+=($1)
  139. depreciated=1
  140. shift
  141. ;;
  142. esac
  143. done
  144. ## check install destination
  145. if [[ $dir -eq 0 ]]; then
  146. printf "install destination (default = $default_destination): "
  147. read destination
  148. ## set to default if empty input
  149. if [ -z $destination ]; then
  150. destination=$default_destination
  151. else
  152. destination=${destination/"~"/"/home/$USER"}
  153. fi
  154. ## check exists
  155. if [ ! -e $destination ]; then mkdir -p "$destination"; fi
  156. ## check if need root permission to r/w
  157. if [ ! -w $destination ]; then echo "$destination: Permission denied"; exit 1; fi
  158. fi
  159. mkdir -p $destination
  160. ## install all $scripts
  161. if [ ${#scripts[@]} -eq 0 ]; then scripts=$source; fi
  162. for script in $scripts; do
  163. if [[ -e ${destination//"~"/$HOME}/$(basename $script .sh) && $force -eq 0 && $reinstall -eq 0 ]]; then
  164. printf "${destination//"~"/$HOME}/$(basename $script .sh) already exists, skipping...\n"
  165. elif [[ $script != "src/depreciated" ]]; then # skip depreciated
  166. install_yn $script ${destination//"~"/$HOME}/$(basename $script .sh)
  167. fi
  168. done
  169. ## install depreciated scripts
  170. while [[ $depreciated -eq 0 ]]; do
  171. printf "install depreciated scripts (default = no) [yes/no/info]? "
  172. read yn
  173. if [[ $yn == "y" || $yn == "Y" || $yn == "yes" ]]; then
  174. for script in $depreciated_src; do
  175. install_yn $script ${destination//"~"/$HOME}/$(basename $script .sh) 1
  176. done
  177. depreciated=1
  178. elif [[ $yn == "n" || $yn == "N" || $yn == "no" ]]; then
  179. depreciated=1
  180. elif [[ $yn == "i" || $yn == "I" || $yn == "info" ]]; then
  181. echo "depreciated scripts have been found redundant, details are provided in the \"info\" for each script"
  182. fi
  183. done
  184. ## end
  185. echo "done"