Advertisement
spudgunman

domoticz pi motd

Jun 4th, 2015
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.40 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. clear
  4.  
  5. function color (){
  6.   echo "\e[$1m$2\e[0m"
  7. }
  8.  
  9. function extend (){
  10.   local str="$1"
  11.   let spaces=60-${#1}
  12.   while [ $spaces -gt 0 ]; do
  13.     str="$str "
  14.     let spaces=spaces-1
  15.   done
  16.   echo "$str"
  17. }
  18.  
  19. function center (){
  20.   local str="$1"
  21.   let spacesLeft=(78-${#1})/2
  22.   let spacesRight=78-spacesLeft-${#1}
  23.   while [ $spacesLeft -gt 0 ]; do
  24.     str=" $str"
  25.     let spacesLeft=spacesLeft-1
  26.   done
  27.  
  28.   while [ $spacesRight -gt 0 ]; do
  29.     str="$str "
  30.     let spacesRight=spacesRight-1
  31.   done
  32.  
  33.   echo "$str"
  34. }
  35.  
  36. function sec2time (){
  37.   local input=$1
  38.  
  39.   if [ $input -lt 60 ]; then
  40.     echo "$input seconds"
  41.   else
  42.     ((days=input/86400))
  43.     ((input=input%86400))
  44.     ((hours=input/3600))
  45.     ((input=input%3600))
  46.     ((mins=input/60))
  47.  
  48.     local daysPlural="s"
  49.     local hoursPlural="s"
  50.     local minsPlural="s"
  51.  
  52.     if [ $days -eq 1 ]; then
  53.       daysPlural=""
  54.     fi
  55.  
  56.     if [ $hours -eq 1 ]; then
  57.       hoursPlural=""
  58.     fi
  59.  
  60.     if [ $mins -eq 1 ]; then
  61.       minsPlural=""
  62.     fi
  63.  
  64.     echo "$days day$daysPlural, $hours hour$hoursPlural, $mins minute$minsPlural"
  65.   fi
  66. }
  67.  
  68. borderColor=35
  69. headerLeafColor=32
  70. headerRaspberryColor=31
  71. greetingsColor=36
  72. statsLabelColor=33
  73.  
  74. borderLine="━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  75. borderTopLine=$(color $borderColor "┏$borderLine┓")
  76. borderBottomLine=$(color $borderColor "┗$borderLine┛")
  77. borderBar=$(color $borderColor "┃")
  78. borderEmptyLine="$borderBar                                                                              $borderBar"
  79.  
  80. # Header
  81. header="$borderTopLine\n$borderEmptyLine"
  82.  
  83. me=$(whoami)
  84.  
  85. # Greetings
  86. greetings="$borderBar$(color $greetingsColor "$(center "Welcome back, $me!")")$borderBar\n"
  87. greetings="$greetings$borderBar$(color $greetingsColor "$(center "$(date +"%A, %d %B %Y, %T")")")$borderBar"
  88.  
  89. # System information
  90. read loginFrom loginIP loginDate loginTime <<< $(last $me | awk 'NR==2 { print $2,$3,$4,$7 }')
  91.  
  92. # TTY login
  93. if [[ $loginDate == - ]]; then
  94.   loginDate=$loginIP
  95.   loginIP=$loginFrom
  96. fi
  97.  
  98.  
  99. login="$(date -d $loginDate +"%A, %d %B %Y,") $loginTime ($loginIP)"
  100.  
  101. read domStatus <<< $(sudo service domoticz.sh status)
  102.  
  103. label1="$(extend "$login")"
  104. label1="$borderBar  $(color $statsLabelColor "Last Login....:") $label1$borderBar"
  105.  
  106. uptime="$(sec2time $(cut -d "." -f 1 /proc/uptime))"
  107. uptime="$uptime ($(date -d "@"$(grep btime /proc/stat | cut -d " " -f 2) +"%d-%m-%Y %H:%M:%S"))"
  108.  
  109. label2="$(extend "$uptime")"
  110. label2="$borderBar  $(color $statsLabelColor "Uptime........:") $label2$borderBar"
  111.  
  112. label3="$(extend "$(free -m | awk 'NR==2 { printf "Total: %sMB, Used: %sMB, Free: %sMB",$2,$3,$4; }')")"
  113. label3="$borderBar  $(color $statsLabelColor "Memory........:") $label3$borderBar"
  114.  
  115. label4="$(extend "$(df -h ~ | awk 'NR==2 { printf "Total: %sB, Used: %sB, Free: %sB",$2,$3,$4; }')")"
  116. label4="$borderBar  $(color $statsLabelColor "Home space....:") $label4$borderBar"
  117.  
  118. label5="$(extend "$domStatus")"
  119. label5="$borderBar  $(color $statsLabelColor "Domoto Service:") $label5$borderBar"
  120.  
  121. stats="$label1\n$label2\n$label3\n$label4\n$label5"
  122.  
  123. # Print motd
  124. echo -e "$header\n$borderEmptyLine\n$greetings\n$borderEmptyLine\n$stats\n$borderEmptyLine\n$borderBottomLine"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement