more verbosity, better error handling, more functions

This commit is contained in:
2015-06-16 16:34:52 +03:00
parent 2d9ad804bb
commit c613d45c22

View File

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