diff --git a/bin/file-age b/bin/file-age new file mode 120000 index 0000000..739f6e6 --- /dev/null +++ b/bin/file-age @@ -0,0 +1 @@ +../files/file-age \ No newline at end of file diff --git a/files/file-age b/files/file-age new file mode 100755 index 0000000..a16dac4 --- /dev/null +++ b/files/file-age @@ -0,0 +1,48 @@ +#!/bin/bash + +_help() { + echo ' +Usage: file-age {file} [units] + + Checks file modification date, and the current time. Prints the age of + the file in [units]. + + [units] is one of S,M,H,d,w,m,y + seconds is the default. All values floored. + month = 30 days + year = 365 days +' +exit +} +for (( i=1; i<=$#; i++ )) +do [[ "${!i}" = "-h" ]] && _help +done + +STATFILE="$1" +[[ -z "$STATFILE" ]] && _help +test -e "$STATFILE" || { echo File "$STATFILE" does not exist >&2; exit 1; } + +UNITS=${2:-S} +created=$( stat -c %Y "$STATFILE" ) +now=$( date +%s ) +age=$(( $now - $created )) + +case $UNITS in + S) + echo $age;; + M) + echo $(( $age / 60 ));; + H) + echo $(( $age / 3600 ));; + d) + echo $(( $age / 86400 ));; + w) + echo $(( $age / 604800 ));; + m) + echo $(( $age / 2592000 ));; + y) + echo $(( $age / 31536000 ));; + *) + echo Unit $UNITS not recognized. + _help;; +esac diff --git a/rc b/rc index 04c5ad2..49798fa 100755 --- a/rc +++ b/rc @@ -42,23 +42,6 @@ function _check_ncsv() { } } -function _change_hg_repo() { - [[ -w "$TOOLSPATH"/.hg ]] || return - [[ -w "$TOOLSPATH"/.hg/hgrc ]] || return - grep -q moonq.kapsi.fi/repo/ "$TOOLSPATH"/.hg/hgrc && { - echo -e "Converting read only repository to bitbucket\n" - sed 's,\s*default\s*=.*moonq.kapsi.fi.*,default = https://bitbucket.org/MoonQ/tools,' "$TOOLSPATH"/.hg/hgrc \ - | diff "$TOOLSPATH"/.hg/hgrc - - sed -i 's,\s*default\s*=.*moonq.kapsi.fi.*,default = https://bitbucket.org/MoonQ/tools,' "$TOOLSPATH"/.hg/hgrc - } - grep -q moonq.kapsi.fi/repoz/ "$TOOLSPATH"/.hg/hgrc && { - echo -e "Converting writable repository to bitbucket\n" - sed 's,\s*default\s*=.*moonq.kapsi.fi.*,default = ssh://hg@bitbucket.org/MoonQ/tools,' "$TOOLSPATH"/.hg/hgrc \ - | diff "$TOOLSPATH"/.hg/hgrc - - echo - sed -i 's,\s*default\s*=.*moonq.kapsi.fi.*,default = ssh://hg@bitbucket.org/MoonQ/tools,' "$TOOLSPATH"/.hg/hgrc - } -} # install ncsv if necessary which ncsv > /dev/null 2>&1 || _check_ncsv > /dev/null 2>&1 unset -f _check_ncsv @@ -79,13 +62,11 @@ TOOLS_TO_UPDATE=1 [[ -w "$TOOLSPATH"/.lastupdate ]] || TOOLS_TO_UPDATE=0 if [ $TOOLS_TO_UPDATE -eq 1 ]; then - _change_hg_repo - TOOLS_LASTUPDATE=$(( $( date +%s ) - $( stat -c %Y "$TOOLSPATH"/.lastupdate ) )) - [[ "$TOOLS_LASTUPDATE" -gt 604800 ]] && _self_update + TOOLS_LASTUPDATE=$( "$TOOLSPATH"/bin/file-age "$TOOLSPATH"/.lastupdate d ) + [[ "$TOOLS_LASTUPDATE" -gt 7 ]] && _self_update unset TOOLS_LASTUPDATE fi unset TOOLS_TO_UPDATE unset -f _self_update -unset -f _change_hg_repo