more verbosity, better error handling, more functions
This commit is contained in:
173
split-dl.sh
173
split-dl.sh
@@ -11,10 +11,52 @@ if [ "$#" -lt "2" ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# # # #
|
||||
# FUNCTIONS
|
||||
# # # #
|
||||
function checkwait {
|
||||
read -p "Continue? Press Ctrl+C to cancel." END
|
||||
}
|
||||
|
||||
function getmd5 {
|
||||
md5sum $1 | awk -F" " '{ print $1 }'
|
||||
}
|
||||
|
||||
function compmd5 {
|
||||
HASH1=$(getmd5 $1)
|
||||
HASH2=$2
|
||||
if [ "$HASH1" == "$HASH2" ]; then
|
||||
echo "true"
|
||||
else
|
||||
echo "false"
|
||||
fi
|
||||
}
|
||||
|
||||
function getdu {
|
||||
du $1 | awk -F" " '{ print $1 }'
|
||||
}
|
||||
|
||||
function compdu {
|
||||
DU1=$(getdu $1)
|
||||
DU2=$2
|
||||
if [ "$DU1" == "$DU2" ]; then
|
||||
echo "true"
|
||||
else
|
||||
echo "false"
|
||||
fi
|
||||
}
|
||||
|
||||
function compall {
|
||||
FILE=$1
|
||||
SIZE=$2
|
||||
MD5=$3
|
||||
if $(compdu $FILE $SIZE) && $(compmd5 $FILE $MD5); then
|
||||
echo="true"
|
||||
else
|
||||
echo="false"
|
||||
fi
|
||||
}
|
||||
|
||||
function checkfolder {
|
||||
FOLDER=$1
|
||||
# Check if folder already exists. If not, create it
|
||||
@@ -37,6 +79,7 @@ function checkfolder {
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# # # #
|
||||
# SERVER MODE
|
||||
# # # #
|
||||
@@ -82,8 +125,8 @@ INFO="$FOLDER/$INFO"
|
||||
|
||||
echo "[INFO] Calculating size and MD5 sum..."
|
||||
BIGNAME=$(basename $FILE)
|
||||
BIGSIZE=$(du $FILE | awk -F" " '{ print $1 }')
|
||||
BIGMD5=$(md5sum $FILE | awk -F" " '{ print $1 }')
|
||||
BIGSIZE=$(getdu $FILE)
|
||||
BIGMD5=$(getmd5 $FILE)
|
||||
|
||||
echo "BIGNAME=$BIGNAME" >> $INFO
|
||||
echo "BIGSIZE=$BIGSIZE" >> $INFO
|
||||
@@ -97,13 +140,12 @@ split --verbose -a 4 -b $SPLITSIZE $FILE $FOLDER/dl-
|
||||
echo "[INFO] Creating info document with necessary specs..."
|
||||
SMALLNO=$(ls $FOLDER | grep -v $INFO | wc -l)
|
||||
echo "SMALLNO=$SMALLNO" >> $INFO
|
||||
|
||||
NO=0
|
||||
ls $FOLDER | grep -v $INFO | while read -r line; do
|
||||
|
||||
SMALLNAME=$line
|
||||
SMALLSIZE=$(du $FOLDER/$SMALLNAME | awk -F" " '{ print $1 }')
|
||||
SMALLMD5=$(md5sum $FOLDER/$SMALLNAME | awk -F" " '{ print $1 }')
|
||||
SMALLSIZE=$(getdu $FOLDER/$SMALLNAME)
|
||||
SMALLMD5=$(getmd5 $FOLDER/$SMALLNAME)
|
||||
|
||||
echo "SMALLNAME[$NO]=$SMALLNAME" >> $INFO
|
||||
echo "SMALLSIZE[$NO]=$SMALLSIZE" >> $INFO
|
||||
@@ -150,59 +192,92 @@ echo
|
||||
|
||||
checkfolder $FOLDER # Checks and creates folder
|
||||
|
||||
for ((i = 0; i < ${#SMALLNAME[*]}; i++)); do
|
||||
SMALLNAME=${SMALLNAME[$i]}
|
||||
SMALLSIZE=${SMALLSIZE[$i]}
|
||||
SMALLMD5=${SMALLMD5[$i]}
|
||||
until [ "$STATUS" == "F" ]; do
|
||||
for ((i = 0; i < ${#SMALLNAME[*]}; i++)); do
|
||||
SMALLNAME=${SMALLNAME[$i]}
|
||||
SMALLSIZE=${SMALLSIZE[$i]}
|
||||
SMALLMD5=${SMALLMD5[$i]}
|
||||
|
||||
SMALLURL=$URL/$SMALLNAME
|
||||
SMALLPATH=$FOLDER/$SMALLNAME
|
||||
SMALLURL=$URL/$SMALLNAME
|
||||
SMALLPATH=$FOLDER/$SMALLNAME
|
||||
|
||||
let NO=i+1
|
||||
echo
|
||||
echo "------------------"
|
||||
echo "Starting file: $SMALLNAME ($SMALLSIZE bytes)"
|
||||
echo "This is file $NO of total $SMALLNO files."
|
||||
echo "Already downloaded "$(du -s $FOLDER | awk -F" " '{ print $1 }')" of $BIGSIZE bytes."
|
||||
echo
|
||||
let NO=i+1
|
||||
echo
|
||||
echo "------------------"
|
||||
echo "Starting file: $SMALLNAME ($SMALLSIZE bytes)"
|
||||
echo "This is file $NO of total $SMALLNO files."
|
||||
echo "Already downloaded "$(getdu $FOLDER)" of $BIGSIZE bytes."
|
||||
echo
|
||||
|
||||
# File doesn't exist yet, so start fresh download
|
||||
if [ ! -e $SMALLPATH ]; then
|
||||
echo "[INFO] File doesn't exits yet. Starting new download."
|
||||
wget -O $SMALLPATH $SMALLURL
|
||||
# File doesn't exist yet, so start fresh download
|
||||
if [ ! -e $SMALLPATH ]; then
|
||||
echo "[INFO] File doesn't exist yet. Starting new download."
|
||||
wget -O $SMALLPATH $SMALLURL
|
||||
if $(compall $SMALLPATH $SMALLSIZE $SMALLMD5); then
|
||||
STATUS="C"
|
||||
else
|
||||
# Downloaded file is not valid. Restart for-loop
|
||||
echo "[ERROR] Downloaded file is corrupt. Restarting."
|
||||
STATUS="E"
|
||||
break
|
||||
fi
|
||||
|
||||
# File already exists but not finished yet
|
||||
elif [ -e $SMALLPATH ] && [ $(du $SMALLPATH | awk -F" " '{ print $1 }') != $SMALLSIZE ]; then
|
||||
echo "[INFO] Continuing download."
|
||||
wget -c -O $SMALLPATH $SMALLURL
|
||||
# File already exists but not finished yet
|
||||
elif [ -e $SMALLPATH ] && ! $(compdu $SMALLPATH $SMALLSIZE); then
|
||||
echo "[INFO] Continuing download."
|
||||
wget -c -O $SMALLPATH $SMALLURL
|
||||
if $(compall $SMALLPATH $SMALLSIZE $SMALLMD5); then
|
||||
STATUS="C"
|
||||
else
|
||||
# Downloaded file is not valid. Restart for-loop
|
||||
echo "[ERROR] Downloaded file is corrupt. Restarting."
|
||||
STATUS="E"
|
||||
break
|
||||
fi
|
||||
|
||||
# File already exists, has correct size, but has wrong MD5
|
||||
elif [ -e $SMALLPATH ] && [ $(du $SMALLPATH | awk -F" " '{ print $1 }') == $SMALLSIZE ] && [ $(md5sum $SMALLPATH | awk -F" " '{ print $1 }') != $SMALLMD5 ]; then
|
||||
echo "[ERROR] MD5 is different but file has same size."
|
||||
echo "[INFO] Deleting file and starting new download."
|
||||
checkwait
|
||||
rm $SMALLPATH
|
||||
wget -O $SMALLPATH $SMALLURL
|
||||
# File already exists, has correct size, but has wrong MD5
|
||||
elif [ -e $SMALLPATH ] && $(compdu $SMALLPATH SMALLSIZE) && ! $(compmd5 $SMALLPATH $SMALLMD5); then
|
||||
echo "[ERROR] MD5 is different but file has same size."
|
||||
echo "[INFO] Deleting file and starting new download."
|
||||
checkwait
|
||||
rm $SMALLPATH
|
||||
wget -O $SMALLPATH $SMALLURL
|
||||
if $(compall $SMALLPATH $SMALLSIZE $SMALLMD5); then
|
||||
STATUS="C"
|
||||
else
|
||||
# Downloaded file is not valid. Restart for-loop
|
||||
echo "[ERROR] Downloaded file is corrupt. Restarting."
|
||||
STATUS="E"
|
||||
break
|
||||
fi
|
||||
|
||||
# File already exists, has correct size, and has correct MD5
|
||||
elif [ -e $SMALLPATH ] && [ $(du $SMALLPATH | awk -F" " '{ print $1 }') == $SMALLSIZE ] && [ $(md5sum $SMALLPATH | awk -F" " '{ print $1 }') == $SMALLMD5 ]; then
|
||||
echo "[SUCCESS] File already exists and is valid."
|
||||
if [ $NO -lt $SMALLNO ]; then
|
||||
echo "[INFO] Starting next download."
|
||||
# File already exists, has correct size, and has correct MD5
|
||||
elif [ -e $SMALLPATH ] && $(compdu $SMALLPATH $SMALLSIZE) && $(compmd5 $SMALLPATH $SMALLMD5); then
|
||||
echo "[SUCCESS] File already exists and is valid."
|
||||
|
||||
# This shouldn't happen...
|
||||
else
|
||||
echo "[SUCCESS] Downloading finished."
|
||||
echo "Dafuq?!"
|
||||
STATUS="E"
|
||||
fi
|
||||
|
||||
# This shouldn't happen...
|
||||
else
|
||||
echo "Dafuq?!"
|
||||
fi
|
||||
# Check if download is finished
|
||||
if [ $NO -lt $SMALLNO ]; then
|
||||
echo "[INFO] Starting next download."
|
||||
STATUS="C"
|
||||
elif [ $NO -lt $SMALLNO ] && [ "$STATUS" == "C" ]; then
|
||||
echo "[SUCCESS] Downloading finished."
|
||||
STATUS="F"
|
||||
else
|
||||
echo "Dafuq^2"
|
||||
fi
|
||||
|
||||
done # /for smallnames
|
||||
done
|
||||
|
||||
|
||||
read -p "Download seems to be finished. Should the splitted files be rebuilt to the original big file again? [y/n] " YN
|
||||
if [ $YN == "y" ]; then
|
||||
|
||||
# Destination file already exists
|
||||
if [ -e $BIGNAME ]; then
|
||||
read -p "Destination file already exists. Should it be overwritten? [y/n] " YN
|
||||
@@ -216,21 +291,21 @@ if [ $YN == "y" ]; then
|
||||
else
|
||||
cat $FOLDER/dl-* > $BIGNAME
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
# Check big file for MD5 and size
|
||||
sleep 2 # In rare cases, this can prevent a wrong du size
|
||||
SIZE=$(du $BIGNAME | awk -F" " '{ print $1 }')
|
||||
MD5=$(md5sum $BIGNAME | awk -F" " '{ print $1 }')
|
||||
echo "[INFO] Checking correct size and MD5 hashsum for rebuilt big file..."
|
||||
|
||||
# Compare sizes
|
||||
if [ "$SIZE" == "$BIGSIZE" ]; then
|
||||
if $(compdu $BIGNAME $BIGSIZE); then
|
||||
echo "[SUCCESS] The size of the completed file is corrent."
|
||||
else
|
||||
echo "[ERROR] The size of the completed file is incorrect."
|
||||
fi
|
||||
|
||||
# Compare MD5 sum
|
||||
if [ "$MD5" == "$BIGMD5" ]; then
|
||||
if $(compmd5 $BIGNAME $BIGMD5); then
|
||||
echo "[SUCCESS] The MD5 sum of the completed file is corrent."
|
||||
else
|
||||
echo "[ERROR] The MD5 sum of the completed file is incorrect."
|
||||
|
||||
Reference in New Issue
Block a user