#!/bin/bash

path_to_svn=/palmdata/svn_repository
path_to_trac=/palmdata/trac

temp_filename=$path_to_trac/passwd_tmp

#echo " "
#echo "Path to svn-directory:  $path_to_svn"
#echo "Path to trac-directory: $path_to_trac"
#echo "svn-passwd file:        $path_to_svn/conf/passwd"
#echo "trac-passwd file:       $path_to_trac/.htpasswd"
#echo " "
#echo "   RIGHT (Y/N)?"
#read confirmation
#if [ $confirmation == "Y" ]; then

#echo " "
#echo " "
#echo " Start converting file!"
#echo " "

# test if svn-passwd-file exists
if [ ! -f "$path_to_svn/conf/passwd" ];
then
  echo "Sorry, svn passwd-file does not exist. Please make sure the name is correct: $path_to_svn/conf/passwd"
  exit 1
fi

# In this part it is hard-coded, that the svn-passwd-file has 7 lines above first user-definition!
#echo " "
#echo "Copy $path_to_svn/conf/passwd to $temp_filename"
tail -n +7 $path_to_svn/conf/passwd > $temp_filename 
#echo "done."


#echo " "
#echo "Create backup of old file... .htpasswd_old"
cp $path_to_trac/.htpasswd $path_to_trac/.htpasswd_old
#echo "done."

#echo " "
#echo "Enter $path_to_trac"
cd $path_to_trac
#echo "done."

if [ ! -f ".htpasswd" ];
then
# just create file!
echo " "
datum=`date`
echo "** $datum:"
echo "   Create .htpasswd and adding dummy-user"
nohup htpasswd2 -cb .htpasswd dummy dummy
#echo "done."
fi

#echo " "
#echo "Add all usernames from $path_to_svn/conf/passwd to $path_to_trac/.htpasswd"
awk '{FS=" = "}{system("htpasswd2 -b .htpasswd " $1" "$2)}' < $temp_filename
#echo "done."

#echo " "
#echo "Delete dummy-user"
htpasswd2 -D .htpasswd dummy
#echo "done."

#echo " "
#echo "Remove $temp_filename"
rm $temp_filename
#echo "done."

#echo " "
#echo "  -->> All passwords encrypted and saved. No need to restart trac!"
#echo " "

#else 
#    echo "EXIT - edit this script!"
#    exit
#fi 


