Last active
August 29, 2015 14:26
-
-
Save pedrokoblitz/f14abbbcbbb32cc98fa9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
readonly WORKING_DIR=$(pwd) | |
readonly CONVERSION_DIR=/tmp/conversion | |
readonly LOG_FILE=$WORKING_DIR/conversion.log | |
remove_base_dir() { | |
local SED_BUFFER=/tmp/sed.buffer | |
echo $1 > $SED_BUFFER | |
sed -e 's/\/home\/pedro//g' $SED_BUFFER | |
rm $SED_BUFFER | |
} | |
get_encoding() { | |
local INFO=$(file $1) | |
if [[ $INFO == *"UTF-8"* ]]; then | |
echo "utf-8" | |
elif [[ $INFO == *"ISO-8859"* ]]; then | |
echo "iso-8859-1" | |
elif [[ $INFO == *"ASCII"* ]]; then | |
echo "ascii" | |
fi | |
} | |
init_log_file () { | |
echo "fullpath,name,encoding" > $LOG_FILE | |
} | |
log_encoding() { | |
local FILE=$1 | |
echo $FILE "," $(basename $FILE) "," $(get_encoding $FILE) >> $LOG_FILE | |
} | |
convert_to_latin1() { | |
local ENCODING=$(get_encoding $1) | |
local INFILE=$1 | |
local OUTFILE=${CONVERSION_DIR}/$(remove_base_dir $1) | |
if [[ $ENCODING -ne "iso-8859-1" ]]; then | |
iconv -f "$ENCODING" -t latin1 "$INFILE" -o "$OUTFILE" | |
log_encoding $OUTFILE | |
fi | |
} | |
get_dir_list() { | |
find $WORKING_DIR -type d | |
} | |
get_file_list() { | |
find $WORKING_DIR -type f -name '*.php' | |
} | |
remove_conversion_folder() { | |
rm -rf ${CONVERSION_DIR}/ | |
} | |
remove_git_folder() { | |
rm -rf ${CONVERSION_DIR}/.git/ | |
} | |
clone_dir_structure() { | |
for DIR in $(get_dir_list); do | |
local NAME=$(remove_base_dir $DIR) | |
mkdir -p ${CONVERSION_DIR}/${NAME} | |
done | |
} | |
process_files() { | |
init_log_file | |
for FILE in $(get_file_list); do | |
convert_to_latin1 $FILE | |
done | |
} | |
sync_back() { | |
rsync -r ${CONVERSION_DIR}/ ${WORKING_DIR}/ | |
} | |
main() { | |
echo "cloning directory structure" | |
clone_dir_structure && echo "done" && \ | |
echo "processing files" && \ | |
process_files && echo "done" && \ | |
echo "syncing back to origin" && \ | |
remove_git_folder && echo "removed git directory" && \ | |
sync_back && echo "done" && \ | |
remove_conversion_folder && echo "removed temp directory" | |
echo "the end" | |
} | |
main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
readonly WORKING_DIR=$(pwd) | |
readonly CONVERSION_DIR=/tmp/conversion | |
readonly LOG_FILE=$WORKING_DIR/conversion.log | |
remove_base_dir() { | |
local SED_BUFFER=/tmp/sed.buffer | |
echo $1 > $SED_BUFFER | |
sed -e 's/\/home\/pedro//g' $SED_BUFFER | |
rm $SED_BUFFER | |
} | |
get_encoding() { | |
local INFO=$(file $1) | |
if [[ $INFO == *"UTF-8"* ]]; then | |
echo "utf-8" | |
elif [[ $INFO == *"ISO-8859"* ]]; then | |
echo "iso-8859-1" | |
elif [[ $INFO == *"ASCII"* ]]; then | |
echo "ascii" | |
fi | |
} | |
init_log_file () { | |
echo "fullpath,name,encoding" > $LOG_FILE | |
} | |
log_encoding() { | |
local FILE=$1 | |
echo $FILE "," $(basename $FILE) "," $(get_encoding $FILE) >> $LOG_FILE | |
} | |
convert_to_utf8() { | |
local ENCODING=$(get_encoding $1) | |
local INFILE=$1 | |
local OUTFILE=${CONVERSION_DIR}/$(remove_base_dir $1) | |
if [[ $ENCODING ]]; then | |
iconv -f "$ENCODING" -t utf-8 "$INFILE" -o "$OUTFILE" | |
log_encoding $OUTFILE | |
fi | |
} | |
get_dir_list() { | |
find $WORKING_DIR -type d | |
} | |
get_file_list() { | |
find $WORKING_DIR -type f | |
} | |
remove_conversion_folder() { | |
rm -rf ${CONVERSION_DIR}/ | |
} | |
remove_git_folder() { | |
rm -rf ${CONVERSION_DIR}/.git/ | |
} | |
clone_dir_structure() { | |
for DIR in $(get_dir_list); do | |
local NAME=$(remove_base_dir $DIR) | |
mkdir -p ${CONVERSION_DIR}/${NAME} | |
done | |
} | |
process_files() { | |
init_log_file | |
for FILE in $(get_file_list); do | |
convert_to_utf8 $FILE | |
done | |
} | |
sync_back() { | |
rsync -r ${CONVERSION_DIR}/ ${WORKING_DIR}/ | |
} | |
main() { | |
echo "cloning directory structure" | |
clone_dir_structure && echo "done" && \ | |
echo "processing files" && \ | |
process_files && echo "done" && \ | |
echo "syncing back to origin" && \ | |
remove_git_folder && echo "removed git directory" && \ | |
sync_back && echo "done" && \ | |
remove_conversion_folder && echo "removed temp directory" | |
echo "the end" | |
} | |
main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare PROJECTS_DIR=${HOME}/projects | |
declare BACKUP_DIR=${HOME}/backup | |
declare WEBROOT=/var/www | |
declare PROJECT_LIST=${HOME}/projects/project.list | |
generate_project_list() { | |
cat /dev/null > $PROJECT_LIST | |
for i in $(ls -d ./*/) | |
do | |
basename $i >> $PROJECT_LIST | |
done | |
} | |
pull_all_repos() { | |
for i in $(cat $PROJECT_LIST) | |
do | |
cd $i | |
gitpull master | |
done | |
} | |
push_all_repos() { | |
for i in $(cat $PROJECT_LIST) | |
do | |
cd $i | |
gitpush master | |
done | |
} | |
gitpull() { | |
local BRANCH=$1 | |
git pull origin $BRANCH | |
} | |
sync_backup() { | |
rsync -r ${PROJECTS_DIR}/ ${BACKUP_DIR}/ | |
} | |
restore_backup() { | |
local PROJECT=$1 | |
rsync -r ${BACKUP_DIR}/${PROJECT}/ ${PROJECTS_DIR}/${PROJECT}/ | |
} | |
sync_project_www() { | |
local PROJECT=$1 | |
rsync -r ${PROJECTS_DIR}/${PROJECT}/ ${WEBROOT}/${PROJECT} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment