-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextractor.sh
executable file
·80 lines (72 loc) · 1.84 KB
/
extractor.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
#!/bin/bash
##Pass the directories in arguements that you would like to watch for rar files
echo "$(date): Starting Extractor"
#Get RAR Archives
#Test the archive
#If passed continue
#Get List of files and check if they exist
#If they pass skip archive
#If they dont extract file
function is_valid()
{
extract_dir=`dirname $1`
if [ -f $extract_dir/.extracted ];
then
return 1
fi
lsar -test $1 >> /dev/null
return $?
}
function isExtracted(){
local file
extract_dir=`dirname $1`
files=`lsar $1 | awk '{if(NR>1)print}'`
count=0
found_count=0
for file in $files;
do
((count++))
if [ -f $(dirname $1)/$file ];
then
((found_count++))
fi
done
#Send Result
if [ $count -eq $found_count ];
then
touch $extract_dir/.extracted
return 0
else
return 1
fi
}
function extract()
{
extract_dir=`dirname $1`
unar -o "$extract_dir" $1
if [ $? -eq 0 ];
then
echo Extracted $extract_dir
touch $extract_dir/.extracted
fi
}
while true;
do
for watch_dir in "$@";
do
archives=`find $watch_dir -name *.rar`
for file in $archives;
do
if is_valid $file;
then
if ! isExtracted $file
then
extract $file
else
echo Skipping $file
fi
fi
done
done
sleep 5
done