From f042a9561d4df4f71afdde3cb6fbca763349e624 Mon Sep 17 00:00:00 2001 From: raspbeguy Date: Wed, 13 May 2020 16:24:04 +0200 Subject: [PATCH] make it better --- test/rename_posts.sh | 58 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/test/rename_posts.sh b/test/rename_posts.sh index f3fd88a..0d0a897 100755 --- a/test/rename_posts.sh +++ b/test/rename_posts.sh @@ -1,7 +1,9 @@ #!/bin/bash -if [ -f config.sh ]; then - . config.sh +BASEDIR=`dirname $0` + +if [ -f $BASEDIR/config.sh ]; then + . $BASEDIR/config.sh fi if [ -z "$POSTS_DIR" ] @@ -10,10 +12,54 @@ then exit 2 fi -cd $POSTS_DIR -for post in * +FIX=0 + +while getopts "f" option do + case $option in + f) + FIX=1 + ;; + esac +done + +shift $(($OPTIND - 1)) + +function check() { + local rc=0 + post=$1 title=`sed -n 's/^title: "\?\(.*\)"\?$/\1/p' $post` safename=`echo $title | iconv -t ascii//TRANSLIT | tr '[:upper:]' '[:lower:]' | tr -s '[:punct:] ' '-' | sed 's/-*$//g;s/^-*//g'` - mv -v $post $safename.md 2> /dev/null -done + if [ $post == $safename.md ] + then + rc=0 + else + if [ $FIX -eq 1 ] + then + mv -v $post $safename.md + else + echo "$post should be named $safename.md" >&2 + fi + rc=1 + fi + return $rc +} + +cd $BASEDIR/$POSTS_DIR +if [ -z "$1" ] +then + rc=0 + for post in * + do + check $post + if [ $? -ne 0 ] + then + rc=1 + fi + done +else + check $1 + rc=$? +fi + +exit $rc