changeset 227:8b1f6f6b6a3b

Bugfixes: - Login and password are not always used now. Added new global variable to check it. - 'export PATH' is now only done if additional path(s) specified. Not really enhancements: - New coding style: - Local variables now start with double underscores and global variables start with single underscore. Both lower letter. - Function name is now snake_case. Camel_And_Snake_Case doesn't actually make sense :> - More comments. Still need to add even more comments.
author edhoprima@gmail.com <edhoprima@gmail.com>
date Mon, 14 Dec 2009 09:57:35 +0000
parents f8be4a3d3b4a
children 5d3a0645b504
files moefetch.sh
diffstat 1 files changed, 60 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/moefetch.sh	Sun Nov 22 06:42:41 2009 +0000
+++ b/moefetch.sh	Mon Dec 14 09:57:35 2009 +0000
@@ -39,6 +39,7 @@
 ###  - unified repository to save bandwidth
 ###  - bug stomping
 ###  - sanity checking
+###  - MOAR comments
 ### WILL BE FOR 0.3
 
 # useless welcome message. Also version
@@ -92,7 +93,9 @@
 	iternum=1
 	> "${TEMP_PREFIX}-list"
 	while [ "${tempnum}" -ge 1000 ]; do
-		wget "http://${SITE}/post/index.xml?tags=$(get_cleantags "${TAGS}")&offset=0&limit=1000&page=${iternum}&login=${LOGIN_USER}&password_hash=${LOGIN_PASS}" -O "${TEMP_PREFIX}-xml" -e continue=off
+		__url="http://${SITE}/post/index.xml?tags=$(get_cleantags "${TAGS}")&offset=0&limit=1000&page=${iternum}"
+		[ ${_use_login} -eq 1 ] && __url="${__url}&login=${LOGIN_USER}&password_hash=${LOGIN_PASS}"
+		wget "${__url}" -O "${TEMP_PREFIX}-xml" -e continue=off || Err_Fatal "Failed download catalog file"
 		printf "Processing XML file... "
 		# xslt evilry
 		xsltproc - "${TEMP_PREFIX}-xml" <<EOF | sed 's/.*\(http.*\)\(\/[a-f0-9]\{32\}\).*\.\([^\.]*\)/\1\2.\3/g' | grep ^http > "${TEMP_PREFIX}-templist"
@@ -280,15 +283,24 @@
 }
 
 # initialize base variables and initial command check
-Init(){
+init()
+{
 	# path initialization
-	[ -n "${ADDITIONAL_PATH}" ] && PATH="${ADDITIONAL_PATH}:${PATH}"
-	export PATH
+	# check if additional path is specified
+	if [ -n "${ADDITIONAL_PATH}" ]
+	then
+		# insert the additional path
+		PATH="${ADDITIONAL_PATH}:${PATH}"
+		export PATH
+	fi
 	
 	# misc variables
 	ISQUICK=
 	ISNEW=
+	# variable to check whether a login is used or not
+	_use_login=0
 	
+	# minimum number of arguments: 2 (command and tag). If less than two, exit and print help message
 	[ $# -lt 2 ] && Err_Help
 	case "$1" in
 		check|fetch|quickfetch)
@@ -302,25 +314,32 @@
 	shift
 	SITE=
 	TAGS=
-	x=1
+	__has_pass=0
+	__has_user=0
 	while getopts "s:(site)n(noclean)u:(user)p:(password)" opt
 	do
 		case "$opt" in
 			s) SITE="$OPTARG";;
 			n) NOCLEAN=1;;
-			p) LOGIN_PASS=$(printf "%s" "$OPTARG" | openssl dgst -sha1);;
-			u) LOGIN_USER="$OPTARG";;
+			p)
+				LOGIN_PASS=$(printf "%s" "$OPTARG" | openssl dgst -sha1)
+				__has_pass=1
+			;;
+			u)
+				LOGIN_USER="$OPTARG"
+				__has_user=1
+			;;
 		esac
-		x=${OPTIND}
 	done
-	shift $(($x-1))
 	TAGS="$@"
 	[ -n "${SITE}" ] || SITE=${DEFAULT_SITE}
 	[ -n "${TAGS}" ] || Err_Fatal "No tag specified"
 	# Get base folder - default, current folder or fallback to ${HOME}
 	[ -n "${BASE_DIR}" ] || BASE_DIR=${PWD}
 	[ -n "${BASE_DIR}" ] || BASE_DIR=${HOME}
-	[ -n "$(echo "${BASE_DIR}" | cut -c1 | grep \/)" ] || BASE_DIR="/${BASE_DIR}"	
+	[ -n "$(echo "${BASE_DIR}" | cut -c1 | grep \/)" ] || BASE_DIR="/${BASE_DIR}"
+	# see if both pass and use are set. If they're set, switch _use_login variable content to 1.
+	[ ${__has_pass} -eq 1 -a ${__has_user} -eq 1 ] && _use_login=1
 
 	echo "Tags: ${TAGS}"
 	# slash is not wanted for folder name
@@ -329,29 +348,36 @@
 	TEMP_PREFIX="${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}"
 }
 
-# initialization
-Msg_Welcome
-Init "$@"
-Check_Tools
-Check_Folders
+main()
+{
+	# initialization
+	Msg_Welcome
+	init "$@"
+	Check_Tools
+	Check_Folders
 
 
-# let's do the job!
-case "${JOB}" in
-	check)
-		Generate_Link
-		Check_Files
-	;;
-	fetch)
-		Generate_Link
-		Check_Files
-		Fetch_Images
-	;;
-	quickfetch)
-		ISNEW=1
-		ISQUICK=1
-		Generate_Link
-		Check_Files
-		Fetch_Images
-	;;
-esac
+	# let's do the job!
+	case "${JOB}" in
+		check)
+			Generate_Link
+			Check_Files
+		;;
+		fetch)
+			Generate_Link
+			Check_Files
+			Fetch_Images
+		;;
+		quickfetch)
+			ISNEW=1
+			ISQUICK=1
+			Generate_Link
+			Check_Files
+			Fetch_Images
+		;;
+	esac
+}
+
+# call the main routine!
+main "$@"
+