#!/usr/bin/bash

# tdmctl: a tool to control and configure tdm.

# This file is part of tdm, a tiny display manager.
#
# tdm is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# tdm 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with tdm.  If not, see <http://www.gnu.org/licenses/>.

function load_confdir_path() {
    # usage: load_confdir_path
    for cfd in "${XDG_CONFIG_HOME:-$HOME/.config}/tdm" "$HOME/.tdm" ; do
        if [ -d "$cfd" ] ; then
            echo "$cfd"
            return
        fi
    done
    echo "${XDG_CONFIG_HOME:-$HOME/.config}/tdm"
}

init(){
    [[ "$1" = "-f" || "$1" = "--force" ]]&&rm -rf "${CONFDIR}"
    # build the directory tree if not exist
    if [[ ! -d "${CONFDIR}" ]]; then
        cp -Rv "${PREFIX}/share/tdm" "${CONFDIR}"
        [ ! -d "${CONFDIR}/extra" ] && mkdir "${CONFDIR}/extra"
    else
        echo "Nothing done (configuration directory already present)."
        echo "If you were asked to run 'tdmctl init', please check your configuration or remove '${CONFDIR}' and re-run 'tdmctl init'"
    fi
}

usage(){
    echo "tdmctl init: initialize the config directory."
    echo "tdmctl list: list available sessions  (X and extra)."
    echo "tdmctl cache: list cached files."
    echo "tdmctl check [extra/]<session>: see what <session> is."
    echo "tdmctl default [session]: show/set default X session."
    echo "tdmctl add <name> <path> [X(default)/extra]: add a session."
    echo "tdmctl remove <name>"
    echo "tdmctl enable/disable <session>: enable/disable session."
    echo "tdmctl migrate"
    exit
}

verify(){
    if [ ! -d "${CONFDIR}" ] ; then
        echo "Configuration directory '${CONFDIR}' does not exist"
        echo "Please run 'tdmctl init' first"
        exit
    fi
    if [ ! -d "${CONFDIR}/sessions" ] ; then
        echo "Configuration directory '${CONFDIR}' does not exist"
        echo "Please fix your configuration"
        exit
    fi
    if [ ! -d "${CONFDIR}/extra" ] ; then
        echo "Configuration directory '${CONFDIR}/extra' does not exist"
        echo "Please fix your configuration"
        exit
    fi
}


check(){
    readlink "$1" || cat "$1" 2>/dev/null || echo "$1 not found or missing read permission"
}

function migrate() {
    if [ "$CONFDIR" != "${XDG_CONFIG_HOME:-$HOME/.config}/tdm" ] ; then
        mv -v "$CONFDIR" "${XDG_CONFIG_HOME:-$HOME/.config}/tdm"
    else
        echo "A configuration is already present in '${XDG_CONFIG_HOME:-$HOME/.config}/tdm'. Please migrate manually."
    fi
}

CONFDIR="$(load_confdir_path)"
CACHEDIR="${CONFDIR}/cache"
PREFIX=/usr

if [ "$CONFDIR" == "$HOME/.tdm" ] ; then
    echo "Following tdm v1.3.0, the configuration files should be moved to '${XDG_CONFIG_HOME:-$HOME/.config}/tdm' in order to become XDG compliant"
    echo "Use 'tdmctl migrate' to automatically migrate your configuration to the new location"
    echo "Support for the old configuration directory ($CONFDIR) will be dropped in tdm 2.x.y"
fi

if [ ! -n "$1" ]; then
    usage
    exit
fi


case "$1" in
    init)
        shift
        init "$@"
        ;;
    list)
        verify
        for session in "$CONFDIR/sessions"/*; do
            [ -x "$session" ] && basename "$session"
        done
        for session in "$CONFDIR/extra"/*; do
            [ -x "$session" ] && echo "extra/$(basename "$session")"
        done
        ;;
    cache)
        verify
        for file in "$CACHEDIR"/*; do
            fn=$(basename "$file")
            echo "${fn:1}"
        done
        ;;
    default)
        verify
        if [ ! -n "$2" ]; then
            if [ ! -L "$CONFDIR/default" ] ; then
                echo "No default session found"
                exit 1
            fi
            echo "Checking $(readlink "$CONFDIR/default")"
            check "$(readlink "$CONFDIR/default")"
        else
            if [[ "$2" == extra/* ]] ; then
                FILE="$CONFDIR/$2"
            else
                FILE="$CONFDIR/sessions/$2"
            fi

            if [ -x "$FILE" ]; then
                echo "Setting default to $2"
                ln -sf "$FILE" "$CONFDIR/default"
            else
                echo "tdmctl error: $2 is not available"
            fi
        fi
        ;;
    check)
        verify
        if [ ! -n "$2" ]; then
            usage
        fi
        if [[ "$2" == extra/* ]] ; then
            FILE="$CONFDIR/$2"
        else
            FILE="$CONFDIR/sessions/$2"
        fi
        if [ -f "$FILE" ]; then
            check "$FILE"
        else
            echo "$2 not exist!"
            exit 1
        fi
        ;;
    add)
        verify
        [ -n "$3" ]||usage
        if [[ "$4" == "X" || "$4" == "" ]]; then
            ln -s "$3" "${CONFDIR}/sessions/$2"
        elif [ "$4" == "extra" ]; then
            ln -s "$3" "${CONFDIR}/extra/$2"
        else
            usage
        fi
        ;;
    remove)
        verify
        [ -n "$2" ] || usage
        if [ -L "${CONFDIR}/sessions/$2" ]; then
            rm -v "${CONFDIR}/sessions/$2"
        fi
        if [ -L "${CONFDIR}/extra/$2" ]; then
            rm -v "${CONFDIR}/extra/$2"
        fi
        ;;
    enable)
        verify
        if [ -L "${CACHEDIR}/X$2" ]; then
            mv -v "${CACHEDIR}/X$2" "${CONFDIR}/sessions/$2"
        fi
        if [ -L "${CACHEDIR}/E$2" ]; then
            mv -v "${CACHEDIR}/E$2" "${CONFDIR}/extra/$2"
        fi
        ;;
    disable)
        verify
        if [ ! -d "${CACHEDIR}" ]; then
            mkdir -p "${CACHEDIR}"
        fi
# backup to cache
        if [ -L "${CONFDIR}/sessions/$2" ]; then
            mv -v "${CONFDIR}/sessions/$2" "${CACHEDIR}/X$2"
        fi
        if [ -L "${CONFDIR}/extra/$2" ]; then
            mv -v "${CONFDIR}/extra/$2" "${CACHEDIR}/E$2"
        fi
        ;;
    migrate)
        migrate
        ;;
    *)
        usage
        ;;
esac
