inst 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/bin/bash
  2. #stat --format '%G' inst
  3. # Functions
  4. function inst_out
  5. {
  6. echo "[inst] $1"
  7. }
  8. function inst_fatal
  9. {
  10. inst_out "FATAL : $1"
  11. }
  12. # Welcome
  13. inst_out 'Script begins'
  14. # Check argument
  15. if [ $# -lt 1 ]
  16. then
  17. inst_fatal 'No script provided'
  18. inst_out 'Usage : inst <script> [user] [target]'
  19. exit 1
  20. fi
  21. # Initialize variables
  22. script=$1
  23. user=$USER
  24. target='/usr/local/bin/'
  25. if [ $# -gt 1 ] && [ ! -z $2 ]
  26. then
  27. user=$2
  28. fi
  29. if [ $# -gt 2 ] && [ ! -z $3 ]
  30. then
  31. target=$3
  32. fi
  33. # Check file existance
  34. # Prompt before installation
  35. inst_out 'Ready for installation'
  36. inst_out "script : $script"
  37. inst_out "user : $user"
  38. inst_out "target : $target"
  39. read -p 'Confirm installation ? [Y/n] > ' choice
  40. if [ ! -z $choice ] && [ "$choice" != 'Y' ]
  41. then
  42. inst_out 'Cancel installation'
  43. exit 0
  44. fi
  45. # Start installing
  46. inst_out 'Installation begins'
  47. # Check if script executable
  48. if [ -x $script ]
  49. then
  50. inst_out 'Script already executable'
  51. else
  52. if [ $USER != 'root' ]
  53. then
  54. inst_fatal "$script is not executable"
  55. inst_out 'to fix it :'
  56. inst_out "chmod u+x $script"
  57. inst_out 'or'
  58. inst_out "sudo $0 $script $user $target"
  59. exit 1
  60. else
  61. chmod u+x $script
  62. fi
  63. fi
  64. # Copy script
  65. cp "$script" "$target$script"
  66. if [ $? -ne 0 ]
  67. then
  68. inst_fatal 'Something went wrong during copy'
  69. inst_out "command : cp $script $target$script"
  70. exit $?
  71. fi
  72. # Chown
  73. if [ $USER = 'root' ]
  74. then
  75. chown "$user:$user" "$target$script"
  76. if [ $? -ne 0 ]
  77. then
  78. inst_fatal 'Something went wrong during chown'
  79. inst_out "chown $user:$user $target$script"
  80. exit $?
  81. fi
  82. fi
  83. # End
  84. inst_out 'Script finished'
  85. exit $?