diff options
-rw-r--r-- | .links | 4 | ||||
-rwxr-xr-x | bin/bootstrap | 22 | ||||
-rwxr-xr-x | bin/checklinks | 90 | ||||
-rwxr-xr-x | bin/pkg | 66 | ||||
-rw-r--r-- | etc/hgrc | 2 |
5 files changed, 184 insertions, 0 deletions
@@ -0,0 +1,4 @@ +soft bin/checklinks ../packages/core/bin/checklinks +soft bin/pkg ../packages/core/bin/pkg +soft .ssh/config ../packages/core/etc/ssh_config +soft .hgrc packages/core/etc/hgrc diff --git a/bin/bootstrap b/bin/bootstrap new file mode 100755 index 0000000..d45597a --- /dev/null +++ b/bin/bootstrap @@ -0,0 +1,22 @@ +#!/bin/zsh +# bootstrap - populate a home with the most necessary scripts +# After running this, other packages can be installed using 'pkg' + +# Without mercurial, this is pretty pointless +if ! which hg &> /dev/null; then + echo Mercurial not found! + exit 100 +fi + +# Make basic dirctories +mkdir -p ~/bin +mkdir -p ~/packages + +# Fetch the core package, containing checklinks and pkg +cd ~/packages +echo "fetching core" +hg clone ssh://derf.homelinux.org/packages/core + +# Create symlinks for pkg and checklinks +cd core +bin/checklinks diff --git a/bin/checklinks b/bin/checklinks new file mode 100755 index 0000000..2f03551 --- /dev/null +++ b/bin/checklinks @@ -0,0 +1,90 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Term::ANSIColor; + +my $base = $ENV{HOME}; +my ($type, $src, $dst); +my $quiet = 0; + +unless (-f ".links") { + exit 0; +} + +if (defined($ARGV[0]) and $ARGV[0] eq '-q') { + $quiet = 1; +} + +open(LINKS, '<.links') or die($!); +while(<LINKS>) { + chomp; + ($type, $src, $dst) = split(/ +/); + if ($type eq 'soft') { + check_symlink($src, $dst); + } elsif ($type eq 'hard') { + check_hardlink($src, $dst); + } +} +close(LINKS); + +sub check_symlink { + my $src = shift; + my $dst = shift; + + #if (not -e "$base/$dst") { + # print_format('no dst!!', $src, $dst, 'red bold'); + #} + if (not -l "$base/$src" and not -e "$base/$src") { + symlink($dst, "$base/$src"); + print_format('created', $src, $dst, 'cyan'); + } + elsif (readlink("$base/$src") eq $dst) { + print_format('ok', $src, $dst, 'green') unless ($quiet); + } + elsif (readlink("$base/$src") eq "$base/$dst") { + print_format('absolute', $src, $dst, 'yellow') unless ($quiet); + } + elsif (not -l "$base/$src" and -e "$base/$src") { + print colored ("$base/$src: File exists but is not a symlink. Not updating.\n", 'bold red'); + } + elsif (-l "$base/$src") { + unlink("$base/$src"); + symlink($dst, "$base/$src"); + print_format('fixed', $src, $dst, 'cyan'); + } +} + +sub check_hardlink { + my $src = shift; + my $dst = shift; + + if (not -e "$base/$dst") { + print_format('no dst!!', $src, $dst, 'red bold'); + } + elsif (not -f "$base/$src") { + link("$base/$dst", "$base/$src") or warn($!); + print_format('created', $src, $dst, 'cyan'); + } + elsif ((stat("$base/$src"))[1] != (stat("$base/$dst"))[1]) { + unlink("$base/$src"); + link("$base/$dst", "$base/$src") or warn($!); + print_format('updated', $src, $dst, 'cyan'); + } + elsif ((stat("$base/$src"))[1] == (stat("$base/$dst"))[1]) { + print_format('ok', $src, $dst, 'green') unless ($quiet); + } +} + +sub print_format { + my ($message, $src, $dst, $color) = @_; + + $message .= ' 'x(9-length($message)); + $src .= ' 'x(15-length($src)); + $dst .= ' 'x(15-length($dst)); + if (defined($color)) { + print colored ($message, $color); + } else { + print $message; + } + print "$src -> $dst\n"; +} @@ -0,0 +1,66 @@ +#!/usr/bin/env zsh +PDIR="$HOME/packages" +PKG_ROOT="ssh://derf.homelinux.org/packages" +local info=$'\e[0;36m' +local reset=$'\e[0m' + +echo_status () { + echo "${info}$*${reset}" +} + +if [ ! -d $PDIR ]; then + echo_status "$PDIR not found!!" + exit 100 +fi + +pkg_add () { + if [ -d $PDIR/$1 ]; then + echo_status "Package already installed!" + return 100 + fi + cd $PDIR || return 255 + echo_status 'Retrieving package...' + hg clone $PKG_ROOT/$1 || return 255 + if [ -f $1/hooks/post-add ]; then + echo_status 'Executing post-add hook' + . $1/hooks/post-add + fi + echo_status 'Checking symlinks...' + cd $1 + checklinks + return 0 +} + +pkg_remove () { + if [ ! -d $PDIR/$1 ]; then + echo_status "Package not installed!" + return 100 + fi + if [ -f $PDIR/$1/hooks/pre-remove ]; then + echo_status 'Executing pre-remove hook' + . $PDIR/$1/hooks/pre-remove + fi + rm -r $PDIR/$1 + echo_status "Package removed." +} + +pkg_update () { + cd $PDIR + for i in *(/); { + echo_status "Updating package $i..." + cd $i + hg fetch + checklinks + if [ -f hooks/post-update ]; then + echo_status 'Executing post-update hook' + . hooks/post-update + fi + cd .. + } +} + +case "$1" in + add) pkg_add "$2" ;; + update) pkg_update ;; + remove) pkg_remove "$2" ;; +esac diff --git a/etc/hgrc b/etc/hgrc new file mode 100644 index 0000000..6e45831 --- /dev/null +++ b/etc/hgrc @@ -0,0 +1,2 @@ +[ui] +username=Daniel Friesel <derf@derf.homelinux.org> |