-
Notifications
You must be signed in to change notification settings - Fork 25
/
do_extract.sh
executable file
·68 lines (57 loc) · 1.44 KB
/
do_extract.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
#!/bin/bash
# ------- get arguments ------------------------
PACKAGE_NAME=${1}
TAR_NAME=${2}
WORK_DIR=${3}
# ------- check default ----------------------
cd $WORK_DIR
if [ -f $TAR_NAME ] ; then
case $TAR_NAME in
*.tar.bz2) TAR_OPT="jf" ;;
*.tar.gz) TAR_OPT="zf" ;;
*.tar.xz) TAR_OPT="f" ;;
*.txz) TAR_OPT="f" ;;
*.tar) TAR_OPT="f" ;;
*.tbz2) TAR_OPT="jf" ;;
*.tgz) TAR_OPT="zf" ;;
*) echo "'$TAR_NAME' cannot be extracted via extract()"; exit ;;
esac
else
echo "'$TAR_NAME' is not a valid file"
exit
fi
function do_extract {
FILE=$1
TAR_NAME=$2
PACK=$3
if [ ! -z "${PACK}" ]; then
FILE=$3
TAR_DIR=" -C ${PACK} "
mkdir -p $PACK
fi
# if tar file is already decompressed, stop
if [[ -d $FILE ]]; then
return
elif [[ -e $FILE ]]; then
return
fi
tar x$TAR_OPT $TAR_NAME $TAR_DIR > /dev/null
}
# set working directory
TAR_ROOT=`tar t${TAR_OPT} "${TAR_NAME}" | sed -e 's@/.*@@' | uniq`
if [ -z $TAR_ROOT ]; then
echo "${PACKAGE_NAME} got something wrong"
exit
fi
if [[ $TAR_ROOT == "." ]]; then
# if no root directory
TAR_ROOT=$PACKAGE_NAME
do_extract $TAR_ROOT $TAR_NAME $PACKAGE_NAME
else
# if root directory exists
do_extract $TAR_ROOT $TAR_NAME
fi
if [[ ! -d $TAR_ROOT ]]; then
echo "${PACKAGE_NAME} has no root ..."
exit
fi