diff options
author | sinanmohd <sinan@firemail.cc> | 2023-07-08 14:39:37 +0400 |
---|---|---|
committer | sinanmohd <sinan@firemail.cc> | 2023-07-08 14:41:11 +0400 |
commit | c82316419babd2af9e7ef5bf11a5bbd1e8d672d1 (patch) | |
tree | b3c11c75be6ab170713b6f9df200a03bcaa50c77 /bin | |
parent | e7ed62a968f5b29d7ee74b57406df758e2bb566f (diff) |
ctov: convert csv to vcf, initial commit
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/ctov.sh | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/bin/ctov.sh b/bin/ctov.sh new file mode 100755 index 0000000..018b70c --- /dev/null +++ b/bin/ctov.sh @@ -0,0 +1,82 @@ +#!/bin/sh + +vfmt="BEGIN:VCARD\r\n\ +VERSION:3.0\r\n\ +N:%s;;;;\r\n\ +FN:%s\r\n\ +TEL;TYPE=CELL:%s\r\n\ +END:VCARD\r\n" + +die() +{ + : "${1:?}" + + printf "\033[31;1merr: %b\033[0m\n" "$1" 1>&2 + exit "${2:-1}" +} + +usage() +{ + cat <<- EOF + Usage: ${0##*/} [options] [ contacts.csv ] + convert csv file to vcf file + Options: + -h show this help cruft + -f path to csv file + + EOF +} + +trimstr() +{ + # usage trimstr <string> + : "${1:?}" + + _str="${1%"${1##*[![:space:]]}"}" + _str="${_str#"${_str%%[![:space:]]*}"}" + + printf "%s" "$_str" + unset _str +} + +makev() +{ + # usage makev <name> <no> + : "${1:?}" + : "${2:?}" + # shellcheck disable=SC2059 + printf "$vfmt" "$1" "$1" "$2" +} + +######## +# MAIN # +######## + +file= + +while getopts "hf:" f; do + case "$f" in + f) + file="$OPTARG" + ;; + h) + usage + exit + ;; + *) + usage + exit 1 + esac +done + +if [ -n "$file" ] && + [ "$(file --brief --dereference --mime-type "$file")" != "application/csv" ]; then + die "input is not a csv file" +fi + +while read -r entry; do + name=$(trimstr "${entry%%,*}") + no=$(trimstr "${entry#*,}") + + makev "$name" "$no" +done <"${file:-/dev/stdin}" |