<commit_before>config_file="config"
profiles_directory="profiles"

detect_x() {
    case $1 in
        "f"*) # file
            if [[ ! -f $2 ]]; then
               fail $2 "File does not exist."
            fi
        ;;
        "d"*) # directory
            if [[ ! -d $2 ]]; then
               fail $2 "Directory does not exist."
            fi
        ;;
        "x"*) # either file or directory
            if [[ (! -f $2) && (! -d $2) ]]; then
                fail $2 "File or directory does not exist."
            fi
        ;;
        "m"*) # mount
            if [[ ! $(mount | grep $2) ]]; then
                fail $2 "Hard drive not mounted."
            fi
        ;;
    esac
}

fail() {
    echo $1
    echo $2
    exit
}

profile() {
    detect_x "f" $profiles_directory/$1

    first=1 # first single character option (add -)
    cat $profiles_directory/$1 | while read -r line || [[ -n "$line" ]]; do
        option=$(echo $line | cut -d \: -f 1)
        
        if [[ ${#option} == 1 ]]; then # single letter options
            if [[ $first == 1 ]]; then
                first=0
                echo -n " -$option"
            else
                echo -n "$option"
            fi
        else # longer options (add --)
            first=1
            echo -n " --$option"
        fi
    done
}

main() {
    detect_x "f" $config_file
    detect_x "d" $profiles_directory

    cat $config_file | while read line || [[ -n "$line" ]]; do
        token=0
        cmd="rsync"

        for p in $(echo $line | tr " " " "); do
            token=$(($token+1))

            if [[ $token == 1 ]]; then # profile
                profile=$(echo $p | awk '{print $1;}') # get first word of $line
                cmd+=$(profile $profile)
            elif [[ $token -ge 2 && $token -le 3 ]]; then # source and destination
                if [[ $token == 2 ]]; then # source
                    if [[ "$(echo $p | head -c 1)" = "~" ]]; then
                        src=$(echo $p | cut -d '~' -f 2)
                        src="$HOME$src"
                    else
                        src=$p
                        detect_x "x" $src
                    fi
                elif [[ $token == 3 ]]; then # destination volume
                    detect_x "m" $p
                    dest=$p
                fi
                cmd+=" $p"
            elif [[ $token == 4 ]]; then # destination folder
                if [[ "$p" != "." ]]; then
                    detect_x "d" "$dest/$p"
                    cmd+="/$p"
                fi
            elif [[ $token -ge 5 ]]; then # excludes
                detect_x "x" "$src/$p"
                cmd+=" --exclude='$p'"
            fi
        done
        token=0
        echo $cmd
        eval $cmd
    done
}

main<commit_msg>redundant if statement<commit_after>config_file="config"
profiles_directory="profiles"

detect_x() {
    case $1 in
        "f"*) # file
            if [[ ! -f $2 ]]; then
               fail $2 "File does not exist."
            fi
        ;;
        "d"*) # directory
            if [[ ! -d $2 ]]; then
               fail $2 "Directory does not exist."
            fi
        ;;
        "x"*) # either file or directory
            if [[ (! -f $2) && (! -d $2) ]]; then
                fail $2 "File or directory does not exist."
            fi
        ;;
        "m"*) # mount
            if [[ ! $(mount | grep $2) ]]; then
                fail $2 "Hard drive not mounted."
            fi
        ;;
    esac
}

fail() {
    echo $1
    echo $2
    exit
}

profile() {
    detect_x "f" $profiles_directory/$1

    first=1 # first single character option (add -)
    cat $profiles_directory/$1 | while read -r line || [[ -n "$line" ]]; do
        option=$(echo $line | cut -d \: -f 1)
        
        if [[ ${#option} == 1 ]]; then # single letter options
            if [[ $first == 1 ]]; then
                first=0
                echo -n " -$option"
            else
                echo -n "$option"
            fi
        else # longer options (add --)
            first=1
            echo -n " --$option"
        fi
    done
}

main() {
    detect_x "f" $config_file
    detect_x "d" $profiles_directory

    cat $config_file | while read line || [[ -n "$line" ]]; do
        token=0
        cmd="rsync"

        for p in $(echo $line | tr " " " "); do
            token=$(($token+1))

            if [[ $token == 1 ]]; then # profile
                profile=$(echo $p | awk '{print $1;}') # get first word of $line
                cmd+=$(profile $profile)
            elif [[ $token == 2 ]]; then # source
                if [[ "$(echo $p | head -c 1)" = "~" ]]; then # ~/source/folder
                    src=$(echo $p | cut -d '~' -f 2)
                    src="$HOME$src" # /Users/user/source/folder
                    p=$src
                else
                    src=$p
                fi
                detect_x "x" $p
                cmd+=" $p"
            elif [[ $token == 3 ]]; then # destination volume
                detect_x "m" $p
                dest=$p
                cmd+=" $p"
            elif [[ $token == 4 ]]; then # destination folder
                if [[ "$p" != "." ]]; then
                    detect_x "d" "$dest/$p"
                    cmd+="/$p"
                fi
            elif [[ $token -ge 5 ]]; then # excludes
                detect_x "x" "$src/$p"
                cmd+=" --exclude='$p'"
            fi
        done
        token=0
        echo $cmd
        eval $cmd
    done
}

main
{'fraction_non_alphanumeric': 0.14738366829621583, 'fraction_numerical': 0.011587905124026797, 'mean_word_length': 1.0940106141015922, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3572297', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1577, 'n_words': 500}