#!/bin/bash

# beesu_requires "mate-text-editor" "mate-text-editor"
if [ ! -x /usr/bin/pluma ]; then
    beesu_requires "mate-text-editor" "pluma (or, depending on available package for your Fedora version, beesu - yum install mate-text-editor)"
fi

#####

if [ "x$HOME" == "x" ]
then
    echo "No HOME set."
    zenity --error --text="No HOME set."
    exit
fi

export BEESU_BASE_SCRIPT_FOLDER="$HOME/.config/caja/scripts/beesu"

echo -ne "BASE_SCRIPT_FOLDER=$BEESU_BASE_SCRIPT_FOLDER\n"

#####

beesu__manage_script()
{
    # $1 = command (0 uninstall; 1 install)
    # $2 = script name
    command="$1"
    script_name="$2"
    script_path="$BEESU_BASE_SCRIPT_FOLDER/$script_name"
    echo -n "\"$script_name\" ---> "
    if [ "$command" -eq "0" ]
    then
        if [ -a "$script_path" ]
        then
            rm -f "$script_path"
        fi
        echo "removed"
    fi
    if [ "$command" -eq "1" ]
    then
        if [ -a "$script_path" ]
        then
            echo "updated"
        else
            echo "installed"
        fi
        echo '#!/bin/bash' > "$script_path"
        echo "BEESU_SCRIPT_NAME=\"$script_name\"" >> "$script_path"
        cat "/usr/libexec/caja-beesu-manager/local-launcher" >> "$script_path"
    fi
}

##### MAIN #####

if [ "`id -u`" -eq "0" ]
then
    zenity --question --title "Alert" --text="It's unusual to run this script as root!\n\nPress OK to EXIT now."
    if [ "$?" -ne "1" ]
    then
        exit
    fi
fi

#####

were_scripts_installed="0"
if [ -d "$BEESU_BASE_SCRIPT_FOLDER" ]
then
    were_scripts_installed="1"
fi

#####


pushd "/usr/libexec/caja-beesu-manager/scripts/" >/dev/null 2>&1
list_of_scripts="`find . -type f | sort | sed 's/^\.\///g'`"
list_of_dirs="`find . -type d | sort | sed 's/^\.\///g' | sed 's/^\.$//g'`"
popd >/dev/null 2>&1

#echo "$list_of_scripts" >&2
#echo "$list_of_dirs" >&2

#####

outfile=`mktemp`

echo 'zenity --list --checklist --multiple --width=530 --height=450 --separator="\n" \' >> "$outfile"
echo '--title="BEESU: caja scripts manager" \' >> "$outfile"
echo '--text="Select the caja scripts you want to install.\nIf you want to uninstall some, uncheck them." \' >> "$outfile"
echo '--column "Install me" --column "Script name" \' >> "$outfile"
echo "$list_of_scripts" | while read script_name
do
    if [ -a "$BEESU_BASE_SCRIPT_FOLDER/$script_name" ]
    then
        echo -n "true" >> "$outfile"
    else
        echo -n "false" >> "$outfile"
    fi
    echo -n " \"$script_name\" " >> "$outfile"
done

code=`cat "$outfile"`

rm -f "$outfile"

#####

list=`eval "$code"`

if [ "$?" -eq "1" ]
then
    exit
fi

#echo "$list" >&2

#####

mkdir -v -p "$BEESU_BASE_SCRIPT_FOLDER"

echo "$list_of_dirs" | while read script_dir
do
    if [ "x$script_dir" != "x" ]
    then
        mkdir -v -p "$BEESU_BASE_SCRIPT_FOLDER/$script_dir"
    fi
done

#####

echo "$list_of_scripts" | while read script_name
do
    to_install=`echo "$list" | grep -c "$script_name"`
    if [ "$to_install" -ne "0" ]
    then
        beesu__manage_script "1" "$script_name"
    else
        beesu__manage_script "0" "$script_name"
    fi
done

#####

chmod -R 755 "$BEESU_BASE_SCRIPT_FOLDER"

#####

echo "$list_of_dirs" | while read script_dir
do
    if [ "x$script_dir" != "x" ]
    then
        rmdir -v --ignore-fail-on-non-empty "$BEESU_BASE_SCRIPT_FOLDER/$script_dir"
    fi
done

rmdir -v --ignore-fail-on-non-empty "$BEESU_BASE_SCRIPT_FOLDER"

#####

if [ -d "$BEESU_BASE_SCRIPT_FOLDER" ]
then
    info="\n\nThe following scripts are now installed:\n\n$list"
    if [ "$were_scripts_installed" -eq "1" ]
    then
        zenity --info --text="Beesu's caja scripts have been updated.$info"
    else
        zenity --info --text="Beesu's caja scripts have been installed.$info"
    fi
    #gnome-open "$BEESU_BASE_SCRIPT_FOLDER"
else
    if [ "$were_scripts_installed" -eq "1" ]
    then
        zenity --info --text="Beesu's caja scripts have been removed."
    fi
fi

