#!/bin/bash FILE="$1" TYPE="$2" SPLITSIZE="50M" INFO="info.cfg" # Check if 2 params are given if [ "$#" -lt "2" ]; then echo "This script requires two arguments: File or Link AND server or client." exit 1 fi function checkwait { read -p "Continue? Press Ctrl+C to cancel." END } function checkfolder { FOLDER=$1 # Check if folder already exists. If not, create it if [ -d $FOLDER ]; then read -p "Destination folder \"$FOLDER\" already exists. Should it be emptied? [y/n]: " YN if [ $YN == "y" ]; then rm -rf $FOLDER mkdir $FOLDER else if [ "$TYPE" == "server" ]; then echo "In server-mode, this isn't recommended. Aborting." exit 1 fi fi elif [ -e $FOLDER ] && [ ! -d $FOLDER ]; then echo "Destination \"$FOLDER\" already exists but is not a folder. Please check." exit 1 else mkdir $FOLDER fi } # # # # # SERVER MODE # # # # if [ $TYPE == "server" ]; then # Check if file has spaces if [ $(echo "$FILE" | grep -q " "; echo $?) == "0" ]; then read -p "Filename has spaces. Should it be renamed? [y/n]: " YN if [ $YN == "y" ]; then # replace spaces by underscores TMPFILENAME=$(echo "$FILE" | sed "s/ /_/g") mv "$FILE" "$TMPFILENAME" FILE=$TMPFILENAME else echo "Aborting." exit 1 fi fi # Check if command is executed in directory of file or not if [ $(basename "$FILE") != "$FILE" ]; then echo "Please execute command in the same directory than the file itself." exit 1 fi # Check if file exists if [ ! -e "$FILE" ]; then echo "File does not exist. Aborting." exit 1 fi # Check if it's a file or a directory if [ ! -f "$FILE" ]; then echo "File is not a file. This script doesn't work with directories." exit 1 fi FOLDER="dl-$FILE" checkfolder $FOLDER INFO="$FOLDER/$INFO.cfg" BIGNAME=$(basename $FILE) BIGSIZE=$(du $FILE | awk -F" " '{ print $1 }') BIGMD5=$(md5sum $FILE | awk -F" " '{ print $1 }') echo "BIGNAME=$BIGNAME" >> $INFO echo "BIGSIZE=$BIGSIZE" >> $INFO echo "BIGMD5=$BIGMD5" >> $INFO echo >> $INFO split -a 4 -b $SPLITSIZE $FILE $FOLDER/dl- # List all splitted files, measure size and md5sum SMALLNO=$(ls $FOLDER | grep -v info.cfg | wc -l) echo "SMALLNO=$SMALLNO" >> $INFO NO=0 ls $FOLDER | grep -v info.cfg | while read -r line; do SMALLNAME=$line SMALLSIZE=$(du $FOLDER/$SMALLNAME | awk -F" " '{ print $1 }') SMALLMD5=$(md5sum $FOLDER/$SMALLNAME | awk -F" " '{ print $1 }') echo "SMALLNAME[$NO]=$SMALLNAME" >> $INFO echo "SMALLSIZE[$NO]=$SMALLSIZE" >> $INFO echo "SMALLMD5[$NO]=$SMALLMD5" >> $INFO let NO=NO+1 done fi # /SERVER MODE # # # # # CLIENT MODE # # # # if [ $TYPE == "client" ]; then URL=$FILE # Check if remote directory is valid and has an info.cfg file if [ $(wget -q --spider $URL/$INFO ; echo $?) != "0" ]; then echo "Remote directory does not exists or has invalid info.cfg file. Aborting." exit 1 fi if [ -e $INFO ]; then rm $INFO; fi wget -q $URL/$INFO source $INFO FOLDER="dl-$BIGNAME" # Print basic status echo echo "Total filesize: $BIGSIZE" echo "MD5: $BIGMD5" echo "Number of splitted files: $SMALLNO" echo "Download folder: "$(readlink -f $FOLDER)"" echo checkwait echo checkfolder $FOLDER # Checks and creates folder for ((i = 0; i < ${#SMALLNAME[*]}; i++)); do SMALLNAME=${SMALLNAME[$i]} SMALLSIZE=${SMALLSIZE[$i]} SMALLMD5=${SMALLMD5[$i]} 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 # 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 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, 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." wget -O $SMALLPATH $SMALLURL # 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." else echo "[SUCCESS] Downloading finished." fi # This shouldn't happen... else echo "Dafuq?!" fi 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 if [ $YN == "y" ]; then cat $FOLDER/dl-* > $BIGNAME else echo "[INFO] Skipping rebuilding." fi # Destination file doesn't exist yet else cat $FOLDER/dl-* > $BIGNAME fi fi 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 }') # Compare sizes if [ "$SIZE" == "$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 echo "[SUCCESS] The MD5 sum of the completed file is corrent." else echo "[ERROR] The MD5 sum of the completed file is incorrect." fi # Clean directories read -p "Clean the download directory and info.cfg file? [y/n] " YN if [ $YN == "y" ]; then rm $INFO rm -r $FOLDER fi fi # /CLIENT MODE