#!/bin/sh

if [ "$(id -u)" != "0" ]; then
    echo "You must be root"
    exit 1
fi

needarg() {
    if [ ! "$1" ]; then
        echo ":: ERROR: argument is needed for this option!"
        exit 1
    fi
}

print_help() {
    printf "usage:\n\t$0 <options>\noptions:\n\t-c,--create - create backup\n\t-r,--restore - restore backup\n\t-d,--disk,--directory <disk or directory> - backup or restore disk or directory\n\t-f,--file,--filename <filename> - filename to backup or restore\n\t-h,--help - this help message\n"
}

create_backup() {
    if [ -d $1 ]; then
        directory=$1
        filename=$2
        CREATOR=$(command -v mksquashfs) || CREATOR=$(command -v tar)
        case $CREATOR in
            /usr/bin/mksquashfs|/bin/mksquashfs)
                $CREATOR $directory $filename
                ;;
            /usr/bin/tar|/bin/tar)
                $CREATOR -cvf $filename $directory
                ;;
        esac
    elif [ -b $1 ]; then
        disk=$1
        filename=$2
        dd if=$disk status=progress | gzip -9 > $filename
    elif [ -c $1 ]; then
        disk=$1
        filename=$2
        dd if=$disk status=progress | gzip -9 > $filename
    else
        echo "Can't find '$1'"
        exit 1
    fi
}

restore_backup() {
    if [ ! -f $1 ]; then
        echo "Can't find '$1' or it's not file"
        exit 1
    fi
    if [ -d $2 ]; then
        filename=$1
        directory=$2
        RESTORER=$(command -v unsquashfs) || RESTORER=$(command -v tar)
        case $RESTORER in
            /usr/bin/unsquashfs|/bin/unsquashfs)
                $RESTORER -f -d $directory $filename
                ;;
            /usr/bin/tar|/bin/tar)
                $RESTORER -xvf $filename -C $directory
                ;;
        esac
    elif [ -b $2 ]; then
        archive=$1
        disk=$2
        gzip -dc $archive | dd of=$disk status=progress
    elif [ -c $2 ]; then
        archive=$1
        disk=$2
        gzip -dc $archive | dd of=$disk status=progress
    else
        echo "Can't find '$2'"
        exit 1
    fi
}

parse_opt() {
    while [ $1 ]; do
        case $1 in
            -c|--create)
                do=0
                ;;
            -r|--restore)
                do=1
                ;;
            -d|--disk|--directory)
                needarg $2
                from=$2
                shift 1
                ;;
            -f|--file|--filename)
                needarg $2
                destination=$2
                shift 1
                ;;
            -h|--help)
                print_help
                exit 0
                ;;
            *)
                echo "ERROR: invalid option '$1'"
                exit 1
                ;;
        esac
        shift
    done
}

main() {
    parse_opt "$@"
    [ "$from" = "" ] || [ "$destination" = "" ] && print_help && exit 1
    case $do in
        0)
            create_backup $from $destination
            ;;
        1)
            restore_backup $destination $from
            ;;
    esac
}

main "$@"
