Преглед изворни кода

* flags, path check logics /A

Anton TETERIN пре 1 година
родитељ
комит
edf4e4ba11
1 измењених фајлова са 80 додато и 36 уклоњено
  1. 80 36
      scripts/media2import.sh

+ 80 - 36
scripts/media2import.sh

@@ -10,59 +10,103 @@
 #
 # History:
 # 2023-05-07  * init /A
+# 2023-05-08  + read flags from CLI /A
 #
 
 function ts() {
     return $(date)
 }
 
-# read arguments
-# src
-# dst
-#desc
+# read flags
+usage () {
+    echo "Usage: $0 -s - source, -d destination, -n note";
+    exit 1;
+}
+while getopts s:d:n: flag
+do
+    case "${flag}" in
+        s) src=${OPTARG};;
+        d) dst=${OPTARG};;
+        n) note=${OPTARG};;
+        ?) usage;;
+    esac
+done
 
 
 # read source, by default current path (if not given)
 # assuming not recursive, only in-directory files will be imported
-echo "Source [$(pwd)]:"
-read src
-if [ ${src} != "" ]l then
-    src = "$(pwd)"
+if [ -z ${src} ]; then
+    read -p "Source      [$(pwd)]: " src
+    if [ -z ${src} ]; then
+        src="$(pwd)"
+    fi
 fi
 
 # read destination, by default current path (if not given)
-echo "Destination [$(pwd)]:"
-read dst
-if [ ${dst} != "" ]l then
-    dst = "$(pwd)"
+if [ -z ${dst} ]; then
+    read -p "Destination [$(pwd)]: " dst
+    if [ -z ${dst} ]; then
+        dst="$(pwd)"
+    fi
+fi
+
+# read noteription, by default empty
+if [ -z ${note} ]; then
+    read -p "Note of session/event: " note
+    if [ -z ${note} ]; then
+        note=""
+    else
+        # adding hyphen before noteription, if it is not empty
+        note=" - ${note}"
+    fi
 fi
 
 # source and destination can not be the same, exit
-if [ ${src} -eq ${dst} ]; then
-    echo "[$ts] Source and destination are the same, exiting..."
+if [ ${src} = ${dst} ]; then
+    echo "Source and destination are the same, exiting..."
     exit 2
 fi
 
-# read description, by default empty
-echo "Description of session/event:"
-read desc
-if [ ${desc} != "" ]l then
-    # adding hyphen before description, if it is not empty
-    desc = " - ${desc}"
-fi
 
-# create filelist from source
-# TODO: output total amount of files and size
-# itirate files
-    # read creation date of file
-    dst_subdir="${dst}/${file_creation_date}${desc}"
-    # read creation date and time of file
-    file_new_name="$(read creation date and time, format)-${file}"
-    # output debug
-    echo "[ $(ts()) ] src: [${src}/${file], dst: [${dst_subdir}/${file_new_name}]"
-    # hash before move
-    # TODO: perhaps, replace with rsync (if there will be any sense)
-    # TODO: check if file exists
-    mv -v ${src}/${file} ${dst_subdir}/${file_new_name}
-    # hash after
-    # TODO: compare hashes, output result
+# confirm
+echo "Source:      [${src}]"
+echo "Destination: [${dst}]"
+echo "Note:        [${note}]"
+
+read -p "Confirm (Y): " confirm
+if [ ${confirm} = "Y" ]; then
+    echo "Reading source and beginning to move..."
+
+    # check that source directory exists, otherwise - exit
+    if ! [ -d {src}]; then
+        echo "Source directory does not exist, exiting..."
+        exit 2
+    fi
+
+    # check and create destination directory, if needed
+    if [ -d {dst}]; then
+        mkdir -p -m 700 ${dst}
+    fi
+
+    find ${dst} -
+
+    # TODO: output total amount of files and size
+    # itirate files
+        # read creation date of file
+        dst_subdir="${dst}/${file_creation_date}${note}"
+        # read creation date and time of file
+        file_new_name="$(read creation date and time, format)-${file}"
+        # output debug
+        # echo "[ $(ts) ] src: [${src}/${file], dst: [${dst_subdir}/${file_new_name}]"
+        # hash before move
+        # TODO: perhaps, replace with rsync (if there will be any sense)
+        # TODO: check if file exists
+        # cp -v ${src}/${file} ${dst_subdir}/${file_new_name}
+        # hash after
+        # TODO: compare hashes, output result
+
+else
+    # not confirmed
+    echo "Operation is not confirmed."
+    exit 1
+fi