-
Notifications
You must be signed in to change notification settings - Fork 0
/
unrar_parts.sh
executable file
·126 lines (109 loc) · 2.98 KB
/
unrar_parts.sh
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
#############################################
# U N R A R _ P A R T S #
# ----------------------------------------- #
# Author: Jan Lipovský, janlipovsky.cz #
# E-mail: janlipovsky@gmail.com #
# Licence: MIT #
# https://github.com/lipoja/unrar_parts.git #
#############################################
# First test if unrar is installed
# Test if unrar is installed
tmp=`which unrar`
if [ $? -ne 0 ]; then
echo "ERROR: unrar is not installed or is not in PATH"
exit 1
fi
# GLOBALS #
INSTALL_PATH="/usr/bin/unrar_parts"
PWDir=`pwd`
if [ "$#" -eq 0 ]; then
DIRECTORY=`pwd`"/"
fi
tmp=`which whoami`
if [ $? -ne 0 ]; then
echo "WARNING: whoami is not installed or is not in PATH"
SUDO="sudo"
else
if [ "$(whoami)" != "root" ]; then
SUDO="sudo"
else
SUDO=""
fi
fi
# catching sigint
trap ctrl_c INT
function ctrl_c() {
echo "** Script stopped by user CTRL-C"
cd "$PWDir"
exit 1
}
function myinstall () {
$SUDO echo -ne "Installing unrar_parts: ... "
$SUDO cp -f "$0" $INSTALL_PATH
echo "DONE"
exit 0
}
function myuninstall () {
$SUDO echo -ne "Removing unrar_parts from system: ... "
$SUDO rm -rf $INSTALL_PATH
echo "DONE"
exit 0
}
show_help () {
echo "Usage: unrar_parts [options] [direcotry]"
echo "------------------------------------------------------------------"
echo -e "directory\t Path to directory with rar files splitted to parts"
echo -e "\t\t If not set current directory will be used (pwd)"
echo -e "--install\t Copy script to '$INSTALL_PATH'"
echo -e "--uninstall\t Remove script '$INSTALL_PATH'"
echo -e "--help, -h\t Show this text"
exit 0
}
while [ "$#" != "0" ]; do
if [ "$1" == "--install" ]; then
myinstall
elif [ "$1" == "--uninstall" ]; then
myuninstall
elif [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
show_help
else
DIRECTORY="$1"
fi
shift
done
if [ -d "$DIRECTORY" ]; then
cd "$DIRECTORY"
shopt -s nullglob
PROCESSED_FILES=0
for file in `ls | grep "part\([0]*\)1"`
do
PROCESSED_FILES=$[PROCESSED_FILES+1]
echo "Unpacking file $file"
unrar -o+ e "$file"
ret=$?
if [ $ret -eq 0 ]; then
for rmf in `echo $file | sed 's/part\([0]*\)1/part\*/g'`
do
echo "Deleting file: $rmf"
rm -f "$rmf"
done
else
echo "ERROR: Could not extract file: $file"
fi
done
if [ $PROCESSED_FILES -eq 0 ]; then
echo "Nothing to process. No files with \"part1.rar\" were found."
else
if [ $PROCESSED_FILES -eq 1 ]; then
echo "Processed $PROCESSED_FILES group of rar files"
else
echo "Processed $PROCESSED_FILES groups of rar files"
fi
fi
else
echo "ERROR: Directory does not exist [$DIRECTORY]"
cd "$PWDir"
exit 1
fi
cd "$PWDir"