inst 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/bin/bash
  2. # Functions
  3. function inst_out
  4. {
  5. echo "[inst] $1"
  6. }
  7. function inst_fatal
  8. {
  9. inst_out "FATAL : $1"
  10. }
  11. function inst_fail
  12. {
  13. inst_fatal 'installation has failed'
  14. }
  15. # Welcome
  16. inst_out 'Script begins'
  17. # Check argument
  18. if [ $# -lt 1 ]
  19. then
  20. inst_fatal 'No script provided'
  21. inst_out 'Usage : inst <script> [user] [prompting:enable|disable] [target]'
  22. exit 1
  23. fi
  24. # Initialize variables
  25. script=$1
  26. user=$USER
  27. prompting='enable'
  28. target='/usr/local/bin/'
  29. if [ $# -gt 1 ] && [ ! -z $2 ]
  30. then
  31. user=$2
  32. fi
  33. if [ $# -gt 2 ] && [ ! -z $3 ]
  34. then
  35. prompting=$3
  36. fi
  37. if [ $# -gt 3 ] && [ ! -z $4 ]
  38. then
  39. target=$4
  40. fi
  41. # Check file existence
  42. if [ ! -e $script ]
  43. then
  44. inst_fatal "$script does not exist"
  45. exit 1
  46. fi
  47. # Prompt before installation
  48. inst_out 'Ready for installation'
  49. inst_out "script : $script"
  50. inst_out "user : $user"
  51. inst_out "target : $target"
  52. if [ $prompting = 'enable' ]
  53. then
  54. read -p 'Confirm installation ? [Y/n] > ' choice
  55. else
  56. choice='Y'
  57. fi
  58. if [ ! -z $choice ] && [ "$choice" != 'Y' ]
  59. then
  60. inst_out 'Cancel installation'
  61. exit 0
  62. fi
  63. # Start installing
  64. inst_out 'Installation begins'
  65. # Check if script executable
  66. if [ -x $script ]
  67. then
  68. inst_out 'Script already executable'
  69. else
  70. if [ $USER != 'root' ]
  71. then
  72. inst_fatal "$script is not executable"
  73. inst_out 'to fix it :'
  74. inst_out "chmod u+x $script"
  75. inst_out 'or'
  76. inst_out "sudo $0 $script $user $target"
  77. exit 1
  78. else
  79. chmod u+x $script
  80. fi
  81. fi
  82. # Copy script
  83. cp "$script" "$target$script"
  84. if [ $? -ne 0 ]
  85. then
  86. inst_fatal 'Something went wrong during copy'
  87. inst_out "command : cp $script $target$script"
  88. exit $?
  89. fi
  90. # Chown
  91. if [ $USER = 'root' ]
  92. then
  93. chown "$user:$user" "$target$script"
  94. if [ $? -ne 0 ]
  95. then
  96. inst_fatal 'Something went wrong during chown'
  97. inst_out "chown $user:$user $target$script"
  98. exit $?
  99. fi
  100. fi
  101. # Check if all right
  102. output="$target$script"
  103. owner_user=`stat --format '%U' $output`
  104. owner_group=`stat --format '%G' $output`
  105. if [ ! -e $output ]
  106. then
  107. inst_fatal "$output does not exist"
  108. inst_fail
  109. exit 1
  110. elif [ $owner_user != $user ]
  111. then
  112. inst_fatal "$output owner user is not $user but $owner_user"
  113. inst_fail
  114. exit 1
  115. elif [ $owner_group != $user ]
  116. then
  117. inst_fatal "$output group is not $user but $owner_group"
  118. inst_fail
  119. exit 1
  120. elif [ ! -x $output ]
  121. then
  122. inst_fatal "$output is not executable"
  123. inst_out "fix : sudo chmod u+x $output"
  124. inst_fail
  125. exit 1
  126. fi
  127. # End
  128. inst_out 'Script finished'
  129. exit $?