#!/bin/bash
# ====================================
#       TRYGGLEIKSKOPI / BACKUP
# ====================================


# For bærbare datamaskiner (klienter)
# ===================================

# Me må vera i heimemappa

brukar=$USER
heim=$HOME
echo "Set upp tryggleikskopisystem for brukar $brukar"
echo "i heimemappa $heim. OK? Skriv anna brukarnamn um du vil [ctrl+c for å avbryta]"
read b
if [[ "x$b" != "x" ]]; then
    brukar=$b
    heim="/home/$b"
    echo "Brukar '$brukar' og '$heim' i staden"
fi

if [ "x$1" == "x-r" ]; then
    echo "Sikker på at du vil fjerna backup-skript osb?"
    echo "Du mistar alle innstillingar. [ctrl+c for å avbryta]"
    read a
    sudo rm -rf /etc/network/if-up.d/dobackup $heim/.backup/ $heim/.ssh/backup
    sudo sed -i 's/DISPLAY.*$//' /etc/acpi/thinkpad-thinkpad.sh
    if [ "x$(gconftool-2 -g /apps/metacity/keybinding_commands/command_1 | grep dobackup)" == "x" ]; then
        echo "> Fjerner gconf-kommando"
        gconftool-2 -s /apps/metacity/keybinding_commands/command_1 -t string ""
    fi

    echo
    echo "> Fjerna backupsystem!"
    exit
fi

echo "" > log.setup

# På klienten lagrar du backup-nykelen i .ssh på heimemappa til brukaren.

    scp root@okse.neitileu.no:.ssh/backup $heim/.ssh/

# Innstaller libnotify

    sudo apt-get --yes install libnotify-bin

# Innstallerer skriptet «dobackup» i .backup/ og link til det frå /etc/network/if-up.d/

    mkdir $heim/.backup/
    scp root@okse.neitileu.no:/home/backup/dobackup $heim/.backup/
    sudo ln -s $heim/.backup/dobackup /etc/network/if-up.d/

# Endra brukar i skriptet

    sed -i "s/%USER%/$brukar/" $heim/.backup/dobackup

# Set upp tastaturknappen til tvungen tryggleikskopiering.
if [ "x" == "x$(grep dobackup /etc/acpi/thinkpad-thinkpad.sh)" ]; then
    sudo bash -c "echo \"DISPLAY=:0.0 su $brukar -c \\\"/bin/bash $heim/.backup/dobackup -f > /tmp/sistbackup\\\"\" >> /etc/acpi/thinkpad-thinkpad.sh"
else
    echo ">> thinkpad-skript hev allereide dobackup installert" >> log.setup
fi
    #sudo setkeycodes e073 148
    #sudo bash -c 'echo "acpi_fakekey 155" >> /etc/acpi/thinkpad-thinkpad.sh'
    #gconftool-2 -s /apps/metacity/global_keybindings/run_command_1 -t string "<Control>F1"
    #gconftool-2 -s /apps/metacity/keybinding_commands/command_1 -t string "$heim/.backup/dobackup -f"

echo
echo
cat log.setup
echo "Ferdig... :)"
