freedombone-blog 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Blogging functions
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2016 Bob Mottram <bob@robotics.uk.to>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. export TEXTDOMAIN=${PROJECT_NAME}-blog
  31. export TEXTDOMAINDIR="/usr/share/locale"
  32. CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
  33. HOSTNAME=
  34. AVATAR=
  35. # get the blog hostname
  36. if grep -q "FULLBLOG_DOMAIN_NAME" $CONFIGURATION_FILE; then
  37. HOSTNAME=$(grep "FULLBLOG_DOMAIN_NAME" $CONFIGURATION_FILE | awk -F '=' '{print $2}')
  38. fi
  39. BASE_DIR=/var/www/$HOSTNAME/htdocs
  40. function show_help {
  41. echo ''
  42. echo $"${PROJECT_NAME}-blog -h [hostname] -a [avatar image file]"
  43. echo ''
  44. echo $'Blogging functions'
  45. echo ''
  46. echo $' --help Show help'
  47. echo $' -h --hostname [name] Hostname'
  48. echo $' -a --avatar [url] Filename or url for avatar'
  49. echo ''
  50. exit 0
  51. }
  52. while [[ $# > 1 ]]
  53. do
  54. key="$1"
  55. case $key in
  56. --help)
  57. show_help
  58. ;;
  59. -h|--hostname)
  60. shift
  61. HOSTNAME="$1"
  62. ;;
  63. -a|--avatar)
  64. shift
  65. AVATAR="$1"
  66. ;;
  67. *)
  68. # unknown option
  69. ;;
  70. esac
  71. shift
  72. done
  73. if [ ! $HOSTNAME ]; then
  74. echo $'No hostname specified'
  75. exit 5748
  76. fi
  77. if [ ! -d $BASE_DIR ]; then
  78. echo "$BASE_DIR was not found"
  79. exit 1
  80. fi
  81. function set_avatar_from_file {
  82. SOURCE_IMAGE_FILE="$1"
  83. if [ ! -f $SOURCE_IMAGE_FILE ]; then
  84. echo $'Source file not found'
  85. exit 2
  86. fi
  87. # copy the source image
  88. cd $BASE_DIR
  89. AVATAR_FILES=$(find . -name avatar.png)
  90. read -a arr <<<$AVATAR_FILES
  91. for i in "${arr[@]}"
  92. do
  93. FILENAME="$BASE_DIR$(echo \"$i\" | awk -F '.' '{print $2}')".png
  94. if [[ "$FILENAME" != "$SOURCE_IMAGE_FILE" ]]; then
  95. cp -f $SOURCE_IMAGE_FILE "$FILENAME"
  96. fi
  97. done
  98. }
  99. function set_avatar_from_url {
  100. if [ ! -d $BASE_DIR/customimages ]; then
  101. mkdir $BASE_DIR/customimages
  102. fi
  103. # download the image
  104. cd $BASE_DIR/customimages
  105. # convert to png
  106. wget $AVATAR -O avatar
  107. if [[ $AVATAR == *".gif" ]]; then
  108. mv avatar avatar.gif
  109. mogrify -format png avatar.gif
  110. fi
  111. if [[ $AVATAR == *".jpg" ]]; then
  112. mv avatar avatar.jpg
  113. mogrify -format png avatar.jpg
  114. fi
  115. if [[ $AVATAR == *".jpeg" ]]; then
  116. mv avatar avatar.jpeg
  117. mogrify -format png avatar.jpeg
  118. fi
  119. if [ -f avatar ]; then
  120. mv avatar avatar.png
  121. fi
  122. # standard size
  123. mogrify -resize 150x150 avatar.png
  124. if [ ! -f $BASE_DIR/customimages/avatar.png ]; then
  125. echo $'Avatar image could not be downloaded'
  126. exit 3
  127. fi
  128. chown -R www-data:www-data $BASE_DIR/customimages
  129. }
  130. if [[ "$AVATAR" == "http"* ]]; then
  131. set_avatar_from_url
  132. fi
  133. AVATAR=$BASE_DIR/customimages/avatar.png
  134. if [ -f $AVATAR ]; then
  135. set_avatar_from_file $AVATAR
  136. fi
  137. exit 0