add arguments, help, bugfixing
This commit is contained in:
126
split-dl.sh
126
split-dl.sh
@@ -1,16 +1,59 @@
|
||||
#!/bin/bash
|
||||
|
||||
FILE="$1"
|
||||
TYPE="$2"
|
||||
SPLITSIZE="10M"
|
||||
INFO="info.cfg"
|
||||
SPLITSIZE="10M" # Desired size of splitted files
|
||||
INFO="info.cfg" # Desired name of document containing information
|
||||
CHECKSUM="md5sum" # Define application to create hashsums
|
||||
FILE=""
|
||||
MODE=""
|
||||
|
||||
# Check if 2 params are given
|
||||
if [ "$#" -lt "2" ]; then
|
||||
echo "This script requires two arguments: File or Link AND server or client."
|
||||
while getopts s:i:c:f:t:help: opt; do
|
||||
case $opt in
|
||||
h)
|
||||
echo "This program is used to split large files into several small
|
||||
files to avoid data loss and the need to re-download them from
|
||||
slow or unstable internet connections."
|
||||
echo
|
||||
echo "Necessary arguments to start this application:"
|
||||
echo "-t Desired MODE. Possible values are \"server\" or \"client\""
|
||||
echo " \"server\" splits the file on the server and needs a file for -f."
|
||||
echo " \"client\" downloads the files and needs a download link for -f."
|
||||
echo "-f File in the same directory to be splitted or link to be downloaded."
|
||||
echo
|
||||
echo "Optional arguments:"
|
||||
echo "-s Define the desired size of splitted files. Default is $SPLITSIZE"
|
||||
echo "-i Define the desired name of the info document. Default is $INFO"
|
||||
echo "-c Define the program which should be used to calculate the hashsum. Default is $CHECKSUM"
|
||||
exit 0
|
||||
;;
|
||||
s)
|
||||
SPLITSIZE=$OPTARG
|
||||
;;
|
||||
i)
|
||||
INFO=$OPTARG
|
||||
;;
|
||||
c)
|
||||
CHECKSUM=$OPTARG
|
||||
;;
|
||||
f)
|
||||
FILE=$OPTARG
|
||||
;;
|
||||
m)
|
||||
MODE=$OPTARG
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# DO NOT EDIT BELOW HERE
|
||||
# ----------------------
|
||||
|
||||
if [ "$FILE" == "" ] || [ "$MODE" == "" ]; then
|
||||
echo "Missing arguments! Please define the mode with -m and the file/link with -f. More info with -h."
|
||||
exit 1
|
||||
else
|
||||
echo "OK"
|
||||
fi
|
||||
|
||||
|
||||
# # # #
|
||||
# FUNCTIONS
|
||||
# # # #
|
||||
@@ -18,12 +61,12 @@ function checkwait {
|
||||
read -p "Continue? Press Ctrl+C to cancel." END
|
||||
}
|
||||
|
||||
function getmd5 {
|
||||
function gethash {
|
||||
md5sum $1 | awk -F" " '{ print $1 }'
|
||||
}
|
||||
|
||||
function compmd5 {
|
||||
HASH1=$(getmd5 $1)
|
||||
function comphash {
|
||||
HASH1=$(gethash $1)
|
||||
HASH2=$2
|
||||
if [ "$HASH1" == "$HASH2" ]; then
|
||||
echo "true"
|
||||
@@ -49,8 +92,8 @@ function compdu {
|
||||
function compall {
|
||||
FILE=$1
|
||||
SIZE=$2
|
||||
MD5=$3
|
||||
if $(compdu $FILE $SIZE) && $(compmd5 $FILE $MD5); then
|
||||
HASH=$3
|
||||
if $(compdu $FILE $SIZE) && $(comphash $FILE $HASH); then
|
||||
echo="true"
|
||||
else
|
||||
echo="false"
|
||||
@@ -66,7 +109,7 @@ function checkfolder {
|
||||
rm -rf $FOLDER
|
||||
mkdir $FOLDER
|
||||
else
|
||||
if [ "$TYPE" == "server" ]; then
|
||||
if [ "$MODE" == "server" ]; then
|
||||
echo "In server-mode, this isn't recommended. Aborting."
|
||||
exit 1
|
||||
fi
|
||||
@@ -83,7 +126,7 @@ function checkfolder {
|
||||
# # # #
|
||||
# SERVER MODE
|
||||
# # # #
|
||||
if [ $TYPE == "server" ]; then
|
||||
if [ $MODE == "server" ]; then
|
||||
|
||||
# Check if file has spaces
|
||||
if [ $(echo "$FILE" | grep -q " "; echo $?) == "0" ]; then
|
||||
@@ -124,20 +167,20 @@ checkfolder $FOLDER
|
||||
INFONAME=$INFO
|
||||
INFO="$FOLDER/$INFONAME"
|
||||
|
||||
echo "[INFO] Calculating size and MD5 sum..."
|
||||
echo "[INFO] Calculating size and HASH sum..."
|
||||
BIGNAME=$(basename $FILE)
|
||||
BIGSIZE=$(getdu $FILE)
|
||||
BIGMD5=$(getmd5 $FILE)
|
||||
BIGHASH=$(gethash $FILE)
|
||||
|
||||
echo "BIGNAME=$BIGNAME" >> $INFO
|
||||
echo "BIGSIZE=$BIGSIZE" >> $INFO
|
||||
echo "BIGMD5=$BIGMD5" >> $INFO
|
||||
echo "BIGHASH=$BIGHASH" >> $INFO
|
||||
echo >> $INFO
|
||||
|
||||
echo "[INFO] Splitting big file into smaller parts..."
|
||||
split --verbose -a 4 -b $SPLITSIZE $FILE $FOLDER/dl-
|
||||
|
||||
# List all splitted files, measure size and md5sum
|
||||
# List all splitted files, measure size and hashsum
|
||||
echo "[INFO] Creating info document with necessary specs..."
|
||||
SMALLNO=$(ls $FOLDER | grep -v $INFONAME | wc -l)
|
||||
echo "SMALLNO=$SMALLNO" >> $INFO
|
||||
@@ -146,11 +189,11 @@ ls $FOLDER | grep -v $INFONAME | while read -r line; do
|
||||
|
||||
SMALLNAME=$line
|
||||
SMALLSIZE=$(getdu $FOLDER/$SMALLNAME)
|
||||
SMALLMD5=$(getmd5 $FOLDER/$SMALLNAME)
|
||||
SMALLHASH=$(gethash $FOLDER/$SMALLNAME)
|
||||
|
||||
echo "SMALLNAME[$NO]=$SMALLNAME" >> $INFO
|
||||
echo "SMALLSIZE[$NO]=$SMALLSIZE" >> $INFO
|
||||
echo "SMALLMD5[$NO]=$SMALLMD5" >> $INFO
|
||||
echo "SMALLHASH[$NO]=$SMALLHASH" >> $INFO
|
||||
|
||||
let NO=NO+1
|
||||
done
|
||||
@@ -164,7 +207,7 @@ fi # /SERVER MODE
|
||||
# # # #
|
||||
# CLIENT MODE
|
||||
# # # #
|
||||
if [ $TYPE == "client" ]; then
|
||||
if [ $MODE == "client" ]; then
|
||||
|
||||
URL=$FILE
|
||||
|
||||
@@ -177,6 +220,9 @@ fi
|
||||
if [ -e $INFO ]; then rm $INFO; fi
|
||||
wget -q $URL/$INFO
|
||||
|
||||
# Rename info.cfg to avoid colissions and source it
|
||||
mv $INFO $BIGNAME-$INFO
|
||||
INFO=$BIGNAME-$INFO
|
||||
source $INFO
|
||||
|
||||
FOLDER="dl-$BIGNAME"
|
||||
@@ -184,7 +230,7 @@ FOLDER="dl-$BIGNAME"
|
||||
# Print basic status
|
||||
echo
|
||||
echo "Total filesize: $BIGSIZE"
|
||||
echo "MD5: $BIGMD5"
|
||||
echo "HASH: $BIGHASH"
|
||||
echo "Number of splitted files: $SMALLNO"
|
||||
echo "Download folder: "$(readlink -f $FOLDER)""
|
||||
echo
|
||||
@@ -197,7 +243,7 @@ until [ "$STATUS" == "F" ]; do
|
||||
for ((i = 0; i < ${#SMALLNAME[*]}; i++)); do
|
||||
SMALLNAME=${SMALLNAME[$i]}
|
||||
SMALLSIZE=${SMALLSIZE[$i]}
|
||||
SMALLMD5=${SMALLMD5[$i]}
|
||||
SMALLHASH=${SMALLHASH[$i]}
|
||||
|
||||
SMALLURL=$URL/$SMALLNAME
|
||||
SMALLPATH=$FOLDER/$SMALLNAME
|
||||
@@ -213,8 +259,8 @@ until [ "$STATUS" == "F" ]; do
|
||||
# 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
|
||||
wget -O $SMALLPATH -nv --show-progress $SMALLURL
|
||||
if $(compall $SMALLPATH $SMALLSIZE $SMALLHASH); then
|
||||
STATUS="C"
|
||||
else
|
||||
# Downloaded file is not valid. Restart for-loop
|
||||
@@ -226,8 +272,8 @@ until [ "$STATUS" == "F" ]; do
|
||||
# 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
|
||||
wget -c -O $SMALLPATH -nv --show-progress $SMALLURL
|
||||
if $(compall $SMALLPATH $SMALLSIZE $SMALLHASH); then
|
||||
STATUS="C"
|
||||
else
|
||||
# Downloaded file is not valid. Restart for-loop
|
||||
@@ -236,14 +282,14 @@ until [ "$STATUS" == "F" ]; do
|
||||
break
|
||||
fi
|
||||
|
||||
# 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."
|
||||
# File already exists, has correct size, but has wrong HASH
|
||||
elif [ -e $SMALLPATH ] && $(compdu $SMALLPATH SMALLSIZE) && ! $(comphash $SMALLPATH $SMALLHASH); then
|
||||
echo "[ERROR] HASH 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
|
||||
wget -O $SMALLPATH -nv --show-progress $SMALLURL
|
||||
if $(compall $SMALLPATH $SMALLSIZE $SMALLHASH); then
|
||||
STATUS="C"
|
||||
else
|
||||
# Downloaded file is not valid. Restart for-loop
|
||||
@@ -252,8 +298,8 @@ until [ "$STATUS" == "F" ]; do
|
||||
break
|
||||
fi
|
||||
|
||||
# File already exists, has correct size, and has correct MD5
|
||||
elif [ -e $SMALLPATH ] && $(compdu $SMALLPATH $SMALLSIZE) && $(compmd5 $SMALLPATH $SMALLMD5); then
|
||||
# File already exists, has correct size, and has correct HASH
|
||||
elif [ -e $SMALLPATH ] && $(compdu $SMALLPATH $SMALLSIZE) && $(comphash $SMALLPATH $SMALLHASH); then
|
||||
echo "[SUCCESS] File already exists and is valid."
|
||||
STATUS="C"
|
||||
|
||||
@@ -295,9 +341,9 @@ if [ $YN == "y" ]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
# Check big file for MD5 and size
|
||||
# Check big file for HASH and size
|
||||
sleep 2 # In rare cases, this can prevent a wrong du size
|
||||
echo "[INFO] Checking correct size and MD5 hashsum for rebuilt big file..."
|
||||
echo "[INFO] Checking correct size and HASH hashsum for rebuilt big file..."
|
||||
|
||||
# Compare sizes
|
||||
if $(compdu $BIGNAME $BIGSIZE); then
|
||||
@@ -306,11 +352,11 @@ else
|
||||
echo "[ERROR] The size of the completed file is incorrect."
|
||||
fi
|
||||
|
||||
# Compare MD5 sum
|
||||
if $(compmd5 $BIGNAME $BIGMD5); then
|
||||
echo "[SUCCESS] The MD5 sum of the completed file is corrent."
|
||||
# Compare HASH sum
|
||||
if $(comphash $BIGNAME $BIGHASH); then
|
||||
echo "[SUCCESS] The HASH sum of the completed file is corrent."
|
||||
else
|
||||
echo "[ERROR] The MD5 sum of the completed file is incorrect."
|
||||
echo "[ERROR] The HASH sum of the completed file is incorrect."
|
||||
fi
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user