freedombone-utils-android 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Integration with the FreedomBox android app
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. function android_update_apps {
  29. if [ "$1" ]; then
  30. detect_installable_apps
  31. fi
  32. local_hostname=$(grep 'host-name' /etc/avahi/avahi-daemon.conf | awk -F '=' '{print $2}').local
  33. plinth_api="/var/www/${local_hostname}/htdocs/plinth/api.json"
  34. # Create a self-signed cert if needed
  35. # The FreedomBox app assumes a self-signed cert
  36. if [ ! -f "/etc/ssl/certs/${local_hostname}.crt" ]; then
  37. install_web_local_user_interface
  38. systemctl restart nginx
  39. fi
  40. if [ ! -d "/var/www/${local_hostname}/htdocs/plinth" ]; then
  41. mkdir -p "/var/www/${local_hostname}/htdocs/plinth"
  42. fi
  43. if [ ! -d "/var/www/${local_hostname}/htdocs/icons" ]; then
  44. mkdir -p "/var/www/${local_hostname}/htdocs/icons"
  45. fi
  46. echo '{' > "$plinth_api"
  47. echo ' "shortcuts": [' >> "$plinth_api"
  48. android_ctr=0
  49. app_index=0
  50. # shellcheck disable=SC2068,SC2034
  51. for a in ${APPS_INSTALLED[@]}
  52. do
  53. app_name=${APPS_INSTALLED_NAMES[$app_index]}
  54. if [ "$app_name" ]; then
  55. app_filename="/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-${app_name}"
  56. if [ -f "$app_filename" ]; then
  57. # get the icon for the app
  58. icon_filename="/usr/share/${PROJECT_NAME}/android-app/${app_name}.png"
  59. if [ -f "$icon_filename" ]; then
  60. cp "$icon_filename" "/var/www/${local_hostname}/htdocs/icons/${app_name}.png"
  61. else
  62. icon_filename=
  63. fi
  64. app_name_upper=$(echo "$app_name" | awk '{print toupper($0)}')
  65. SHORT_DESCRIPTION=
  66. DESCRIPTION=
  67. MOBILE_APP_URL=
  68. if ! grep -q "${app_name_upper}_SHORT_DESCRIPTION=" "$app_filename"; then
  69. app_index=$((app_index+1))
  70. continue
  71. fi
  72. if grep -q "#${app_name_upper}_SHORT_DESCRIPTION=" "$app_filename"; then
  73. app_index=$((app_index+1))
  74. continue
  75. fi
  76. SHORT_DESCRIPTION="$(grep "${app_name_upper}_SHORT_DESCRIPTION=" "$app_filename" | head -n 1 | sed 's|\$||g' | sed "s|'||g" | sed 's|\"||g' | awk -F '=' '{print $2}')"
  77. if grep -q "${app_name_upper}_DESCRIPTION=" "$app_filename"; then
  78. DESCRIPTION="$(grep "${app_name_upper}_DESCRIPTION=" "$app_filename" | head -n 1 | sed 's|\$||g' | sed "s|'||g" | sed 's|\"||g' | awk -F '=' '{print $2}')"
  79. fi
  80. if grep -q "${app_name_upper}_MOBILE_APP_URL=" "$app_filename"; then
  81. MOBILE_APP_URL="$(grep "${app_name_upper}_MOBILE_APP_URL=" "$app_filename" | head -n 1 | sed 's|\$||g' | sed 's|\$||g' | sed "s|'||g" | sed 's|\"||g' | awk -F '=' '{print $2}')"
  82. fi
  83. if [ $android_ctr -gt 0 ]; then
  84. echo ',' >> "$plinth_api"
  85. fi
  86. { echo ' {';
  87. echo " \"name\": \"${app_name}\",";
  88. echo " \"short_description\": \"$SHORT_DESCRIPTION\",";
  89. echo " \"description\": [";
  90. echo " \"$DESCRIPTION\"";
  91. echo ' ],';
  92. echo " \"icon_url\": \"/icons/${app_name}.png\",";
  93. echo " \"clients\": ["; } >> "$plinth_api"
  94. read_config_param "${app_name_upper}_DOMAIN_NAME"
  95. test_domain_name="${app_name_upper}_DOMAIN_NAME"
  96. domain_name=${!test_domain_name}
  97. if [ "$domain_name" ]; then
  98. if [[ "$domain_name" != *'.onion' ]]; then
  99. domain_name="https://${!test_domain_name}"
  100. else
  101. domain_name="http://${!test_domain_name}"
  102. fi
  103. fi
  104. if [[ "$domain_name" && "$app_name" != 'matrix' ]]; then
  105. { echo ' {';
  106. echo " \"name\": \"${app_name}\",";
  107. echo " \"platforms\": [";
  108. echo ' {';
  109. echo ' "type": "web",';
  110. echo " \"url\": \"$domain_name\"";
  111. echo -n ' }'; } >> "$plinth_api"
  112. fi
  113. if [ "$MOBILE_APP_URL" ]; then
  114. if [[ "$domain_name" && "$app_name" != 'matrix' ]]; then
  115. echo ',' >> "$plinth_api"
  116. else
  117. { echo ' {';
  118. echo " \"name\": \"${app_name}\",";
  119. echo " \"platforms\": ["; } >> "$plinth_api"
  120. fi
  121. store_name='f-droid'
  122. if [[ "$MOBILE_APP_URL" == *'google'* ]]; then
  123. store_name='google-play'
  124. fi
  125. { echo ' {';
  126. echo ' "type": "store",';
  127. echo ' "os": "android",';
  128. echo " \"store_name\": \"$store_name\",";
  129. echo " \"url\": \"$MOBILE_APP_URL\"";
  130. echo ' }'; } >> "$plinth_api"
  131. else
  132. echo '' >> "$plinth_api"
  133. fi
  134. { echo ' ]';
  135. echo ' }';
  136. echo ' ]';
  137. echo -n ' }'; } >> "$plinth_api"
  138. android_ctr=$((android_ctr+1))
  139. fi
  140. fi
  141. app_index=$((app_index+1))
  142. done
  143. { echo '';
  144. echo ' ]';
  145. echo '}'; } >> "$plinth_api"
  146. chown -R www-data:www-data "/var/www/${local_hostname}/htdocs/plinth"
  147. chown -R www-data:www-data "/var/www/${local_hostname}/htdocs/icons"
  148. }
  149. # NOTE: deliberately no exit 0