#!/bin/bash

# snap-pac
# https://github.com/wesbarnett/snap-pac
# Copyright (C) 2016, 2017 James W. Barnett

# This program 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 2 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 General Public License for more details.

# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Main script.

function error_exit 
{
    exit 1
}

function kill_exit 
{
    printf "\nExited due to user intervention.\n"
    exit 1
}

trap error_exit ERR
trap kill_exit SIGTERM SIGINT

SNAPPER_CONFIG_FILE=/etc/conf.d/snapper
DESC_LIMIT=48

ERRORMSG="\033[00;31mERROR:\033[00m"
WARNINGMSG="\033[00;33mWARNING:\033[00m"

PACMAN_ABORT_ON_FAIL="no"

# Virtual console fonts don't have the unicode checkmark
if [[ "$TERM" == "linux" ]]; then
    checkmark="done"
# Ensure user is using UTF locale
elif [[ $(awk '/UTF-8/' <(echo $LANG)) ]]; then
    checkmark="✓"
else
    checkmark="done"
fi

if [[ -f "$SNAPPER_CONFIG_FILE" ]]; then
    source "$SNAPPER_CONFIG_FILE"
else
    printf "%b %s does not exist!\n" "$ERRORMSG" "$SNAPPER_CONFIG_FILE"
    exit 1
fi

if [[ $EUID -ne 0 ]]; then
    printf "%b Script must be run as root.\n" "$ERRORMSG"
    if [[ $PACMAN_ABORT_ON_FAIL == "yes" ]]; then
        exit 1
    else
        exit 0
    fi
fi

if [[ ! -d /var/run/dbus ]]; then
    printf "%b Unable to use snapper without dbus. Are you in a chroot environment?\n" "$ERRORMSG"
    if [[ $PACMAN_ABORT_ON_FAIL == "yes" ]]; then
        exit 1
    else
        exit 0
    fi
fi

if [[ $# -ne 1 ]]; then
    printf "%b Only one argument should be passed to this script.\n" "$ERRORMSG"
    if [[ $PACMAN_ABORT_ON_FAIL == "yes" ]]; then
        exit 1
    else
        exit 0
    fi
fi

if [[ $1 != "pre" ]] && [[ $1 != "post" ]] && [[ $1 != "rem" ]]; then
    printf "%b First argument should either be 'pre', 'post', or 'rem'.\n" "$ERRORMSG"
    if [[ $PACMAN_ABORT_ON_FAIL == "yes" ]]; then
        exit 1
    else
        exit 0
    fi
fi

if [[ -z "$SNAPPER_CONFIGS" ]]; then
    printf "%b No snapper configurations found, so not taking any snapshots!\n" "$ERRORMSG" 
    if [[ $PACMAN_ABORT_ON_FAIL == "yes" ]]; then
        exit 1
    else
        exit 0
    fi
fi

declare -r pre_or_post=$1
declare pacman_cmd
pacman_cmd="$(sed 's./usr/bin/pacman.pacman.g' <(ps -C pacman -o args=))"

if [[ -z "$pacman_cmd" ]]; then
    printf "%b Didn't find pacman running.\n" "$WARNINGMSG"
    pacman_cmd="pacman"
fi

declare -i x=0
for CONFIG in $SNAPPER_CONFIGS; do

    # Set defaults
    PACMAN_PRE_POST="no"
    if [[ $CONFIG == "root" ]]; then
        PACMAN_PRE_POST="yes"
    fi
    PACMAN_PRE_DESCRIPTION="$pacman_cmd"
    PACMAN_POST_DESCRIPTION="$pacman_cmd"
    PACMAN_CLEANUP_ALGORITHM="number"

    # Source snapper configuration to override defaults
    source /etc/snapper/configs/"$CONFIG"

    if [[ $PACMAN_PRE_POST == "yes" ]]; then 

        prefile="/tmp/snap-pac-pre_$CONFIG"
        snapper_cmd="snapper --config $CONFIG create --type $pre_or_post --cleanup-algorithm $PACMAN_CLEANUP_ALGORITHM"

        x=$((x+1))

        if [[ "$pre_or_post" == "pre" ]]; then

            printf "    %s  " "$CONFIG"

            if [[ "${#PACMAN_PRE_DESCRIPTION}" -gt $DESC_LIMIT ]]; then
                PACMAN_PRE_DESCRIPTION="$(echo $PACMAN_PRE_DESCRIPTION | cut -c 1-$DESC_LIMIT)..."
            fi

            $snapper_cmd --description "$PACMAN_PRE_DESCRIPTION" --print-number > "$prefile"
            printf "%s %s\n" "$(< "$prefile")" "$checkmark"

        elif [[ "$pre_or_post" == "post" ]]; then

            if [[ -f $prefile ]]; then

                printf "    %s  " "$CONFIG"

                if [[ "${#PACMAN_POST_DESCRIPTION}" -gt $DESC_LIMIT ]]; then
                    PACMAN_POST_DESCRIPTION="$(echo $PACMAN_POST_DESCRIPTION | cut -c 1-$DESC_LIMIT)..."
                fi

                postnum=$($snapper_cmd --description "$PACMAN_POST_DESCRIPTION" --print-number --pre-number "$(< "$prefile")")
                printf "%s %s\n" "$postnum" "$checkmark"
                rm "$prefile"

            else

                printf "%b %s does not exist, so no post snapshot for %s will be taken. If you are initially installing snap-pac, this is normal.\n" "$WARNINGMSG" "$prefile" "$CONFIG"

            fi

        elif [[ "$pre_or_post" == "rem" ]]; then

            if [[ -f $prefile ]]; then
                rm "$prefile"
            fi

        fi

    fi

done

if [[ $x -eq 0 ]]; then
    printf "%b No snapper configurations are set up for snapshots to be taken!\n" "$ERRORMSG"
    if [[ $PACMAN_ABORT_ON_FAIL == "yes" ]]; then
        exit 1
    else
        exit 0
    fi
fi

exit 0
