#!/bin/sh
#	Allow switching from one mac to another by saving stuff stored outside the /Users/ directory
#	Guillaume DURR
#	g.durr.removethis at free d o t france
#
# http://www.macosxhints.com/article.php?story=20040329150916121
#
#
#	Stuff are saved in the same folder this script is
#	If the script is executed by root, the crontab, mail DB, ... are saved for ALL users
###############################		Configure		###################################
# What Password to use to connect to the localhost mySQL database ?
sql_password=''
################################################################################
###############################		History		################################
#		22 june 04			0.1 			first release
#		august 04			0.2 			add GENERAL Preferences
#		sept 04				0.3 			add mailDB, Startupitem info
#		23 oct 04			0.4 			add mysql support
#		8 nov 04			0.5 			add "byHost" folder, correct the" User List" using NetInfo
#		7 nov 04			0.6 			save *conf files, FTP files
#		17 sept 05			0.7 			now save php.ini file, and entropy files if exists
#		11 mars 06			0.8 			now save sshd config
#		26 mars 06			0.9				copie du lancement du demon FTP
#		11 mai 06			0.91			ajout du dossier /Library/bin
#		16 juillet 06		0.92			ajout de /etc/my.cnf
#		27 juillet 06		0.93			Correction de plusieurs bug; Ajout des sauvegarde de /Library/Launch*
#		5 fev 07			0.94			add CISCO Profile (if exists)
#		18 mai 07			0.95			add list of activated LaunchDaemon
#		26 juillet 07		0.96			export all sysctl values; 
#		30 juillet 07		0.97			saved fstab
################################################################################



major=$(sw_vers -productVersion | cut -f 2 -d.)

if [ $major -eq 1 ]; 		then	puma=1; 		fi
if [ $major -eq 2 ]; 		then	jaguar=1; 		fi
if [ $major -eq 3 ]; 		then	panther=1; 		fi
if [ $major -eq 4 ]; 		then	tiger=1; 		fi
if [ $major -eq leopard ]; 	then	leopard=1; 		fi

X=$(sw_vers -productName);
if [ "$X" = "Mac OS X Server" ]; then
	type_os="s";			#server
elif [ "$X" = "Mac OS X" ]; then
	type_os="c";			#client
fi;






clear

#list_all_user=`/bin/ls /Users/ | grep -v "Shared" | grep -v "^\."`
list_all_user=$(nidump passwd / | grep "/Users"| awk 'BEGIN { FS = ":"} { print $1 }')

me=`whoami`
if [ "$me" != 'root' ]; then
 	list_user=$me
 	only_one_user=true
 else
 	list_user=$list_all_user
 	only_one_user=false
fi

cd `dirname "$0"`
mpath=`date`
mkdir "$mpath"
cd "$mpath"

#---------------------------------	General Infos	---------------------------------
echo "$mpath" > info.txt
echo "All Users" >> info.txt
echo "$list_all_user" >> info.txt
echo $PATH >> info.txt
echo "-------------------------------" >> info.txt
sw_vers >> info.txt
echo "SN" >> info.txt

ioreg -l | grep IOPlatformSerialNumber | awk -F\" '{print $4}' >> info.txt
#"
#ARD version
if [ -d "/System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/" ]; then
    echo "ARD 2.1 : present" >> info.txt
fi

if [ -d "/System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/" ]; then
    echo "ARD 1.2 :  present" >> info.txt
fi

#---------------------------------	APACHE/WEB	---------------------------------
mkdir "./http_web"
cp /etc/httpd/httpd.conf "./http_web/"
cp /etc/httpd/mime.types "./http_web/"


#le fichier user n'existe pas sur un serveur, on le test
if [ -d /etc/httpd/users ]; then
	cp -R /etc/httpd/users "./http_web/"
fi

if [ -f /etc/php.ini ]; then
	cp /etc/php.ini "./http_web/"
fi
### sauvegarde entropy http://www.entropy.ch/software/macosx/

#php5
if [ -d /usr/local/php5/ ]; then
	mkdir "./http_web/entropy"
	cp -R /usr/local/php5/ "./http_web/entropy"
fi

#php4
if [ -d /usr/local/php/ ]; then
	if [ ! -d "./http_web/entropy" ]; then
		mkdir "./http_web/entropy"
	fi
	cp -R /usr/local/php4/ "./http_web/entropy"
fi




	#---------------------------------	WEB SITE	---------------------------------
cp -R /Library/WebServer/ "./http_web/"

#---------------------------------	SSH	---------------------------------
mkdir "./ssh"
if [ -f /etc/sshd_config ]; then
	cp /etc/sshd_config "./ssh/"
fi


#---------------------------------	FTP	---------------------------------
mkdir "./ftp"
if [ -f /etc/ftpchroot ]; then
	cp /etc/ftpchroot "./ftp/"
fi
if [ -f /etc/ftpusers ]; then
	cp /etc/ftpusers "./ftp/"
fi

if [ -f /etc/ftpwelcome ]; then
	cp /etc/ftpwelcome "./ftp/"
fi

if [ -f /etc/motd ]; then
	cp /etc/motd "./ftp/"
fi

if [ -f /etc/ftpd.conf ]; then
	cp /etc/ftpd.conf "./ftp/"
fi

if [ -f /System/Library/LaunchDaemons/ftp.plist ]; then
	cp /System/Library/LaunchDaemons/ftp.plist "./ftp/"
fi

#ftpd.conf


#---------------------------------	GENERAL Preferences	---------------------------------

cp -R /Library/Preferences/SystemConfiguration .
cp /etc/ftpusers .
cp /etc/hostconfig .

mkdir "./conf"
cp /etc/*conf "./conf/"
mkdir "./conf/cups/"; cp /etc/cups/*conf "./conf/cups/"
mkdir "./conf/openldap/"; cp /etc/openldap/*conf "./conf/openldap/"
ln -s  "./http_web/httpd.conf" "./conf/httpd.conf"



        #---------------------------------	User Preferences	---------------------------------
mkdir "./preferences_users"
ethernet=$(ifconfig en0 | grep ether| awk '{print $2}'| sed 's/://g')
echo "Ethernet Address $ethernet" >> info.txt

for user in $list_user
		do
			if [ -d /Users/$user/Library/Preferences/ByHost/ ];
			then
				mkdir "./preferences_users/$user"
				cp /Users/$user/Library/Preferences/ByHost/*$ethernet.plist ./preferences_users/$user
			fi
        done



#---------------------------------	CRONTAB	---------------------------------
mkdir crontab
cp /etc/crontab ./crontab/

if [ $only_one_user == true ]; then
	crontab_user=`crontab -l`
	
	if [ -z  "$crontab_user" ]; then
		echo "no crontab for $user" > ./crontab/$me
	else
		echo  "$crontab_user" > ./crontab/$me
	fi	
else
	for user in $list_user
		do
			echo $user
			crontab_user=`crontab -l -u $user`
			if [ -z  "$crontab_user" ]; then
				echo "no crontab for $user" > ./crontab/$user
			else
				echo  "$crontab_user" > ./crontab/$user
			fi
		done
fi
cp /etc/crontab ./crontab/




#---------------------------------	STARTUPITEM	/ LaunchDaemon/Agent---------------------------------
if [ $major -ge 3 ]; then
	echo "StartUpItem" >> ./Info_startupitem.txt
	ls -lisa /Library/StartupItems/ >> ./Info_startupitem.txt
fi

if [ $tiger ]; then
	echo "LaunchDaemon, LaunchAgent" >> ./Info_startupitem.txt
	ls -lisa /Library/LaunchDaemon/ >> ./Info_startupitem.txt
	ls -lisa /Library/LaunchAgent/ >> ./Info_startupitem.txt
	echo "Launchctl list" >> ./Info_startupitem.txt
	launchctl list >> ./Info_startupitem.txt
fi
#---------------------------------	LOCAL MAIL DB	---------------------------------
mkdir mail
for user in $list_user
	do
		if [ -f /var/mail/$user ]; then
			cp /var/mail/$user ./mail/$user
		else
			echo "no mail DB for $user" > ./mail/$user
		fi
	done
	

#---------------------------------	SPECIAL VARIABLES	---------------------------------
sysctl -A > "./sysctl.txt"
nvram -p > "./nvram.txt"

#---------------------------------	fstab	---------------------------------
if [ -f /etc/fstab ]; then
	cp /etc/fstab "./fstab.txt"
fi
	

#--------------------	Sauvegarde de /Library/bin	
mkdir bin_library
if [ -d /Library/bin ]; then
	cp -R /Library/bin/ "./bin_library/"
fi
	
#--------------------	Sauvegarde des fichiers profile CISCO
mkdir "cisco profile"
if [ -d /private/etc/opt/cisco-vpnclient/Profiles/ ]; then
	cp -R /private/etc/opt/cisco-vpnclient/Profiles/ "./cisco profile"
fi
	
	
	#---------------------------------	MYSQL	---------------------------------
mkdir mysql
if [ -f /etc/my.cnf ]; then
	cp /etc/my.cnf "./mysql/"
fi

if [ -e /usr/local/mysql/bin/mysqldump ]; then
	if [ -z $sql_password ]; then
		echo "There is no default password to access the mySQL Database"
		echo "Next time, you can add a password by setting the 'sql_password' variable at the top of the script"
		echo "What password to access the mySQL Database?"
		read sql_password
	fi
	if [ -z "$sql_password" ]; then
        echo "no password given. Can not saved the mySQL Database" > ./mysql/mysql.txt
	else
	   /usr/local/mysql/bin/mysqldump -h localhost --user=root --password="$sql_password" --all-databases > ./mysql.txt
	fi
fi
#---------------------------------	END	---------------------------------

