Skip to content

Commit

Permalink
better commandline option parsing, to work with aur package managers
Browse files Browse the repository at this point in the history
  • Loading branch information
simonzack committed Jan 16, 2015
1 parent c905b41 commit 8bc4cc4
Showing 1 changed file with 71 additions and 60 deletions.
131 changes: 71 additions & 60 deletions pacmatic
Original file line number Diff line number Diff line change
Expand Up @@ -180,50 +180,72 @@ To use Pacmatic's functionality in a script, source with
END
}

explode_args()
{
unset OPTS
local arg=$1
while [[ $arg ]]; do
[[ $arg = "--" ]] && OPTS+=("$@") && break;
if [[ ${arg:0:1} = "-" && ${arg:1:1} != "-" ]]; then
OPTS+=("-${arg:1:1}")
(( ${#arg} > 2 )) && arg="-${arg:2}" || { shift; arg=$1; }
else
OPTS+=("$arg"); shift; arg=$1
fi
done
}

if [ $# -eq 0 ]; then
docs
exit
fi

[ $1 == "--as-lib" ] && return
orig_args=$*
explode_args "$@"
set -- "${OPTS[@]}"
unset OPTS

while [[ $1 ]]; do
case "$1" in
--as-lib) return;;
--arch|--assume-installed|--cachedir|--color|--config|--gpgdir|--ignore|--ignoregroup|\
--logfile|--print-format|-b|--dbpath|-o|--owns|-r|--root|-s|--search) shift;;
-S|--sync) SYNC=1;;
-U|--upgrade) UPGRADE=1;;
-y|--refresh) REFRESH=1;;
-u|--sysupgrade) SYSUPGRADE=1;;
-*) ;;
*) NAME=1;;
esac
shift
done

current_time=$(date +%s)
sync_time=$(last_sync)
if [ -z $REFRESH ]; then
sync_time=$(last_sync)
else
sync_time=$(date +%s)
fi
upgrade_time=$(last_upgrade)

case "$1" in
-Syu|-Su|-Suy|-Syyu|-Syuy|-Suyy|-S|-Sy|-Syy)
# do not like this threading
check_news &
mail_summary
wait
echo "Recent ML chatter: $mail_counts"
if ( ! check_checkspace ); then
echo -e "\nPlease enable CheckSpace in pacman.conf"
fi
;;
esac

case "$1" in
-Syu|-Su|-Suy|-Syyu|-Syuy|-Suyy)
#check_news
;;
-S)
#check_news
if [ $(( $sync_time - $upgrade_time )) -gt $(( $warn_time )) ]; then
echo -e "\nWarning: stale installation, rerun with '-Syu'\n"
fi
;;
-Sy|-Syy)
#check_news
if [ $(( $current_time - $upgrade_time )) -gt $(( $warn_time )) ]; then
echo -e "\nWarning: stale installation, rerun with '-Syu'\n"
fi
;;
esac
if [ ! -z $SYNC ]; then
# do not like this threading
check_news &
mail_summary
wait
echo "Recent ML chatter: $mail_counts"
if ( ! check_checkspace ); then
echo -e "\nPlease enable CheckSpace in pacman.conf"
fi
fi

if [ ! -z $SYNC ] && [ -z $REFRESH ]; then
if [ $(( $sync_time - $upgrade_time )) -gt $(( $warn_time )) ]; then
echo -e "\nWarning: stale installation, rerun with '-Syu'\n"
fi
fi

old_pc=$(pacnew_count)
$pacman_program $*
$pacman_program $orig_args
exit_code=$?
exit_time=$(date "+[%F %H:%M]")
if [ -w "$pacman_log" ]; then
Expand All @@ -232,30 +254,19 @@ fi
new_pc=$(pacnew_count)
add_pc=$(( $new_pc - $old_pc ))

# this section is a little clumsy

if [ "$new_pc" = "0" ]; then
echo "No pacnew files to update."
exit 0
fi

case "$1" in
-Syu|-Su|-S|-Sy|-Syyu|-Syy)
echo -en "\n$new_pc pacnew files found ($add_pc added). Update files now? (Y/n) "
read char
if [ -z "$char" ]; then
$pacdiff_program
fi
case "$char" in
Y|y)
if [ ! -z $UPGRADE ] || ([ ! -z $SYNC ] && ([ ! -z $NAME ] || [ ! -z $SYSUPGRADE ])); then
if [ "$new_pc" = "0" ]; then
echo "No pacnew files to update."
else
echo -en "\n$new_pc pacnew files found ($add_pc added). Update files now? (Y/n) "
read char
if [ -z "$char" ]; then
$pacdiff_program
;;
N|n)
;;
*)
;;
esac
;;
*)
;;
esac
fi
case "$char" in
Y|y) $pacdiff_program;;
N|n) ;;
*) ;;
esac
fi
fi

0 comments on commit 8bc4cc4

Please sign in to comment.