· 6 years ago · Jun 29, 2019, 11:08 PM
1#!/bin/bash -x
2
3set -e # Exit on any error
4
5db_file="wait.db"
6dir_prefix="folder" # if the folder prefix is "folder_", the folders will be generated like "folder_1", "folder_2", etc.
7fcount_limit="40" # The amount of files that should be in each folder
8########################################
9# I have no idea how to implement this yet ==> max_dirs="200" # The maximum amount of folders to consider when running this
10fcount=0
11target_number=1 # The number for each folder with dir_prefix, gets checked by max_dirs
12prefix_length=$(expr length "$dir_prefix")
13cut_start=$(expr $prefix_length + 1)
14
15function error() {
16 echo "[ERROR]: Something has gone horribly wrong..."
17 echo "[ERROR]: The error is: $1"
18 echo "[ERROR]: The script will exit now."
19 exit 1
20}
21
22function OK() {
23 echo "[OK]: $1"
24}
25
26function warn() {
27 echo "[Warning]: $1"
28}
29
30function log() {
31 echo "[Log]: $1"
32}
33
34function check_permissions() {
35 if [ ! -w . ]; then
36 error "You don't have write permissions for the current directory! Exiting..."
37 fi
38}
39
40function create_db() {
41 touch "$db_file"
42 OK "Creating tables..."
43 sqlite3 "$db_file" "CREATE TABLE Transfers (Source TEXT, Destination TEXT);"
44 sqlite3 "$db_file" "CREATE TABLE Count (Count INTEGER, Folder TEXT);"
45}
46
47# function wipe_counts() {
48# sqlite3 "$db_file" "DROP TABLE Count;"
49# sqlite3 "$db_file" "CREATE TABLE Count (Count INTEGER, Folder TEXT);"
50# }
51
52function insert_count() {
53 true
54}
55
56function insert_transfer() {
57 sqlite3 "$db_file" "INSERT INTO Transfers VALUES ('$1', '$2');"
58}
59
60function get_db_count() {
61 last_accessed_count=$(sqlite3 "$db_file" "SELECT Count FROM Count ORDER BY Count ASC LIMIT 1;")
62 last_accessed_folder=$(sqlite3 "$db_file" "SELECT Folder FROM Count ORDER BY Count ASC LIMIT 1;")
63
64 # There may or may not be data in both of these variables, which is really annoying.
65 if [ -z "$last_accessed_count" ]; then
66 empty_count=True
67 elif [ -n "$last_accessed_count" ]; then
68 empty_count=False
69 fi
70}
71
72function start_up() {
73 # Check if a database file exists
74 # If not, we will create it
75 if [ ! -f $db_file ]
76 then
77 check_permissions
78 warn "Database file does not exist!"
79 log "Database file will be written to $db_file..."
80 create_db || error "Error creating database!"
81 OK "Database file has successfully been written to $db_file"
82 fi
83 log "A database file already exists at $db_file."
84 log "The script will use it to log its transfers."
85
86 current_dir=$(pwd)
87 get_db_count
88
89 if [ $empty_count == "False" ]; then
90 fcount=$last_accessed_count
91 target_number=$(echo "$last_accessed_folder" | cut -c"$cut_start"- -)
92 log "This program has previously been used. The first target dir is $dir_prefix$target_number with a count of $fcount."
93 fi
94 target_dir="$dir_prefix$target_number"
95
96}
97
98function main() {
99 start_up || error "Error while attempting to initialize script"
100
101 inotifywait -r -m -q -e close_write --format %w%f . | while IFS= read -r file; do #check close_write event on a current dir where the script is running, then print the path+filename to stdout
102 if [ "$fcount" -ge "$fcount_limit" ]; then #check if file count is at the limit
103 fcount=0 #reset the file count for the next folder
104 ((target_number++))
105 target_dir="$dir_prefix$target_number" #set the new target dir
106 fi
107 mkdir -p -- "$target_dir/${file%/*}" && mv "$file" "$target_dir/${file%/*}" #make a non-existing dir structure following the source #pattern, to match the target dir (without the filename) and move the file there
108 insert_transfers "$current_dir/$file" "$current_dir/$target_dir/$file"
109 insert_count "$fcount" "$target_dir"
110 ((fcount++)) #count the transfer which goes toward the file limits
111 done
112}