#!/bin/bash
# This script renames and/or numbers *.JPG files so they are lowercase
#
# This script was written by flipjargendy.
# Version: 0.6
# Location: http://flipjarg.blinkenshell.org
# Address: flipjarg [adt] blinkenshell [dodt] org
#
# To use this file make it executable and rename it to `rename.sh`

clear # clear screen

# WARNING
clear
echo Welcome to Weenie Mode. Advanced Mode is being developed at the moment.
echo Website: http://flipjarg.blinkenshell.org/
echo
# Rename and Number or just Rename?
echo "What would you like to do?"
echo
echo "1 - Rename only JPGs with uppercase extensions and number(ex. uniqueName.1.jpg)"
echo "2 - Only shrink the file extension"
echo "3 - Rename all JPG files and number (ex commonName.1.jpg)"
echo "Enter anything else to exit"
echo
read -p "Enter option: " 

# Moves *.JPG to "$NAME.$NUM.jpg"
if [ "$REPLY" = "1" ]; then
	clear
	echo -n "Enter the name that each file will be renamed to (ex. roadTrip): "; read NAME;
	echo 
	
	# Determines whether input was numeric
	NUM=1
	while [ "$ANS" != "yes" ]
	do 
		echo -n "Enter the number you'd like to start at (1 Enter): "; read NUM;
		if [ -z $NUM ]; then
			NUM=1
		fi
		
		echo $NUM | grep "[^0-9]" > /dev/null 2>&1
		if [ "$?" -eq "0" ]; then
			# If the grep found something other than 0-9 then it's not an integer.
			ANS=no
			echo "Sorry, wanted a number"
		else
			# The grep found only 0-9, so it's an integer. We can safely do a test on it.
			ANS=yes
		fi
	done

  clear
  echo "You chose to number each file and make the extension lowercase. The files below will be changed:"
  echo
  echo $(ls *.JPG)
  echo
  read -p "Do you still want to continue? (Y|n): "
    if [ "$REPLY" = "y" ] || [ "$REPLY" = "Y" ] || [ -z $REPLY ]; then
	    for i in *JPG; do mv -- "$i" "$NAME.$NUM.jpg"; (( NUM += 1 )); done
		echo
		echo "The files have been renamed!"
		exit 4
	else
	  exit 1
	fi

# Moves to lowercase extension
elif [ "$REPLY" = "2" ]; then

  clear
  echo "You have chosen to change the extensions to lowercase. The files below will be changed:"
  echo
  echo $(ls *.JPG)
  echo
  read -p "Do you still want to continue? (Y|n): "
if [ "$REPLY" = "y" ] || [ "$REPLY" = "Y" ] || [ -z $REPLY ]; then      
      for i in *.JPG; do mv -- "$i" "$(basename "$i" JPG)jpg"; done;
      echo
      echo "The extensions have been shrunk!"
    else
      echo "Exited"
	  exit 3
    fi

# Renames and sequences like so... $name.$num.jpg
elif [ "$REPLY" = "3" ]; then
	clear
	echo -n "Enter the name that each file will be renamed to (ex. myPicture): "; read NAME;
	echo 
	
	# Determines whether a number was entered and if it was zero or higher
	NUM=1
	while [ "$ANS" != "yes" ]
	do 
		echo -n "Enter the number you'd like to start at (1 Enter): "; read NUM;
		if [ -z $NUM ]; then
			NUM=1
		fi
		
		echo $NUM | grep "[^0-9]" > /dev/null 2>&1
		if [ "$?" = "0" ]; then
			# If the grep found something other than 0-9 then it's not an integer.
			ANS=no
			echo "Sorry, wanted a number"
		else
			# The grep found only 0-9, so it's an integer. We can safely do a test on it.
			ANS=yes
		fi
	done
	
	for i in *.JPG *.jpg; do mv -- "$i" "${NAME}.${NUM}.jpg"; (( NUM += 1 )); done;
	echo
	echo "The files have been renamed!"
	exit 4
	
else
  echo
  echo "Script exited."
  echo
  exit 1
fi