| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 | #!/bin/bash
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
#
#                              Freedom in the Cloud
#
# Integration with the FreedomBox android app
#
# License
# =======
#
# Copyright (C) 2018 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
function android_update_apps {
    if [ "$1" ]; then
        detect_installable_apps
    fi
    local_hostname=$(grep 'host-name' /etc/avahi/avahi-daemon.conf | awk -F '=' '{print $2}').local
    plinth_api="/var/www/${local_hostname}/htdocs/plinth/api/1"
    if [ ! -d "/var/www/${local_hostname}/htdocs/plinth/api" ]; then
        mkdir -p "/var/www/${local_hostname}/htdocs/plinth/api"
    fi
    echo '{' > "$plinth_api"
    echo '  "shortcuts": [' >> "$plinth_api"
    android_ctr=0
    app_index=0
    # shellcheck disable=SC2068
    for a in ${APPS_INSTALLED[@]}
    do
        if [[ "$a" == "1" ]]; then
            app_name=${APPS_INSTALLED_NAMES[$app_index]}
            app_filename="/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-${app_name}"
            if [ -f "$app_filename" ]; then
                app_name_upper=$(echo "$app_name" | awk '{print toupper($0)}')
                SHORT_DESCRIPTION=
                DESCRIPTION=
                ICON_URL=
                MOBILE_APP_URL=
                if ! grep -q "${app_name_upper}_SHORT_DESCRIPTION=" "$app_filename"; then
                    continue
                fi
                if grep -q "#${app_name_upper}_SHORT_DESCRIPTION=" "$app_filename"; then
                    continue
                fi
                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}')"
                if grep -q "${app_name_upper}_DESCRIPTION=" "$app_filename"; then
                    DESCRIPTION="$(grep "${app_name_upper}_DESCRIPTION=" "$app_filename" | head -n 1 | sed 's|\$||g' | sed "s|'||g" | sed 's|\"||g' | awk -F '=' '{print $2}')"
                fi
                if grep -q "${app_name_upper}_ICON_URL=" "$app_filename"; then
                    ICON_URL="$(grep "${app_name_upper}_ICON_URL=" "$app_filename" | head -n 1 | sed 's|\$||g' | sed 's|\$||g' | sed "s|'||g" | sed 's|\"||g' | awk -F '=' '{print $2}')"
                fi
                if grep -q "${app_name_upper}_MOBILE_APP_URL=" "$app_filename"; then
                    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}')"
                fi
                if [ $android_ctr -gt 0 ]; then
                    echo ',' >> "$plinth_api"
                fi
                { echo '    {';
                  echo "      \"name\": \"${app_name}\",";
                  echo "      \"short_description\": \"$SHORT_DESCRIPTION\",";
                  echo "      \"description\": \"$DESCRIPTION\",";
                  echo "      \"icon_url\": \"$ICON_URL\",";
                  echo "      \"clients\": ["; } >> "$plinth_api"
                read_config_param "${app_name_upper}_DOMAIN_NAME"
                test_domain_name="${app_name_upper}_DOMAIN_NAME"
                domain_name=${!test_domain_name}
                if [ "$domain_name" ]; then
                    if [[ "$domain_name" != *'.onion' ]]; then
                        domain_name="https://${!test_domain_name}"
                    else
                        domain_name="http://${!test_domain_name}"
                    fi
                fi
                if [[ "$domain_name" && "$app_name" != 'matrix' ]]; then
                    { echo '        {';
                      echo "          \"name\": \"${app_name}\",";
                      echo "          \"platforms\": [";
                      echo '            {';
                      echo '              "type": "web",';
                      echo "              \"url\": \"$domain_name\"";
                      echo -n '            }'; } >> "$plinth_api"
                fi
                if [ "$MOBILE_APP_URL" ]; then
                    if [[ "$domain_name" && "$app_name" != 'matrix' ]]; then
                        echo ',' >> "$plinth_api"
                    else
                        { echo '        {';
                          echo "          \"name\": \"${app_name}\",";
                          echo "          \"platforms\": ["; } >> "$plinth_api"
                    fi
                    { echo '            {';
                      echo '              "type": "store",';
                      echo '              "os": "android",';
                      echo '              "store_name": "f-droid",';
                      echo "              \"url\": \"$MOBILE_APP_URL\"";
                      echo '            }'; } >> "$plinth_api"
                else
                    echo '' >> "$plinth_api"
                fi
                { echo '          ]';
                  echo '        }';
                  echo '      ]';
                  echo -n '    }'; } >> "$plinth_api"
                android_ctr=$((android_ctr+1))
            fi
        fi
        app_index=$((app_index+1))
    done
    { echo '';
      echo '  ]';
      echo '}'; } >> "$plinth_api"
    chown -R www-data:www-data "/var/www/${local_hostname}/htdocs/plinth"
}
# NOTE: deliberately no exit 0
 |