]> git.phdru.name Git - dotfiles.git/commitdiff
Add scripts to copy/move a file with recoded filename
authorOleg Broytman <phd@phdru.name>
Fri, 9 Feb 2024 11:37:15 +0000 (14:37 +0300)
committerOleg Broytman <phd@phdru.name>
Fri, 9 Feb 2024 11:37:15 +0000 (14:37 +0300)
bin/cp_recode_fname [new file with mode: 0755]
bin/mv_recode_fname [new symlink]

diff --git a/bin/cp_recode_fname b/bin/cp_recode_fname
new file mode 100755 (executable)
index 0000000..8cae733
--- /dev/null
@@ -0,0 +1,40 @@
+#! /bin/sh
+set -e
+
+usage() {
+   echo "Usage: $0 [[from_enc] to_enc] filename" >&2
+   exit 1
+}
+
+if [ $# -eq 1 ]; then
+    from_enc="`python3 -c \"from m_lib.defenc import default_encoding; print(default_encoding)"`"
+    if [ "$from_enc" != utf-8 ]; then
+        to_enc=utf-8
+    else
+        usage
+    fi
+    filename="$1"
+elif [ $# -eq 2 ]; then
+    from_enc="`python3 -c \"from m_lib.defenc import default_encoding; print(default_encoding)"`"
+    to_enc="$1"
+    filename="$2"
+elif [ $# -eq 3 ]; then
+    from_enc="$1"
+    to_enc="$2"
+    filename="$3"
+else
+    usage
+fi
+
+cmd="`basename \"$0\"`"
+case "$cmd" in
+    cp_*) cmd="cp -p" ;;
+    mv_*) cmd=mv ;;
+    *)
+        echo "Uncnown command $0, aborting" >&2
+        exit 2
+    ;;
+esac
+
+filename_recoded=`echo "$filename" | iconv -f "$from_enc" -t "$to_enc"`
+exec $cmd "$filename" "$filename_recoded"
diff --git a/bin/mv_recode_fname b/bin/mv_recode_fname
new file mode 120000 (symlink)
index 0000000..16052c7
--- /dev/null
@@ -0,0 +1 @@
+cp_recode_fname
\ No newline at end of file