diff --git a/t.sh b/t.sh index 70061ce..f904569 100755 --- a/t.sh +++ b/t.sh @@ -123,19 +123,6 @@ function getuid() { return 0 } -function getorgenid() { - local res uid v=0 - uid=$1 - res=$(getid $uid) - if [[ -z $res ]]; then - res=$nextid - idmap[$nextid]=$uid - nextid=$((nextid + 1)) - needsave=1 - fi - echo "$res" -} - function loadids() { local LINE n uid id max count count=0 @@ -161,12 +148,26 @@ dblog "got $count IDs from $idfile" } function saveids() { - local id - 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 - done + local id verbose=0 + local ess nids=0 + + [[ $1 -eq "-v" ]] && verbose=1 + + if [[ ${#idmap[@]} -gt 0 ]]; then + 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 + nids=$((nids + 1)) + done + [[ $nids -eq 1 ]] && ess="" || ess="s" + [[ $verbose -eq 1 ]] && action "Saved $nids ID mapping$ess to $idfile." + else + [[ $verbose -eq 1 ]] && warn "No ID mappings found to save" + return 1 + fi + + return 0 } function dumpids() { @@ -178,10 +179,14 @@ function dumpids() { function loadtasks() { local f ess x thislev par parid show thissum thisdesc + local verbose=0 maxid=-1 maxindent=-1 maxtaskwidth=-1 ntasks=0 + while [[ $# -ge 1 ]]; do + [[ $1 == "-v" ]] && verbose=1 && shift 1 + done dblog "Loading tasks..." for f in $dir/*.vcf; do dblog "--> loading from $f" @@ -216,6 +221,7 @@ function loadtasks() { done [[ $ntasks -eq 1 ]] && ess="" || ess="s" + [[ $verbose -eq 1 ]] && action "Finished loading $ntasks task$ess." dblog "Finished loading $ntasks task$ess." return 0 } @@ -300,7 +306,13 @@ dblog "process token '${tok[$i]}'" #nexti=$((i + 1)) #dblog "i=$i tok[i]=${tok[$i]} i+1=$((i + 1)) tok[i+1]=${tok[$((i + 1))]} uid $uid id $id" #dblog "i=$i tok[i]=${tok[$i]} i+1=$((i + 1)) tok[nexti]=${tok[$nexti]} uid $uid id $id" - id=$(getorgenid $uid) + id=$(getid $uid) + if [[ -z $id ]]; then + id=$nextid + idmap[$nextid]=$uid + nextid=$((nextid + 1)) + needsave=1 + fi #dblog "i=$i tok[i]=${tok[$i]} uid $uid id $id" elif [[ ${tok[$i]} == "sum" ]]; then sum="${tok[$((i + 1))]}" @@ -1103,6 +1115,24 @@ function getchildren() { return 0 } +function makedir() { + local d + d="$1" + + if [[ -d "$d" ]]; then + info "Directory $d already exists" + else + mkdir -p "${d}" + if [[ $? -eq 0 ]]; then + action "Created directory ${BOLD}$d${PLAIN}" + else + error "Couldn't mkdir ${BOLD}$d${PLAIN}${RED}" + return 1 + fi + fi + return 0 +} + function init() { local gotvdconf=0 d url count cals cals_arr thiscal selcal action "Initialising configration" @@ -1120,17 +1150,7 @@ function init() { if [[ $gotvdconf -ne 1 ]]; then for d in "$vdirsyncdir" "$vdircaldir" "$vdirstatdir"; do - if [[ -d "$d" ]]; then - info "Directory $d already exists" - else - mkdir -p "$vdirsyncdir" - if [[ $? -eq 0 ]]; then - action "Created directory ${BOLD}$d${PLAIN}" - else - error "Couldn't mkdir ${BOLD}$d${PLAIN}${RED}" - return 1 - fi - fi + makedir "$d" || return 1 done echo -e "${MAGENTA}Please provide the URL of your caldav server.${PLAIN}" @@ -1183,7 +1203,14 @@ EOF count=$(echo "$cals" | wc -l | tr -d ' ') fi - if [[ ! -e "${confdir}/docs" ]] ; then + makedir "$confdir" || return 1 + if [[ -e "${allcalsdir}" ]] ; then + warn "Calendar symlink ${allcalsdir} already exists. Not overwriting it." + else + ln -s "${vdircaldir}" "${allcalsdir}" + action "Created symlink to $vdircaldir" + fi + if [[ ! -e "${defaultcalfile}" ]] ; then cals_arr=() selcal="" if [[ $count -gt 0 ]]; then @@ -1202,38 +1229,27 @@ EOF read sel done selcal=${cals_arr[$(($sel - 1))]} + echo "$selcal" >"$defaultcalfile" else warn "No calendars found." fi fi - if [[ ! -d "$confdir" ]] ; then - mkdir -p "$confdir" - action "Created $confdir" - fi - - if [[ ! -e "${confdir}/docs" ]] ; then - if [[ -n $selcal ]]; then - ln -s "${vdircaldir}/${selcal}" "${confdir}/docs" - action "Created symlink to $selcal" + if [[ -z $selcal ]]; then + warn "No calendar selected " + echo "${YELLOW}Please put default calendar name in ${defaultcalfile}" + echo "${YELLOW}then: touch ${idfile}_\$(cat $defaultcalfile)" + selcal="" + else + if [[ -e "${idfile}_${selcal}" ]]; then + warn "ID mapping file for $selcal already exists. Not overwriting it." else - warn "No calendar selected " - echo "${YELLOW}Please manually symlink ${confdir}/docs to the required local directory under $vdircaldir" - selcal="" + touch "${idfile}_${selcal}" fi - else - warn "Calendar symlink ${confdir}/docs already exists. Not overwriting it." - selcal=$(basename $(readlink -n ${confdir}/docs)) - fi - - if [[ -e "$idfile" ]]; then - warn "ID mapping file $idfile already exists. Not overwriting it." - else - touch "$idfile" - info "Created ID mapping file in ${idfile}" fi info "Initialisation complete" + [[ -z $selcal ]] && selcal="" echo -e "${CYAN} vdirsyncer config is in ${BOLD}${vdirsyncdir}${PLAIN}" echo -e "${CYAN} task_cli config is in ${BOLD}${confdir}${PLAIN}" echo -e "${CYAN} selected calendar is '${BOLD}${selcal}${PLAIN}'" @@ -1328,8 +1344,9 @@ DEFAULT_VDS_LOCAL=cal_local DEFAULT_VDS_REMOTE=cal_remote basedir=/Users/rpearce/code/task_cli confdir=${HOME}/.task_cli -dir=$confdir/docs # symlink to vdirsyncer/calendar/xxx/ -idfile=$confdir/idmappings.txt +allcalsdir=$confdir/allcals # symlink to vdirsyncer/calendar/ +defaultcalfile=$confdir/defaultcal +idfile=$confdir/idmappings nextid=1 indent=0 indentamt=4 @@ -1368,8 +1385,9 @@ addcmd "view" "view " "Show detailed info for given task" "v|view|info|vcal" DEBUG=0 TESTMODE=0 AUTOSYNC=0 +CALID=$(cat "$confdir/defaultcal") -ARGS="fhidstyl:r:" +ARGS="fhic:dstyl:r:" while getopts "$ARGS" i; do case "$i" in h) @@ -1380,6 +1398,9 @@ while getopts "$ARGS" i; do init exit $? ;; + c) + CALID="$OPTARG" + ;; l) VDS_LOCAL="$OPTARG" ;; @@ -1408,10 +1429,26 @@ while getopts "$ARGS" i; do done shift $((OPTIND - 1)) +dir=$allcalsdir/$CALID +idfile=$confdir/idmappings_$CALID + +if [[ ! -e $idfile ]]; then +oooo + warn "No ID mapping file found for calendar '$CALID' ($idfile)" + confirm "Create one from existing tasks?" + if [[ $? -eq 0 ]]; then + loadtasks -v + saveids -v || exit 1 + else + die + fi +fi [[ -z $VDS_LOCAL ]] && VDS_LOCAL="$DEFAULT_VDS_LOCAL" [[ -z $VDS_REMOTE ]] && VDS_REMOTE="$DEFAULT_VDS_REMOTE" + + mode="" for x in $valid_modes; do thisone="words_$x"