Mercurial > ec-dotfiles
changeset 124:5bafb912837e
Massive addition of old scripts collection.
author | Edho Prima Arief <edho@myconan.net> |
---|---|
date | Wed, 26 Oct 2011 15:22:00 +0700 |
parents | 173833e4ba35 |
children | 7c01fc6f7460 |
files | bin/256colors2.pl bin/basicopt bin/cek.pl bin/cek.rb bin/ed2k.py bin/index bin/index2 bin/moeauto bin/moeauto-genlink bin/moedump bin/old-cek bin/proxy-loop bin/putcrc.pl bin/smu bin/statfun bin/testcrc bin/xkcdmania |
diffstat | 17 files changed, 597 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/256colors2.pl Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,63 @@ +#!/usr/bin/perl +# Author: Todd Larason <jtl@molehill.org> +# $XFree86: xc/programs/xterm/vttests/256colors2.pl,v 1.2 2002/03/26 01:46:43 dickey Exp $ + +# use the resources for colors 0-15 - usually more-or-less a +# reproduction of the standard ANSI colors, but possibly more +# pleasing shades + +# colors 16-231 are a 6x6x6 color cube +for ($red = 0; $red < 6; $red++) { + for ($green = 0; $green < 6; $green++) { + for ($blue = 0; $blue < 6; $blue++) { + printf("\x1b]4;%d;rgb:%2.2x/%2.2x/%2.2x\x1b\\", + 16 + ($red * 36) + ($green * 6) + $blue, + ($red ? ($red * 40 + 55) : 0), + ($green ? ($green * 40 + 55) : 0), + ($blue ? ($blue * 40 + 55) : 0)); + } + } +} + +# colors 232-255 are a grayscale ramp, intentionally leaving out +# black and white +for ($gray = 0; $gray < 24; $gray++) { + $level = ($gray * 10) + 8; + printf("\x1b]4;%d;rgb:%2.2x/%2.2x/%2.2x\x1b\\", + 232 + $gray, $level, $level, $level); +} + + +# display the colors + +# first the system ones: +print "System colors:\n"; +for ($color = 0; $color < 8; $color++) { + print "\x1b[48;5;${color}m "; +} +print "\x1b[0m\n"; +for ($color = 8; $color < 16; $color++) { + print "\x1b[48;5;${color}m "; +} +print "\x1b[0m\n\n"; + +# now the color cube +print "Color cube, 6x6x6:\n"; +for ($green = 0; $green < 6; $green++) { + for ($red = 0; $red < 6; $red++) { + for ($blue = 0; $blue < 6; $blue++) { + $color = 16 + ($red * 36) + ($green * 6) + $blue; + print "\x1b[48;5;${color}m "; + } + print "\x1b[0m "; + } + print "\n"; +} + + +# now the grayscale ramp +print "Grayscale ramp:\n"; +for ($color = 232; $color < 256; $color++) { + print "\x1b[48;5;${color}m "; +} +print "\x1b[0m\n";
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/basicopt Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,11 @@ +#!/bin/sh +get_filename() { + printf "%s\n" "$*" | sed -e 's/^\(.*\)\.[^.]*$/\1/' +} +get_extension() { + if [ -n "$(printf "%s\n" "$*" | sed -e 's/^[^.]*//')" ]; then + printf "%s\n" "$*" | sed -e 's/^.*\(\.[^.]*$\)/\1/' + else + printf "%s\n" "" + fi +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/cek.pl Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,49 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use String::CRC32; +use File::Basename; + +my @files = @ARGV or print("Usage: ",basename($0)," file1 file2 ... fileN\n") && exit(1); +my $num_ok=0; +my $num_err=0; +my $num_nf=0; +my $num_na=@files; + +foreach(@files) { + my $filename = $_; + my ($name_crc,$real_crc); + unless (-f $filename) { + $num_nf++;$num_na--; + print(qq(Could not find "$filename", skipping\n)); + next(); + } + if(/(\[|\()([0-9A-F]{8})(\]|\))/i){ + $name_crc=uc($2); + } + open(FILE,"<",$filename); + binmode(FILE); + $real_crc=sprintf("%08X",crc32(*FILE)); + close(FILE); + if($name_crc) { + $num_na--; + if($name_crc eq $real_crc) { + $num_ok++; + print("$filename: OK - $real_crc\n"); + } else { + $num_err++; + print("$filename: NOT OK - $real_crc, should be $name_crc\n"); + } + } else { + print("$filename: $real_crc\n"); + } +} + +printf("%s\n","-"x40); +if($num_ok > 0) { print("Files OK: $num_ok\n"); } +if($num_err > 0) { print("Files error: $num_err\n"); } +if($num_nf > 0) { print("Files not found: $num_nf\n"); } +if($num_na > 0) { print("Files without crc information: $num_na\n"); } + +exit(0);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/cek.rb Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby +require 'zlib' + +block = 1048576 +ARGV.each do |filename| + crc = 0 + file = File.open(filename, 'rb') + currentbyte = 0 + while (line = file.read(block)) do + crc = Zlib.crc32(line,crc) + end + file.close + printf("%s %08X\n", filename, crc.to_s) +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/ed2k.py Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,19 @@ +#!/usr/bin/env python + +import os, sys +from Crypto.Hash import MD4 + +def ed2k(filename): + block = 9500*1024 + hash = "" + file = open(filename, "rb") + fileblock = file.read(block) + while fileblock: + hash += MD4.new(fileblock).digest() + fileblock = file.read(block) + file.close() + return MD4.new(hash).hexdigest() + +if __name__ == "__main__": + for file in sys.argv[1:]: + print "%s %s" %(ed2k(file), file)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/index Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,176 @@ +#!/bin/sh + +#un(POSIX / bourne shell)-ness: "read -r" + +WORKDIR="${PWD}" +TEMPFILE="${WORKDIR}/temp/all" +RESFILE="${WORKDIR}/list/all" +ARCDIR="${WORKDIR}/archives" +FILEDIRS="complete1 complete3 complete4 complete5 incomplete movie" +#FILEDIRS="complete-all incomplete movie" + +Err_Fatal() { + echo "Error: $*" + exit 1 +} + +Progress_Init() { + _last="-" + printf "${_last}" +} + +Progress_Anim() { + case "${_last}" in + /) _last="-";; + -) _last=\\;; + \\) _last=\|;; + \|) _last="/";; + esac + printf "\b${_last}" +} + +Progress_Done() { printf "\bdone\n"; } + +Check_Dirs() { + [ -w "${WORKDIR}" ] || Err_Fatal "Unable to write ${WORKDIR}" + for dir in temp list archives backup; do + mkdir -p "${WORKDIR}/${dir}" 2>/dev/null + [ -w "${WORKDIR}/${dir}" ] || Err_Fatal "Unable to write ${WORKDIR}/${dir}" + done +} + +Finish_Copy() { + printf "Copying result files... " + rm "${WORKDIR}/list/"* + cp "${WORKDIR}/temp/"* "${WORKDIR}/list" + mkdir -p "archives/${DATE}" && cp "${WORKDIR}/temp/"* "${WORKDIR}/archives/${DATE}" + echo "done" +} + +Generate_File_List() { + printf "Generating list of files... " + > "${TEMPFILE}-bare" + for i in ${FILEDIRS}; do + printf "${i}... " + dir="/anime/${i}" + [ -d "${dir}" ] && find "${dir}/"* -type f >> "${TEMPFILE}-bare" + done + echo "done" + printf "Checking list validity... " + [ -n "$(grep -v ^/ "${TEMPFILE}-bare")" ] && Err_Fatal "Newline in filename. Please check ${TEMPFILE}-bare" + echo "done" + + printf "Generating list of filenames... " + sed -e 's/.*\/\([^/]*\)/\1/' "${TEMPFILE}-bare" > "${TEMPFILE}-basename" + echo "done" + + printf "Sorting and finding duplicates... " + sort -- "${TEMPFILE}-basename" > "${TEMPFILE}-basename-sorted" + mv -- "${TEMPFILE}-basename-sorted" "${TEMPFILE}-basename" + sort -u -- "${TEMPFILE}-basename" > "${TEMPFILE}-basename-sortuniq" + comm -23 -- "${TEMPFILE}-basename" "${TEMPFILE}-basename-sortuniq" > "${TEMPFILE}-dup-basename" + echo "done" + if [ "$(wc -l < "${TEMPFILE}-dup-basename")" -gt 0 ]; then + > "${TEMPFILE}-dup-bare" + printf "Duplicate files detected. Generating list of duplicates... " + Progress_Init + while read -r dup; do + fgrep "${dup}" "${TEMPFILE}-bare" >> "${TEMPFILE}-dup-bare" + Progress_Anim + done < "${TEMPFILE}-dup-basename" + Progress_Done + Err_Fatal "Duplicated files detected. Please resolve this before continuing. + Check ${TEMPFILE}-dup-basename for list of duplicates" + else + rm "${TEMPFILE}-dup-basename" "${TEMPFILE}-basename-sortuniq" + fi + #yay finished +} + +Generate_New_List() { + printf "Generating list of new files... " + Progress_Init + > "${TEMPFILE}-newbase" + sort "${RESFILE}-basename" > "${TEMPFILE}-res-basename" + mv "${TEMPFILE}-res-basename" "${RESFILE}-basename" + comm -13 "${RESFILE}-basename" "${TEMPFILE}-basename" > "${TEMPFILE}-newbase" + Progress_Anim + > "${TEMPFILE}-new" + while read -r added; do + fgrep "${added}" "${TEMPFILE}-bare" >> "${TEMPFILE}-new" + Progress_Anim + done < "${TEMPFILE}-newbase" + rm "${TEMPFILE}-newbase" + Progress_Done +} + +Init_Check() { + cp "${TEMPFILE}-bare" "${TEMPFILE}-new" +} + +Run_ed2k() { + #[ "$(echo $(wc -l < "${TEMPFILE}-new"))" -eq 0 ] && Err_Fatal "Not an error. Just lazy to create new handler. (Nothing new)" + printf "Generating ed2k links...0" + > "${TEMPFILE}-ed2k-newlist" + num=0 + while read -r file; do + ed2k -l "${file}" >> "${TEMPFILE}-ed2k-newlist" + num=$((num+1)) + if [ "$((num%10))" -eq 0 ]; then + printf "${num}" + else + printf "." + fi + done < "${TEMPFILE}-new" + echo " done" +} + +Generate_Removed() { + printf "Generating list of removed files... " + Progress_Init + cp "${TEMPFILE}-ed2k-newlist" "${ARCDIR}/ed2k-newlist.${DATE}" + cat "${RESFILE}-ed2k-newlist" >> "${TEMPFILE}-ed2k-newlist" + sort -u "${TEMPFILE}-ed2k-newlist" > "${TEMPFILE}-ed2k-newlist-temp" + mv "${TEMPFILE}-ed2k-newlist-temp" "${TEMPFILE}-ed2k-newlist" + Progress_Anim + comm -13 "${TEMPFILE}-basename" "${RESFILE}-basename" | sort > "${TEMPFILE}-removed" + Progress_Anim + if [ "$(echo $(wc -l < "${TEMPFILE}-removed"))" -gt 0 ]; then + > "${TEMPFILE}-ed2k-newlisttemp" + while read -r deleted; do + fgrep "${deleted}" "${TEMPFILE}-ed2k-newlist" >> "${TEMPFILE}-ed2k-removed" + Progress_Anim + fgrep -v "${deleted}" "${TEMPFILE}-ed2k-newlist" > "${TEMPFILE}-ed2k-newlisttemp" + mv "${TEMPFILE}-ed2k-newlisttemp" "${TEMPFILE}-ed2k-newlist" + Progress_Anim + done < "${TEMPFILE}-removed" + cp "${TEMPFILE}-ed2k-removed" "${ARCDIR}/ed2k-removed.${DATE}" + #sed -e 's/ed2k:\/\/|file|\(.*\)|[^|]*|[^|]*|$/\1/' "${TEMPFILE}-ed2k-removed" > "${TEMPFILE}-base-removed" + Progress_Anim + while read -r deleted; do + fgrep "${deleted}" "${RESFILE}-bare" >> "${TEMPFILE}-bare-removed" + Progress_Anim + done < "${TEMPFILE}-removed" + cp "${TEMPFILE}-bare-removed" "${ARCDIR}/bare-removed.${DATE}" + fi + Progress_Done +} + +INIT= +UPDATE= +DATE=$(date -u "+%Y%m%d-%H.%M") +case "$1" in + init) INIT=1;; + update|up) UPDATE=1;; + *) Err_Fatal "You fail.";; +esac +Check_Dirs +[ -n "${UPDATE}" ] && cd "${WORKDIR}" && tar zcf "${WORKDIR}/backup/${DATE}.tar.gz" "temp" "list" "archives" +[ -n "${UPDATE}" ] && rm "${WORKDIR}/temp/"* 2>/dev/null +Generate_File_List +[ -n "${INIT}" ] && Init_Check +[ -n "${UPDATE}" ] && Generate_New_List +Run_ed2k +[ -n "${UPDATE}" ] && Generate_Removed +Finish_Copy +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/index2 Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,39 @@ +#!/bin/sh + +# Index version 2! Much better than first one. Hopefully. + +# Execute safely. +safe_path() +{ + __start=$(printf "%s" | cut -c 1) + __path= + case "${__start}" in + .|/) __path="$*";; + *) __path="./$*";; + esac + printf "%s" "${__path}" +} + +get_basename() { basename "$(safe_path "$*")"; } +get_basename_pipe() { sed -e 's#.*/\([/]*\)$#\1#'; } + +filename_hash() +{ + printf "%s" "$(get_basename "$*")" +} +generate_table_of_files() +{ + filetable_generator "$@" +} +filetable_generator() +{ + # Uses perl for speed. + perl -e 'use File::Find; +use Digest::SHA1 qw(sha1_hex); +use_warnings; +use_strict; +sub printer { printf("%s\n", sha1_hex($_)) if(-e && -f && !-l); } +find(\&printer, @ARGV);' "$@" +} +_srcdir=. +generate_table_of_files "$@"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/moeauto Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,24 @@ +#!/bin/sh + +if [ "$#" -lt 1 ]; then + cat <<EOF +Usage: $(basename -- "$0") +EOF + exit 1 +fi + +if [ ! -r "$*" ] || [ -d "$*" ]; then + cat <<EOF +File "$*" can't be read! +EOF + exit 1 +fi + +SERVER= +while read LINE; do + if [ -n "$(printf "%s" "${LINE}" | grep "^SERVER=")" ]; then + SERVER=$(printf "%s" "${LINE}" | sed -e 's/SERVER=//') + else + [ -n "${SERVER}" ] && [ ! -z "$(printf "%s" "${LINE}" | grep -v "^$")" ] && moefetch fetch -s "${SERVER}" ${LINE} + fi +done < "$*"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/moeauto-genlink Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,24 @@ +#!/bin/sh + +if [ "$#" -lt 1 ]; then + cat <<EOF +Usage: $(basename -- "$0") +EOF + exit 1 +fi + +if [ ! -r "$*" ] || [ -d "$*" ]; then + cat <<EOF +File "$*" can't be read! +EOF + exit 1 +fi + +SERVER= +while read LINE; do + if [ -n "$(printf "%s" "${LINE}" | grep "^SERVER=")" ]; then + SERVER=$(printf "%s" "${LINE}" | sed -e 's/SERVER=//') + else + [ -n "${SERVER}" ] && [ ! -z "$(printf "%s" "${LINE}" | grep -v "^$")" ] && moefetch check -s "${SERVER}" ${LINE} + fi +done < "$*"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/moedump Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,38 @@ +#!/bin/sh + +safe_mv() { mv -- "$@"; } +safe_ln() { ln -s -- "$1" "$2"; } +safe_basename() { basename -- "$1"; } +get_md5() { ${MD5} -- "$1" | cut -d' ' -f1; } +get_filename() { safe_basename "${1%.*}"; } + +eod() { "$@" || errx "$@"; } +errx() { echo "Error executing: $@"; exit 1; } +MD5="md5sum" +[ "$(safe_basename "${PWD}")" = ".all" ] || { echo "Wrong working directory. This script must be run in .all directory. Aborting."; exit 1; } + +[ -d "../.trash" ] || eod mkdir ../.trash + +echo "Total files: $(find ../* -type f | wc -l) files." + +num=0 +for i in ../*/* +do + if [ -h "$i" ] + then + [ -f "$i" ] || { eod safe_mv "$i" "../.trash" && echo "Broken file: $(safe_basename "$i"). Moved to trash."; } + else + if [ "$(get_md5 "$i")" = "$(get_filename "$i")" ] + then + eod safe_mv "$i" . + eod safe_ln "../.all/$(safe_basename "$i")" "$i" + else + eod safe_mv "$i" "../.trash" && echo "Broken file: $(get_filename "$i"). Moved to .trash." + fi + printf "%s" "." + num=$((num+1)) + [ "$((num%100))" -eq 0 ] && printf "${num}" + fi +done + +echo "done"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/old-cek Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,49 @@ +#!/usr/bin/perl + +use warnings; +use strict; +use String::CRC32; +use File::Basename; + +unless($#ARGV+1>0) { + print("Usage: ", basename($0), " file1 ... fileN\n"); + exit(1); +} + +my @files = @ARGV; +#or print("Usage: ", basename($0), " file1 ... fileN\n") && exit(1); + # print usage message if called without arguments + +foreach (@files) { + my $filename = $_; + my ($input, $name_checksum, $real_checksum, $checksum); + unless((-r $filename) && (-f $filename)) { + print(qq("$filename" is not a file or unreadable, skipping\n)); + next(); + } + #print(qq(Could not find file "$filename", skipping\n)) and next() unless -r $filename; + #print(qq("$filename" is not a file, skipping\n)) and next() unless -f $filename; + + open(FILE,$filename); + $real_checksum = sprintf("%08X",crc32(*FILE)); + close(FILE); + + if ( /(\[|\()([0-9A-F]{8})(\]|\))/i ) { # does it have a checksum? + $name_checksum = $2; + if (lc($real_checksum) eq lc($name_checksum)) { + print("${filename}: $real_checksum - OK!\n"); + next(); + } + else { + print("${filename}: $real_checksum - NOT OK! Should be ${name_checksum}!\n"); + next(); + } + } + else { # can't find checksum in filename, just print filename + generated checksum and let the user do the thinking + print("${filename}: $real_checksum\n"); + next(); + } +} + +exit(0); +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/proxy-loop Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,5 @@ +#!/bin/sh +while true; do + ssh -N localhost + sleep 10 +done
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/putcrc.pl Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +use warnings; +use strict; +use String::CRC32; + +my @files = @ARGV or print("Usage: ren file1 ... fileN\n") && exit(1); +my $skip=0; +foreach (@files) { + my $newname = $_; + open(FILE,$_); + my $crc=sprintf("%08X",crc32(*FILE)); + close(FILE); + $newname =~ s/(.*)\.([^.]*)/$1 [$crc].$2/; + $newname =~ s/\] (\[$crc)/]$1/; + print(qq($_: $newname already exists, skipping\n)) and next() if -e $newname and $_ ne $newname; + if ($_ eq $newname) { + print("$_: no need to rename, skipping\n"); + } else { + if($skip==1) { print("$_ --> $newname\n"); } + else { + if(rename($_, $newname)) { print("$_ -> $newname\n"); } + else { print("$_: rename failed"); } + } + } + next(); +} + +exit(0);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/smu Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,4 @@ +#!/bin/sh + +keyword=$(printf "%s\n" "$*" | tr ' ' '*') +find /anime/music/ -iname "*${keyword}*"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/statfun Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,8 @@ +#!/bin/sh +> /tmp/mainlogfun +while read line; do + filename=$(printf "%s" "${line}" | grep OK\ DOWNLOAD | grep -v "^$" | sed -e 's/.*OK DOWNLOAD: Client "[^"]*", "\(.*\)"..*/\1/') + basename -- "${filename}" >> /tmp/mainlogfun +done < /srv/log/vsftpd/main.log +grep -v "^$" /tmp/mainlogfun > /root/mainlogfun +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/testcrc Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,16 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use String::CRC32; + +my @files = @ARGV or print("Usage u fail") && exit(1); + +foreach(@files) { + my $filename = $_; + open(FILE,$filename); + my $crc=crc32(*FILE); + close(FILE); + $crc=sprintf("%X",$crc); + print("$filename: $crc\n"); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/xkcdmania Wed Oct 26 15:22:00 2011 +0700 @@ -0,0 +1,29 @@ +#!/bin/sh + +dir="/anime/misc/webcomics/xkcd" +[ -d "${dir}" ] || exit 1 + +fetchlink() { + if [ "$1" -a "$2" ]; then + wget --quiet -O "$2" "$1" + else + return 1 + fi +} +latest=$(fetchlink http://xkcd.com/ - | grep 'Permanent link' | sed -e 's/.*\/\([0-9]*\)\/.*/\1/') +[ -n "${latest}" ] || exit 1 +next=1 +while [ "${next}" -le "${latest}" ]; do + to_dl="${to_dl} ${next}" + next=$((next + 1)) +done + +for i in $to_dl +do + if [ ! -n "$(find "$dir/$(printf '%04d' $i)"* -type f 2>/dev/null)" ] && [ "${i}" -ne 404 ]; then + page=$(fetchlink "http://xkcd.com/$i/" -) + img=$(echo "$page" | grep 'Image URL' | sed -e 's/.*\(http[^<]*\).*/\1/' | head -n 1) + echo "$(echo "$page" | grep "src" | grep "$img" | sed -e 's/.*title="\([^\"]*\)".*/\1/')" > "$dir/$(printf '%04d' $i) $(basename $img | sed -e 's/\(.*\)\..*/\1/').txt" + fetchlink "${img}" "$dir/$(printf '%04d' $i) $(basename "$img")" + fi +done