#!/usr/bin/env bash

# Update to bigbashview after 2024
/usr/share/biglinux/webapps/update_old_desktop_files.sh

# After this line run old verifications:

# The folder where the .desktop files will be saved
Folder="$HOME/.local/share/applications"

# Creates the folder if it doesn't already exist
if [ ! -e "$Folder" ]; then
    mkdir -p $Folder
fi

# Change the current directory to the folder
cd "$Folder"

# Search if there is a default webapp active
for file in *webapp-biglinux.desktop; do
    # Stop the loop after finding one
    if [ -e "$file" ]; then
        Found=true
        break
    fi
done

# Change the icon location to the generic name, remove this in future, fix to update in 2023-08-30
if [ "$Found" = true ]; then
    if grep -q '/usr/share/icons/hicolor/128x128/apps/' *webapp-biglinux.desktop; then
        sed -Ei 's/Icon=\/usr\/share\/icons\/hicolor\/128x128\/apps\/(.*)\..*/Icon=webapp-\1/g' *-webapp-biglinux.desktop
    fi
fi

# If there is no default webapp active and the file /etc/biglinux-webapps-not-create-default does not exist, copy the default webapps
if [[ "$Found" != true && ! -e "/etc/biglinux-webapps-not-create-default" && ! -e "$HOME/.config/biglinux-webapps-not-create-default" ]]; then
    cp /usr/share/bigbashview/bcc/apps/biglinux-webapps/webapps/* .

    # On first run create file to not recreate in next boot
    > ~/.config/biglinux-webapps-not-create-default
fi
