Skip to content

Instantly share code, notes, and snippets.

@cakyus
Last active August 29, 2015 14:05
Show Gist options
  • Save cakyus/a8323295dd7d3ace4c3c to your computer and use it in GitHub Desktop.
Save cakyus/a8323295dd7d3ace4c3c to your computer and use it in GitHub Desktop.
md5sum based file repository
#!/bin/sh
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
cmd_help() {
echo "md5sum based file repository"
echo "usage: fsx command [arguments]"
echo "commands:"
echo " init initialize"
echo " add <file> add file into repository"
echo " move <file> move file into repository"
echo " list list all files in repository"
exit 1
}
cmd_add_file() {
echo ">>>" add file $1
MD5FILE=`md5sum "$1" | awk '{print $1}'`
if [ -f ".fsx/$MD5FILE" ]
then
echo $MD5FILE `realpath "$1"` >> ".fsx/MD5SUM"
else
cp "$1" ".fsx/$MD5FILE"
echo $MD5FILE `realpath "$1"` >> ".fsx/MD5SUM"
fi
cat ".fsx/MD5SUM" | sort | uniq > ".fsx/MD5SUM.tmp"
cp ".fsx/MD5SUM.tmp" ".fsx/MD5SUM"
cat ".fsx/MD5SUM" | grep $MD5FILE
exit 0
}
cmd_move_file() {
FILE=`realpath '$1'`
echo ">>>" move file $FILE
MD5FILE=`md5sum "$FILE" | awk '{print $1}'`
if [ -f ".fsx/$MD5FILE" ]
then
mv "$FILE" ".fsx/$MD5FILE.X"
echo $MD5FILE $FILE >> ".fsx/MD5SUM"
else
mv "$FILE" ".fsx/$MD5FILE"
echo $MD5FILE $FILE >> ".fsx/MD5SUM"
fi
cat ".fsx/MD5SUM" | sort | uniq > ".fsx/MD5SUM.tmp"
cp ".fsx/MD5SUM.tmp" ".fsx/MD5SUM"
cat ".fsx/MD5SUM" | grep $MD5FILE
exit 0
}
cmd_add_dir() {
echo ">>>" add dir $1
echo ">>> sorry, not yet supported"
exit 0
}
cmd_list() {
if [ -f ".fsx/MD5SUM" ]
then
cat ".fsx/MD5SUM"
fi
exit 0
}
cmd_init() {
if [ -d ".fsx" ] || mkdir ".fsx"
then
echo 0k
fi
exit 0
}
if [ "$1" = "init" ]
then
cmd_init
elif [ "$1" = "list" ]
then
cmd_list
elif [ "$1" = "add" ]
then
if [ -f "$2" ]
then
cmd_add_file "$2"
elif [ -d "$2" ]
then
cmd_add_dir "$2"
fi
elif [ "$1" = "move" ]
then
if [ -f "$2" ]
then
cmd_move_file "$2"
fi
fi
cmd_help
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment