#!/bin/bash

# Copyright (c) 2015-2016 Keefer Rourke <keefer.rourke@gmail.com>

# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


# this script will update various icon symlinks based on your machine's
# reported distribution and desktop environment


# formats text to lower and removes whitespace and non-alpha chars
format() {
    name=$(awk '{print tolower($0)}' <<< $1)
    sed 's/\s+//g;s/[^a-z]//g'<<< $name
}

# define variables
DISTRO=$(format "$(lsb_release -si 2>/dev/null)")
DE=$(format "$XDG_CURRENT_DESKTOP")

distro_error=0
de_error=0

wd=$PWD

# set distributor logo based on distro detected through lsb_release
cd "$wd/places/scalable"

distromatch() {
    case $DISTRO in
        arch) ln -sf distributor-logo-archlinux.svg distributor-logo.svg ;;
        debian|raspbian) ln -sf distributor-logo-debian.svg distributor-logo.svg ;;
        elementaryos) ln -sf distributor-logo-elementaryos.svg distributor-logo.svg ;;
        fedora) ln -sf distributor-logo-fedora.svg distributor-logo.svg ;;
        gentoo) ln -sf distributor-logo-gentoo.svg distributor-logo.svg ;;
        korora) ln -sf distributor-logo-korora.svg distributor-logo.svg ;;
        linuxmint) ln -sf distributor-logo-linuxmint.svg distributor-logo.svg ;;
	pearl) ln -sf distributor-logo-pearl.svg distributor-logo.svg ;;
        mageia) ln -sf distributor-logo-mageia.svg distributor-logo.svg ;;
        manjaro) ln -sf distributor-logo-manjaro.svg distributor-logo.svg ;;
	    nixos) ln -sf distributor-logo-nixos.svg distributor-logo.svg ;;
        opensuse) ln -sf distributor-logo-opensuse.svg distributor-logo.svg ;;
        ubuntu) ln -sf distributor-logo-ubuntu.svg distributor-logo.svg ;;
        *) return 1 ;;
    esac
}

# in case lsb_release detection fails, try /etc/os-release
alt_distromatch() {
    local pattern="ID=arch:ID=opensuse:ID=raspbian:ID=ubuntu:ID=nixos"
    local IFS=:
    distro_error=1
    for i in $pattern; do
        if $(grep -wsq "$i" /etc/os-release); then
            DISTRO=${i##*=}
            distromatch
            distro_error=0
            break
        fi
    done
    if [ $distro_error -eq 1 ]; then
        echo "Could not determine distribution logo."
        ln -sf distributor-logo-gnome.svg distributor-logo.svg
    fi
}

# attempt to detect distribution
! distromatch && alt_distromatch

# prompt for desktop environment integration instead of distro
read -p "Use distributor logo from desktop environment? [y/N] " yn
case $yn in
    [Yy]* )
        case $DE in
            gnome) ln -sf distributor-logo-gnome.svg distributor-logo.svg ;;
            unity) ln -sf distributor-logo-unity.svg distributor-logo.svg ;;
            xfce) ln -sf distributor-logo-xfce.svg distributor-logo.svg ;;
            lxde) ln -sf distributor-logo-lxde.svg distributor-logo.svg ;;
            kde) ln -sf distributor-logo-kde.svg distributor-logo.svg ;;
            *) 
                echo "Could not determine desktop environment... Using distribution logo."
                de_error=1 
            ;;
         esac
    ;;
    * ) if [ $distro_error -eq 0 ]; then
            echo "Using distribution logo."
        elif [ $distro_error -eq 1 ] || [ $de_error -eq 1 ]; then
            echo "Could not determine distributor logo, you may need to set this manually in places/scalable/"
        fi
    ;;
esac

ln -sf distributor-logo.svg start-here.svg # fixes potentially broken link

cd "$wd"

ln -sfr "$wd"/places/scalable/distributor-logo.svg "$wd"/apps/scalable/cs-desktop.svg
ln -sfr "$wd"/places/scalable/distributor-logo.svg "$wd"/apps/scalable/applications-system.svg

echo "Updated distributor logo."

# update mimetypes based on distro and de
cd "$wd"/mimetypes/scalable

case $DISTRO in
    opensuse) ln -sf application-x-rpm-opensuse.svg application-x-rpm.svg ;;
	fedora) ln -sf application-x-rpm-fedora.svg application-x-rpm.svg ;;
    debian|raspbian) ln -sf application-x-deb-debian.svg application-x-deb.svg ;;
    ubuntu) ln -sf application-x-deb-ubuntu.svg application-x-deb.svg ;;
    linuxmint) 
        ln -sf application-x-deb-linuxmint.svg application-x-deb.svg 
	    ln -sf application-x-desktop-linuxmint.svg application-x-desktop.svg
    ;;
    * )
	    ln -sf application-x-deb-debian.svg application-x-deb.svg
	    ln -sf application-x-rpm-redhat.svg application-x-rpm.svg
    ;;
esac

echo "Updated package archive icons."

case $DE in
    gnome) ln -sf application-x-desktop-gnome.svg application-x-desktop.svg ;;
    unity) ln -sf application-x-desktop-unity.svg application-x-desktop.svg ;;
    lxde) ln -sf application-x-desktop-lxde.svg application-x-desktop.svg ;;
    xfce) ln -sf application-x-desktop-xfce.svg application-x-desktop.svg ;;
    kde) ln -sf application-x-desktop-kde.svg application-x-desktop.svg ;;
esac

echo "Updated mimetype icons."
