cjdns.rc.d 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/usr/bin/env bash
  2. # This script is licensed under the GPL2 and includes modified content from the GPL2 licensed /etc/rc.d/functions script that used to be included in Archlinux
  3. ### Modified selection from the now deprecated /etc/rc.d/functions ###
  4. calc_columns () {
  5. STAT_COL=80
  6. if [[ ! -t 1 ]]; then
  7. USECOLOR=""
  8. elif [[ -t 0 ]]; then
  9. # stty will fail when stdin isn't a terminal
  10. STAT_COL=$(stty size)
  11. # stty gives "rows cols"; strip the rows number, we just want columns
  12. STAT_COL=${STAT_COL##* }
  13. elif tput cols &>/dev/null; then
  14. # is /usr/share/terminfo already mounted, and TERM recognized?
  15. STAT_COL=$(tput cols)
  16. fi
  17. if (( STAT_COL == 0 )); then
  18. # if output was 0 (serial console), set default width to 80
  19. STAT_COL=80
  20. USECOLOR=""
  21. fi
  22. # we use 13 characters for our own stuff
  23. STAT_COL=$(( STAT_COL - 13 ))
  24. if [[ -t 1 ]]; then
  25. SAVE_POSITION="\e[s"
  26. RESTORE_POSITION="\e[u"
  27. DEL_TEXT="\e[$(( STAT_COL + 4 ))G"
  28. else
  29. SAVE_POSITION=""
  30. RESTORE_POSITION=""
  31. DEL_TEXT=""
  32. fi
  33. }
  34. calc_columns
  35. # disable colors on broken terminals
  36. TERM_COLORS=$(tput colors 2>/dev/null)
  37. if (( $? != 3 )); then
  38. case $TERM_COLORS in
  39. *[!0-9]*) USECOLOR="";;
  40. [0-7]) USECOLOR="";;
  41. '') USECOLOR="";;
  42. esac
  43. fi
  44. unset TERM_COLORS
  45. deltext() {
  46. printf "${DEL_TEXT}"
  47. }
  48. stat_busy() {
  49. printf "${C_OTHER}${PREFIX_REG} ${C_MAIN}${1}${C_CLEAR} "
  50. printf "${SAVE_POSITION}"
  51. deltext
  52. printf " ${C_OTHER}[${C_BUSY}BUSY${C_OTHER}]${C_CLEAR} "
  53. }
  54. stat_done() {
  55. deltext
  56. printf " ${C_OTHER}[${C_DONE}DONE${C_OTHER}]${C_CLEAR} \n"
  57. }
  58. stat_fail() {
  59. deltext
  60. printf " ${C_OTHER}[${C_FAIL}FAIL${C_OTHER}]${C_CLEAR} \n"
  61. }
  62. status_started() {
  63. deltext
  64. echo -ne "$C_OTHER[${C_STRT}STARTED$C_OTHER]$C_CLEAR "
  65. }
  66. status_stopped() {
  67. deltext
  68. echo -ne "$C_OTHER[${C_STRT}STOPPED$C_OTHER]$C_CLEAR "
  69. }
  70. # Return PID of $1
  71. get_pid() {
  72. pidof -o %PPID $1 || return 1
  73. }
  74. # set colors
  75. if [[ $USECOLOR != [nN][oO] ]]; then
  76. if tput setaf 0 &>/dev/null; then
  77. C_CLEAR=$(tput sgr0) # clear text
  78. C_MAIN=${C_CLEAR}$(tput bold) # main text
  79. C_OTHER=${C_MAIN}$(tput setaf 4) # prefix & brackets
  80. C_SEPARATOR=${C_MAIN}$(tput setaf 0) # separator
  81. C_BUSY=${C_CLEAR}$(tput setaf 6) # busy
  82. C_FAIL=${C_MAIN}$(tput setaf 1) # failed
  83. C_DONE=${C_MAIN} # completed
  84. C_BKGD=${C_MAIN}$(tput setaf 5) # backgrounded
  85. C_H1=${C_MAIN} # highlight text 1
  86. C_H2=${C_MAIN}$(tput setaf 6) # highlight text 2
  87. else
  88. C_CLEAR="\e[m" # clear text
  89. C_MAIN="\e[;1m" # main text
  90. C_OTHER="\e[1;34m" # prefix & brackets
  91. C_SEPARATOR="\e[1;30m" # separator
  92. C_BUSY="\e[;36m" # busy
  93. C_FAIL="\e[1;31m" # failed
  94. C_DONE=${C_MAIN} # completed
  95. C_BKGD="\e[1;35m" # backgrounded
  96. C_H1=${C_MAIN} # highlight text 1
  97. C_H2="\e[1;36m" # highlight text 2
  98. fi
  99. fi
  100. PREFIX_REG="::"
  101. ### CJDNS service ###
  102. . /etc/default/cjdns
  103. PID=$(get_pid $CJDROUTE)
  104. case "$1" in
  105. start)
  106. stat_busy "Starting cjdns"
  107. #START CJDNS AND ENABLE THE DAEMON IF IT SUCCEEDS
  108. if [ -z "$PID" ]; then
  109. cjdns.sh start &> /dev/null
  110. if [ $? -gt 0 ]; then
  111. stat_busy "Unable to start the daemon"
  112. stat_fail
  113. exit 1
  114. else
  115. stat_done
  116. fi
  117. else
  118. stat_busy "The daemon is already running"
  119. stat_fail
  120. exit 1
  121. fi
  122. ;;
  123. stop)
  124. stat_busy "Stopping cjdns"
  125. cjdns.sh stop
  126. if [ $? -gt 0 ]; then
  127. stat_busy "The daemon was not running"
  128. stat_fail
  129. else
  130. stat_done
  131. fi
  132. ;;
  133. restart)
  134. $0 stop
  135. while [ ! -z "$PID" -a -d "/proc/$PID" ]; do sleep 1; done
  136. $0 start
  137. ;;
  138. status)
  139. stat_busy "The daemon is currently..."
  140. if [ $(cjdns.sh status | grep -c not) = 0 ]; then status_started; else status_stopped; fi
  141. ;;
  142. *)
  143. echo "usage: $0 {start|stop|restart|status}"
  144. esac
  145. exit 0