inst 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. if [ ! -e $script ]
  35. then
  36. inst_fatal "$script does not exist"
  37. exit 1
  38. fi
  39. # Prompt before installation
  40. inst_out 'Ready for installation'
  41. inst_out "script : $script"
  42. inst_out "user : $user"
  43. inst_out "target : $target"
  44. read -p 'Confirm installation ? [Y/n] > ' choice
  45. if [ ! -z $choice ] && [ "$choice" != 'Y' ]
  46. then
  47. inst_out 'Cancel installation'
  48. exit 0
  49. fi
  50. # Start installing
  51. inst_out 'Installation begins'
  52. # Check if script executable
  53. if [ -x $script ]
  54. then
  55. inst_out 'Script already executable'
  56. else
  57. if [ $USER != 'root' ]
  58. then
  59. inst_fatal "$script is not executable"
  60. inst_out 'to fix it :'
  61. inst_out "chmod u+x $script"
  62. inst_out 'or'
  63. inst_out "sudo $0 $script $user $target"
  64. exit 1
  65. else
  66. chmod u+x $script
  67. fi
  68. fi
  69. # Copy script
  70. cp "$script" "$target$script"
  71. if [ $? -ne 0 ]
  72. then
  73. inst_fatal 'Something went wrong during copy'
  74. inst_out "command : cp $script $target$script"
  75. exit $?
  76. fi
  77. # Chown
  78. if [ $USER = 'root' ]
  79. then
  80. chown "$user:$user" "$target$script"
  81. if [ $? -ne 0 ]
  82. then
  83. inst_fatal 'Something went wrong during chown'
  84. inst_out "chown $user:$user $target$script"
  85. exit $?
  86. fi
  87. fi
  88. # Check if all right
  89. # End
  90. inst_out 'Script finished'
  91. exit $?