summaryrefslogtreecommitdiffstats
path: root/a
blob: 998fe6e600380758a4c347b0ae9756cc210facff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
######################################################
#+ Simple archive script with multiple backends
#+ Rev. 3 - 2/07/09 - optimum.reflex@gmail.com
######################################################
hlp () { # Help function
    echo "usage: $0 [--help] [archive] [sources]"
    echo "[sources] is the files or folder you want added to the archive."
    echo
    echo "[archive] is the name of the archive generated, including extension."
    echo "Extensions are: .7z .zip .bz2 .gz .lzma .tar.*"
    exit 0
}

if [ $# -lt 2 ]; then # Need atleast an archive and one source
    hlp
fi

tarfunc () { # function that does the work
    MSG="packing [$SRCD] to [$DIR/$TAR]... "
    case "$TAR" in
        *.7z )
        echo -n $MSG
        /usr/bin/7z a -mx=9 $DIR/$TAR $SRC
        ;;
        *.t* )
        echo -n $MSG
        /bin/tar -acZf $DIR/$TAR $SRC
        ;;
        *.bz2 )
        echo -n $MSG
        /bin/bzip2 -cq9 $SRC > $DIR/$TAR
        ;;
        *.gz )
        echo -n $MSG
        /bin/gzip -cq9 $SRC > $DIR/$TAR
        ;;
        *.zip )
        echo -n $MSG
        /usr/bin/zip -qr9 $DIR/$TAR $SRC
        ;;
        *.lzma )
        echo -n $MSG
        /usr/bin/lzma -cq9 $SRC > $DIR/$TAR
        ;;
        *)
        hlp;;
    esac
}

tdir () { # Determin target directory for archive
    if [ -d $1 ]; then
        if [ "$1" == "." ]; then
            DIR="$PWD"
        else
            DIR="$1"
        fi
    else
        DIR="$PWD"
    fi
}

case "$@" in
    *--help*) hlp;;
esac

TAR="`basename $1`"
tdir `dirname $1` && shift

if [ $# -gt 1 ]; then # If more than one source
    SRCD="$@" # all sources to $SRCD
    i=0 # counter
    while [ "$1" ]; do
        if [ $i -eq 0 ]; then # only if the first source
            SRC="$1" && shift
            if [ ! -r "$SRC" ]; then
                echo "Location [$SRC] does not exist or is unreadable."
                exit 1
            fi
            ((i++)) # increment
        else # if sources after the first
            if [ ! -r "$1" ]; then
                echo "Location [$1] does not exist or is unreadable."
                exit 1
            fi
            SRC="$SRC $1" && shift # copy current $SRC and append next source
        fi
    done
    tarfunc # do the work
else # else if there is only one source
    SRC="$1"
        SRCD="$SRC" # copy $SRC
    if [ ! -r "$SRC" ]; then
        echo "Location [$SRC] does not exist or is unreadable."; exit 1
    elif [ -d "$SRC" ]; then # if source is a directory
        cd `dirname $SRC` # goto the directory one up from $SRC
        SRC="`basename $SRC`/" # change $SRC for correct directory packing structure
    fi
    tarfunc # do the work
fi

if [ $? -ne 0 ]; then # if last command failed
    cd $DIR
    rm -f $TAR && echo "failure detected, archive deleted."
    exit 1
else
    echo "success!"; exit 0
fi