diff --git a/t.sh b/t.sh index 97136a6..bfe8896 100755 --- a/t.sh +++ b/t.sh @@ -154,10 +154,10 @@ function saveids() { [[ $1 == "-v" ]] && verbose=1 if [[ ${#idmap[@]} -gt 0 ]]; then - cp /dev/null $idfile + cp /dev/null "$idfile" for id in ${!idmap[@]}; do #dblog "save to $idfile: uid ${idmap[$id]} id $id sum ${tasksum[$id]}" >> $idfile - echo "${idmap[$id]}:$id" >> $idfile + echo "${idmap[$id]}:$id" >> "$idfile" nids=$((nids + 1)) done [[ $nids -eq 1 ]] && ess="" || ess="s" @@ -188,9 +188,9 @@ function loadtasks() { [[ $1 == "-v" ]] && verbose=1 && shift 1 done dblog "Loading tasks..." - for f in $dir/*.vcf; do + for f in "$dir"/*.vcf; do dblog "--> loading from $f" - loadtask $f || return 1 + loadtask "$f" || return 1 ntasks=$((ntasks + 1)) done numwid=${#maxid} @@ -285,7 +285,7 @@ function loadtask() { dblog "open $1" file="$1" - res=$(cat $file | awk -F: 'BEGIN { checked=0; folded=0; } /^UID:/ { printf("uid©%s©",$2); } /^SUMMARY:/ { sub("SUMMARY:",""); printf("sum©%s©",$0); } /^CATEGORIES:/ { sub("^CATEGORIES:",""); printf("cats©%s©",$0); } /^DESCRIPTION:/ { sub("DESCRIPTION:",""); printf("desc©%s©",$0); } /^RELATED-TO/ { sub("^RELATED-TO.*:",""); printf("parent©%s©",$0); } /^X-OC-HIDESUBTASKS:1/ { folded=1; } /^STATUS:COMPLETED/ { checked=1; } END { printf("checked©%d©folded©%d\n",checked,folded); }') + res=$(cat "$file" | awk -F: 'BEGIN { checked=0; folded=0; } /^UID:/ { printf("uid©%s©",$2); } /^SUMMARY:/ { sub("SUMMARY:",""); printf("sum©%s©",$0); } /^CATEGORIES:/ { sub("^CATEGORIES:",""); printf("cats©%s©",$0); } /^DESCRIPTION:/ { sub("DESCRIPTION:",""); printf("desc©%s©",$0); } /^RELATED-TO/ { sub("^RELATED-TO.*:",""); printf("parent©%s©",$0); } /^X-OC-HIDESUBTASKS:1/ { folded=1; } /^STATUS:COMPLETED/ { checked=1; } END { printf("checked©%d©folded©%d\n",checked,folded); }') IFS='©' read -ra tok <<< "$res" @@ -1233,9 +1233,10 @@ EOF if [[ -z $selcal ]]; then warn "No calendar selected " echo "${YELLOW}Please put default calendar name in ${defaultcalfile}" - echo "${YELLOW}then: touch ${idfile}_\$(cat $defaultcalfile)" + echo "${YELLOW}then: touch \"${idfile}_\$(cat $defaultcalfile)\"" selcal="" else + selcal=${selcal// /_} if [[ -e "${idfile}_${selcal}" ]]; then warn "ID mapping file for $selcal already exists. Not overwriting it." else @@ -1298,6 +1299,27 @@ function getcals() { # countvar arrayvar done <<< "$loc_cals" } +function getcalname() { # cal_name_or_number + local lc larr d found="" + getcals lc larr + for d in ${!larr[@]}; do + if [[ ${larr[$d]} == $1 ]]; then + found="${larr[$d]}" + break + elif [[ $1 =~ ^[0-9]+$ && $d -eq $(($1 - 1)) ]]; then + # make this the default + found="${larr[$d]}" + break + fi + done + echo "$found" + if [[ -z $found ]]; then + return 1 + fi + return 0 +} + + # Should be in config file: #basedir=/Users/rpearce/scripts/t #dir=$basedir/docs @@ -1440,8 +1462,17 @@ while getopts "$ARGS" i; do done shift $((OPTIND - 1)) +# validate CALID +foundcal=$(getcalname "$CALID") +if [[ -n $foundcal ]]; then + CALID="$foundcal" +else + error "Calendar '$CALID' does not exist." + exit 1 +fi + dir=$allcalsdir/$CALID -idfile=$confdir/idmappings_$CALID +idfile="$confdir/idmappings_${CALID// /_}" if [[ ! -e $idfile ]]; then warn "No ID mapping file found for calendar '$CALID' ($idfile)" @@ -1520,28 +1551,12 @@ elif [[ $mode == "setcal" ]]; then error "No calendar name provided." exit 1 fi - getcals count cals_arr - if [[ $count -gt 0 ]]; then - found=0 - for d in ${!cals_arr[@]}; do - if [[ ${cals_arr[$d]} == $1 ]]; then - # make this the default - found=1 - break - elif [[ $1 =~ ^[0-9]+$ && $d -eq $(($1 - 1)) ]]; then - # make this the default - found=1 - break - fi - done - if [[ $found -eq 1 ]]; then - action "Default calendar set to '${cals_arr[$d]}'" - echo "${cals_arr[$d]}" > "$defaultcalfile" - else - error "Calendar '$1' not found" - fi + foundcal=$(getcalname $1) + if [[ -n $foundcal ]]; then + action "Default calendar set to '${foundcal}'" + echo "${foundcal}" > "$defaultcalfile" else - error "No calendars found." + error "Calendar '$1' not found" fi elif [[ $mode == "rename" ]]; then uid=$(getuid_witherror $1)